diff --git a/README.md b/README.md index bfe61dc..83490de 100644 --- a/README.md +++ b/README.md @@ -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 = 'example@example.org'; $contact->save(); +var_dump($contact); // Contact object (as saved in Moneybird) // Example: Use the Moneybird synchronisation API $contactVersions = $moneybird->contact()->listVersions(); diff --git a/example/example.php b/example/example.php index 6d9e016..41cdbbf 100644 --- a/example/example.php +++ b/example/example.php @@ -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(); diff --git a/src/Picqer/Financials/Moneybird/Actions/Storable.php b/src/Picqer/Financials/Moneybird/Actions/Storable.php index e553cad..64b3c0d 100644 --- a/src/Picqer/Financials/Moneybird/Actions/Storable.php +++ b/src/Picqer/Financials/Moneybird/Actions/Storable.php @@ -13,10 +13,10 @@ public function save() { if ($this->exists()) { - return $this->update(); + $this->update(); } else { - return $this->insert(); + $this->insert(); } } @@ -27,7 +27,7 @@ public function insert() { $result = $this->connection()->post($this->url, $this->jsonWithNamespace()); - return $this->makeFromResponse($result); + $this->selfFromResponse($result); } /** @@ -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); } } \ No newline at end of file diff --git a/src/Picqer/Financials/Moneybird/Model.php b/src/Picqer/Financials/Moneybird/Model.php index 8c22cf2..a85b2ab 100644 --- a/src/Picqer/Financials/Moneybird/Model.php +++ b/src/Picqer/Financials/Moneybird/Model.php @@ -219,6 +219,7 @@ private function getArrayWithNestedObjects($useAttributesAppend = true) /** + * Create a new object with the response from the API * @param $response * @return static */ @@ -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; } /**