Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Update Method to Application when GUID property doesn't exist i.e TaxRate #905

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions src/XeroPHP/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function loadByGUID($model, $guid)
/** @var Remote\Model $class */
$class = $this->validateModelClass($model);

if(!$guid){
if (! $guid) {
throw new Remote\Exception\NotFoundException;
}

Expand Down Expand Up @@ -238,7 +238,7 @@ public function loadByGUIDs($model, $guids)
/** @var $class Remote\Model */
$class = $this->validateModelClass($model);

if(empty($guids)){
if (empty($guids)) {
return [];
}

Expand Down Expand Up @@ -288,7 +288,7 @@ public function save(Remote\Model $object, $replace_data = false)
//(special saving endpoints)
$this->savePropertiesDirectly($object);

if (!$object->isDirty()) {
if (! $object->isDirty()) {
return null;
}

Expand All @@ -305,7 +305,51 @@ public function save(Remote\Model $object, $replace_data = false)
$object->setApplication($this);
}

if (!$object::supportsMethod($method)) {
if (! $object::supportsMethod($method)) {
throw new Exception(sprintf('%s doesn\'t support [%s] via the API', get_class($object), $method));
}

//Put in an array with the first level containing only the 'root node'.
$data = [$object::getRootNodeName() => $object->toStringArray(true)];
$url = new URL($this, $uri, $object::getAPIStem());
$request = new Request($this, $url, $method);

$request->setBody(Helpers::arrayToXML($data))->send();
$response = $request->getResponse();

if (false !== $element = current($response->getElements())) {
$object->fromStringArray($element, $replace_data);
}
//Mark the object as clean since no exception was thrown
$object->setClean();

return $response;
}

/**
* @param Remote\Model $object
* @param bool $replace_data
*
* @throws Exception
*
* @return Remote\Response|null
*/
public function update(Remote\Model $object, $replace_data = false)
{
//Saves any properties that don't want to be included in the normal loop
//(special saving endpoints)
$this->savePropertiesDirectly($object);

if (! $object->isDirty()) {
return null;
}

$object->validate();

$method = $object::supportsMethod(Request::METHOD_POST) ? Request::METHOD_POST : Request::METHOD_PUT;
$uri = sprintf('%s/%s', $object::getResourceURI(), $object->getGUID());

if (! $object::supportsMethod($method)) {
throw new Exception(sprintf('%s doesn\'t support [%s] via the API', get_class($object), $method));
}

Expand Down Expand Up @@ -441,7 +485,7 @@ private function savePropertiesDirectly(Remote\Model $object)
*/
public function delete(Remote\Model $object)
{
if (!$object::supportsMethod(Request::METHOD_DELETE)) {
if (! $object::supportsMethod(Request::METHOD_DELETE)) {
throw new Exception(
sprintf(
'%s doesn\'t support [DELETE] via the API',
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/PurchaseOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function getLineItems()
public function addLineItem(LineItem $value)
{
$this->propertyUpdated('LineItems', $value);
if (! isset($this->_data['LineItems'])) {
if (!isset($this->_data['LineItems'])) {
$this->_data['LineItems'] = new Remote\Collection();
}
$this->_data['LineItems'][] = $value;
Expand Down
2 changes: 1 addition & 1 deletion src/XeroPHP/Models/Accounting/TaxRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function getTaxComponents()
public function addTaxComponent(TaxComponent $value)
{
$this->propertyUpdated('TaxComponents', $value);
if (! isset($this->_data['TaxComponents'])) {
if (!isset($this->_data['TaxComponents'])) {
$this->_data['TaxComponents'] = new Remote\Collection();
}
$this->_data['TaxComponents'][] = $value;
Expand Down
3 changes: 2 additions & 1 deletion src/XeroPHP/Models/Accounting/TrackingCategory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace XeroPHP\Models\Accounting;

use XeroPHP\Remote;
Expand Down Expand Up @@ -207,7 +208,7 @@ public function getTrackingOptionID()
{
return $this->_data['TrackingOptionID'];
}

/**
* @param string $value
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace XeroPHP\Models\Accounting\TrackingCategory;

use XeroPHP\Remote;
Expand Down
32 changes: 29 additions & 3 deletions src/XeroPHP/Remote/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public function fromStringArray($input_array, $replace_data = false)
continue;
}

if ($isArray && ! is_array($input_array[$property])) {
if ($isArray && !is_array($input_array[$property])) {
$this->_data[$property] = null;

continue;
Expand Down Expand Up @@ -349,7 +349,7 @@ public static function castFromString($type, $value, $php_type)
case self::PROPERTY_TYPE_BOOLEAN:
return in_array(strtolower($value), ['true', '1', 'yes'], true);

/** @noinspection PhpMissingBreakStatementInspection */
/** @noinspection PhpMissingBreakStatementInspection */
case self::PROPERTY_TYPE_TIMESTAMP:
$timezone = new \DateTimeZone('UTC');

Expand Down Expand Up @@ -428,6 +428,24 @@ public function validate($check_children = true)
return true;
}

/**
* Shorthand update an object if it is instantiated with app context.
*
* @throws Exception
*
* @return Response|null
*/
public function update()
{
if ($this->_application === null) {
throw new Exception(
'->update() is only available on objects that have an injected application context.'
);
}

return $this->_application->update($this);
}

/**
* Shorthand save an object if it is instantiated with app context.
*
Expand Down Expand Up @@ -494,6 +512,10 @@ public function __isset($property)
*/
public function __get($property)
{
if (empty($property)) {
return false;
}

$getter = sprintf('get%s', $property);

if (method_exists($this, $getter)) {
Expand All @@ -513,6 +535,10 @@ public function __get($property)
*/
public function __set($property, $value)
{
if (empty($property)) {
return false;
}

$setter = sprintf('set%s', $property);

if (method_exists($this, $setter)) {
Expand All @@ -521,7 +547,7 @@ public function __set($property, $value)

trigger_error(sprintf("Undefined property %s::$%s.\n", __CLASS__, $property));
}

protected function propertyUpdated($property, $value)
{
$currentValue = isset($this->_data[$property]) ? $this->_data[$property] : null;
Expand Down