Skip to content

Commit

Permalink
Assign new values to self when saving an object
Browse files Browse the repository at this point in the history
  • Loading branch information
stephangroen committed Nov 27, 2015
1 parent 9a5df5d commit 62d88d3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ $contact->company_name = 'Picqer';
$contact->firstname = 'Stephan';
$contact->lastname = 'Groen';
$contact->save();
var_dump($contact); // Contact object (as saved in Moneybird)

// Example: Update existing contact, change email address
$contact = $moneybird->contact()->find(89672345789233);
$contact->email = '[email protected]';
$contact->save();
var_dump($contact); // Contact object (as saved in Moneybird)

// Example: Use the Moneybird synchronisation API
$contactVersions = $moneybird->contact()->listVersions();
Expand Down
2 changes: 1 addition & 1 deletion example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function connect($redirectUrl, $clientId, $clientSecret)
$connection->setAdministrationId($administrationId);
$moneybird = new \Picqer\Financials\Moneybird\Moneybird($connection);

// Get the contacts from our administration
// Get the sales invoices from our administration
try {
$salesInvoices = $moneybird->salesInvoice()->get();

Expand Down
8 changes: 4 additions & 4 deletions src/Picqer/Financials/Moneybird/Actions/Storable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public function save()
{
if ($this->exists())
{
return $this->update();
$this->update();
} else
{
return $this->insert();
$this->insert();
}
}

Expand All @@ -27,7 +27,7 @@ public function insert()
{
$result = $this->connection()->post($this->url, $this->jsonWithNamespace());

return $this->makeFromResponse($result);
$this->selfFromResponse($result);
}

/**
Expand All @@ -37,7 +37,7 @@ public function update()
{
$result = $this->connection()->patch($this->url . '/' . urlencode($this->id), $this->jsonWithNamespace());

return $this->makeFromResponse($result);
$this->selfFromResponse($result);
}

}
24 changes: 18 additions & 6 deletions src/Picqer/Financials/Moneybird/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ private function getArrayWithNestedObjects($useAttributesAppend = true)


/**
* Create a new object with the response from the API
* @param $response
* @return static
*/
Expand All @@ -227,20 +228,31 @@ public function makeFromResponse($response)
$entity = new static($this->connection);
$entity->fill($response);

foreach ($entity->getSingleNestedEntities() as $key => $value)
$entity->selfFromResponse($response);

return $entity;
}

/**
* Recreate this object with the response from the API
* @param $response
*/
public function selfFromResponse($response)
{
$this->fill($response);

foreach ($this->getSingleNestedEntities() as $key => $value)
{
$entityName = 'Picqer\Financials\Moneybird\Entities\\' . $value;
$entity->$key = new $entityName($this->connection, $response[$key]);
$this->$key = new $entityName($this->connection, $response[$key]);
}

foreach ($entity->getMultipleNestedEntities() as $key => $value)
foreach ($this->getMultipleNestedEntities() as $key => $value)
{
$entityName = 'Picqer\Financials\Moneybird\Entities\\' . $value;
$instaniatedEntity = new $entityName($this->connection);
$entity->$key = $instaniatedEntity->collectionFromResult($response[$key]);
$this->$key = $instaniatedEntity->collectionFromResult($response[$key]);
}

return $entity;
}

/**
Expand Down

0 comments on commit 62d88d3

Please sign in to comment.