Skip to content

Commit

Permalink
Add updateItem method
Browse files Browse the repository at this point in the history
  • Loading branch information
xxswingxx committed Oct 8, 2018
1 parent d193eb8 commit c029cbc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "quaderno/quaderno",
"type": "library",
"description": "PHP wrapper for the Quaderno REST API",
"version": "1.6.2",
"version": "1.6.3",
"keywords": ["billing","receipts", "invoices", "stripe", "paypal", "braintree", "gocardless", "vat", "gst"],
"homepage": "https://github.com/quaderno/quaderno-php",
"license": "MIT",
Expand Down
8 changes: 4 additions & 4 deletions quaderno_class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function __get($name)
return array_key_exists($name, $this->data) ? $this->data[$name] : null;
}

public function __isset($name)
{
return array_key_exists($name, $this->data);
}
public function __isset($name)
{
return array_key_exists($name, $this->data);
}

protected function getArray()
{
Expand Down
24 changes: 22 additions & 2 deletions quaderno_document.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,28 @@ public function addContact($contact)
public function addItem($item)
{
$length = isset($this->data['items_attributes']) ? count($this->data['items_attributes']) : 0;
$this->data['items_attributes'][$length] = $item->getArray();
return count($this->data['items_attributes']) == $length + 1;

if (isset($item->id) && $length > 0)
{
$index = 0;

for($index; $index < $length; $index ++){
if ($this->data['items_attributes'][$index]['id'] == $item->id) break;
}

$this->data['items_attributes'][$index] = $item->getArray();
return count($this->data['items_attributes']) == $length;
}
else
{
$this->data['items_attributes'][$length] = $item->getArray();
return count($this->data['items_attributes']) == $length + 1;
}
}

public function updateItem($item)
{
return $this->addItem(new QuadernoDocumentItem($item));
}

/* Interface - only subclasses which implement original ones (i.e. without exec-) can call these methods */
Expand Down
8 changes: 7 additions & 1 deletion quaderno_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,19 @@ public function save()
if ($new_data) $this->data = $new_data->data;
}

$new_data = (isset($this->data['items_attributes']) && !$new_object);

/**
* 3rd step - Update object
* Update object - This is only necessary when it's not a new object, or new payments have been created.
*/
if (!$new_object || $new_data)
{
$response = QuadernoBase::save(static::$model, $this->data, $this->id);
$body_data = $this->data;

if (isset($this->items)) unset($body_data['items']);

$response = QuadernoBase::save(static::$model, $body_data, $this->id);

if (QuadernoBase::responseIsValid($response))
{
Expand Down

0 comments on commit c029cbc

Please sign in to comment.