Commit a5bf2252 authored by Karamel's avatar Karamel
Browse files

Update write API to return the full object, instead of it's id only.

parent 14a39186
......@@ -98,24 +98,22 @@ abstract class APIHelper implements API
/** Write a single or an array of entries. If the ID is set, it's an update,
* otherwise it's a create. Sets the ID in $data.
* $data Entities to write/update.
* @return The Id(s)
* @return The single entry or the array of entries.
* @throw \InvalidArgumentException when at least one entry is not of the
* managed class. */
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
foreach ($data as $d) {
$this->dao->write($d);
}
$this->dao->commit();
$ids = array();
foreach ($data as $d) {
$ids[] = $d->getId();
}
if (count($data) == 1) {
$ids = $ids[0];
if ($arrayArgs) {
return $data;
} else {
return $data[0];
}
return $ids;
}
/** Delete a single or an array of entries by their ID.
......
......@@ -33,7 +33,8 @@ abstract class APIRefHelper extends APIHelper
* Sets the ID in $data.
* @return the ID(s), like for APIHelper->write. */
public function forceWrite($data) {
if (!is_array($data)) {
$arrayArgs = is_array($data);
if (!$arrayArgs) {
$data = array($data);
}
for ($i = 0; $i < count($data); $i++) {
......@@ -50,7 +51,7 @@ abstract class APIRefHelper extends APIHelper
}
}
}
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
/** Get a single entry from it's reference. */
......
......@@ -118,13 +118,14 @@ class CashsessionAPI extends APIHelper implements API
* be done first with the opened session. Then register all the tickets and
* finally call write with the full cash session data.
* When closing a session, the next one is automatically created.
* @return The Id(s)
* @return The entry or the array of entries.
* @throw \BadMethodCallException When trying to close a session without
* writing it as opened before, or when trying to update an already closed
* session. */
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
$previousFTicket = null;
foreach ($data as $d) {
// Defensive checks
......@@ -192,14 +193,11 @@ class CashsessionAPI extends APIHelper implements API
}
$this->updateEOSTicket($previousFTicket);
$this->dao->commit();
$ids = array();
foreach ($data as $d) {
$ids[] = $d->getId();
}
if (count($data) == 1) {
$ids = $ids[0];
if ($arrayArgs) {
return $data;
} else {
return $data[0];
}
return $ids;
}
/** Update the signature of the end-of-sequence FiscalTicket with
......
......@@ -39,7 +39,8 @@ class CurrencyAPI extends APIRefHelper
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
$main = $this->getMain();
$updateMain = false;
$removeMain = false;
......@@ -63,7 +64,7 @@ class CurrencyAPI extends APIRefHelper
$main->setMain(false);
$this->dao->write($main);
}
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
/** Delete a single or an array of entries by their ID.
......
......@@ -64,7 +64,8 @@ class CustomerAPI extends APIHelper implements API
* dedicated function. */
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
for ($i = 0; $i < count($data); $i++) {
$d = $data[$i];
if ($d->getId() === null) {
......@@ -82,7 +83,7 @@ class CustomerAPI extends APIHelper implements API
}
$data[$i] = $d;
}
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
/** Update the customer's balance for when the balance is updated
......
......@@ -76,7 +76,8 @@ class ImageAPI extends APIHelper implements API
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
foreach ($data as $d) {
if ($this->thumbnailer !== null) {
$thumb = $this->thumbnailer->thumbnail($d->getImage());
......@@ -89,7 +90,7 @@ class ImageAPI extends APIHelper implements API
// Set hasImage to true in associated model.
$this->updateHasImage($d->getModel(), $d->getModelId(), true);
}
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
public function delete($id) {
......
......@@ -36,7 +36,8 @@ class PaymentmodeAPI extends APIHelper
// Same as APIHelper::write but delete return and values for replacement.
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
// Delete previously registered return and values
$returnsHelper = new SubclassHelper($this->dao,
PaymentModeReturn::class, 'returns', 'paymentMode');
......@@ -44,7 +45,7 @@ class PaymentmodeAPI extends APIHelper
$valuesHelper = new SubclassHelper($this->dao,
PaymentModeValue::class, 'values', 'paymentMode');
$valuesHelper->deleteSubrecords($data);
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
public function delete($id) {
......
......@@ -34,12 +34,13 @@ class PlaceAPI extends APIHelper
// Same as APIHelper::write but delete all places for replacement.
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
// Delete previously registered places
$placesHelper = new SubclassHelper($this->dao,
Place::class, 'places', 'floor');
$placesHelper->deleteSubrecords($data);
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
public function delete($id) {
......
......@@ -35,12 +35,13 @@ class TariffareaAPI extends APIHelper
// Same as APIHelper::write but delete all price for replacement.
public function write($data) {
$this->supportOrDie($data);
$data = (is_array($data)) ? $data : array($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
// Delete previously registered prices
$pricesHelper = new SubclassHelper($this->dao,
TariffAreaPrice::class, 'prices', 'tariffArea');
$pricesHelper->deleteSubrecords($data);
return parent::write($data);
return parent::write(($arrayArgs) ? $data : $data[0]);
}
public function delete($id) {
......
......@@ -123,10 +123,10 @@ class APIHelperTest extends TestCase
$cat = new Category();
$cat->setReference('ref1');
$cat->setLabel('Test1');
$id = $this->api->write($cat);
$this->assertNotNull($id);
$this->assertEquals($cat->getId(), $id);
$read = $this->dao->read(APIHelperMock::MODEL_NAME, $id);
$resCat = $this->api->write($cat);
$this->assertNotNull($resCat);
$this->assertEquals($cat->getId(), $resCat->getId());
$read = $this->dao->read(APIHelperMock::MODEL_NAME, $resCat->getId());
$this->checkEquality($cat, $read);
}
......@@ -138,14 +138,14 @@ class APIHelperTest extends TestCase
$cat2->setReference('ref2');
$cat2->setLabel('Test2');
$writeData = array($cat, $cat2);
$ids = $this->api->write($writeData);
$this->assertNotNull($ids[0]);
$this->assertNotNull($ids[1]);
$this->assertEquals(2, count($ids));
$this->assertEquals($cat->getId(), $ids[0]);
$this->assertEquals($cat2->getId(), $ids[1]);
$read = $this->dao->read(APIHelperMock::MODEL_NAME, $ids[0]);
$read2 = $this->dao->read(APIHelperMock::MODEL_NAME, $ids[1]);
$resCats = $this->api->write($writeData);
$this->assertNotNull($resCats[0]->getId());
$this->assertNotNull($resCats[1]->getId());
$this->assertEquals(2, count($resCats));
$this->assertEquals($cat->getId(), $resCats[0]->getId());
$this->assertEquals($cat2->getId(), $resCats[1]->getId());
$read = $this->dao->read(APIHelperMock::MODEL_NAME, $resCats[0]->getId());
$read2 = $this->dao->read(APIHelperMock::MODEL_NAME, $resCats[1]->getId());
$this->checkEquality($cat, $read);
$this->checkEquality($cat2, $read2);
}
......
......@@ -64,10 +64,10 @@ class APIRefHelperTest extends TestCase
$cat2 = new Category();
$cat2->setReference('A');
$cat2->setLabel('edited');
$id = $this->api->forceWrite($cat2);
$resCat = $this->api->forceWrite($cat2);
$this->assertEquals(1, $this->api->count());
$this->assertEquals($cat->getId(), $id);
$read = $this->api->get($id);
$this->assertEquals($cat->getId(), $resCat->getId());
$read = $this->dao->readSnapshot(Category::class, $resCat->getId());
$this->assertEquals($cat2->getLabel(), $read->getLabel());
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment