Skip to content
This repository has been archived by the owner on Apr 29, 2022. It is now read-only.

Commit

Permalink
Add employee employment
Browse files Browse the repository at this point in the history
  • Loading branch information
dextermb committed Jan 4, 2019
1 parent b6ab44b commit 996ba1f
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 44 deletions.
134 changes: 102 additions & 32 deletions src/XeroPHP/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function __construct(array $user_config)
//better here for overriding
$this->setConfig($user_config);

$this->oauth_client = new Client($this->config['oauth']);
$this->oauth_client = new Client($this->config[ 'oauth' ]);
}

/**
Expand All @@ -91,31 +91,33 @@ public function getAuthorizeURL($oauth_token = null)
*/
public function getConfig($key)
{
if (!isset($this->config[$key])) {
if (!isset($this->config[ $key ])) {
throw new Exception("Invalid configuration key [$key]");
}
return $this->config[$key];
return $this->config[ $key ];
}

/**
* @param string $config
* @param mixed $option
* @param mixed $value
* @return mixed
* @throws Exception
*/
public function getConfigOption($config, $option) {
if (!isset($this->getConfig($config)[$option])) {
* @param string $config
* @param mixed $option
* @param mixed $value
* @return mixed
* @throws Exception
*/
public function getConfigOption($config, $option)
{
if (!isset($this->getConfig($config)[ $option ])) {
throw new Exception("Invalid configuration option [$option]");
}
return $this->getConfig($config)[$option];
return $this->getConfig($config)[ $option ];
}

/**
* @param array $config
* @return array
*/
public function setConfig($config) {
public function setConfig($config)
{
$this->config = array_replace_recursive(
self::$_config_defaults,
static::$_type_config_defaults,
Expand All @@ -132,11 +134,12 @@ public function setConfig($config) {
* @return array
* @throws Exception
*/
public function setConfigOption($config, $option, $value) {
if (!isset($this->config[$config])) {
public function setConfigOption($config, $option, $value)
{
if (!isset($this->config[ $config ])) {
throw new Exception("Invalid configuration key [$config]");
}
$this->config[$config][$option] = $value;
$this->config[ $config ][ $option ] = $value;
return $this->config;
}

Expand Down Expand Up @@ -166,12 +169,12 @@ public function validateModelClass($class)
/**
* Prepend the configuration namespace to the class.
*
* @param string $class
* @param string $class
* @return string
*/
protected function prependConfigNamespace($class)
{
return $this->getConfig('xero')['model_namespace'].'\\'.$class;
return $this->getConfig('xero')[ 'model_namespace' ] . '\\' . $class;
}


Expand Down Expand Up @@ -300,9 +303,10 @@ public function save(Remote\Model $object, $replace_data = false)
}

$url = new URL($this, $uri, $object::getAPIStem());

$request = new Request($this, $url, $method);

if(!empty($object::getRootNodeName())) {
if (!empty($object::getRootNodeName())) {

//Put in an array with the first level containing only the 'root node'.
$data = [$object::getRootNodeName() => $object->toStringArray(true)];
Expand All @@ -315,16 +319,31 @@ public function save(Remote\Model $object, $replace_data = false)
}

$response = $request->getResponse();
$current = current($response->getElements());

if (false !== $element = current($response->getElements())) {
$object->fromStringArray($element, $replace_data);
if ($current !== false) {
if (!is_array($current)) {
$object->fromStringArray($response->getElements(), $replace_data);
} else {
$object->fromStringArray($element, $replace_data);
}
}

//Mark the object as clean since no exception was thrown
$object->setClean();

return $response;
}

/**
* @param Remote\Model $object
* @throws Exception
*/
public function saveRelationships(Remote\Model $object)
{
return $this->savePropertiesDirectly($object);
}

/**
* @param Collection|array $objects
* @return Remote\Response
Expand All @@ -335,7 +354,7 @@ public function saveAll($objects, $checkGuid = true)
$objects = array_values($objects);

//Just get one type to compare with, doesn't matter which.
$current_object = $objects[0];
$current_object = $objects[ 0 ];
/**
* @var Object $type
*/
Expand Down Expand Up @@ -373,8 +392,8 @@ public function saveAll($objects, $checkGuid = true)

foreach ($response->getElements() as $element_index => $element) {
if ($response->getErrorsForElement($element_index) === null) {
$objects[$element_index]->fromStringArray($element);
$objects[$element_index]->setClean();
$objects[ $element_index ]->fromStringArray($element);
$objects[ $element_index ]->setClean();
}
}

Expand All @@ -393,31 +412,82 @@ public function saveAll($objects, $checkGuid = true)
private function savePropertiesDirectly(Remote\Model $object)
{
foreach ($object::getProperties() as $property_name => $meta) {
if ($meta[Remote\Model::KEY_SAVE_DIRECTLY] && $object->isDirty($property_name)) {
//Then actually save
if ($meta[ Remote\Model::KEY_SAVE_DIRECTLY ] && $object->isDirty($property_name)) {
$property_objects = $object->$property_name;
/** @var Object $property_type */

if (is_object($property_objects)) {

/** @var Remote\Model $model */
$model = $property_objects;

$url = new URL(
$this,
sprintf(
'%s/%s/%s',
$object::getResourceURI(),
$object->getGUID(),
$model::getResourceURI()
),
$model::getAPIStem()
);

$method = $model::getCreateMethod() ?? Request::METHOD_PUT;
$data = $model->toStringArray(true);

$request = new Request($this, $url, $method);
$request->setBody(json_encode($data), Request::CONTENT_TYPE_JSON);
$request->send();

$response = $request->getResponse();

foreach ($response->getElements() as $key => $element) {
if ($response->getErrorsForElement($key) === null) {
$method = 'set' . ucfirst($key);

if (method_exists($model, $method)) {
$model->{$method}($element);
$model->setClean($key);
}
}
}

continue;
}

/** @var Remote\Model[] $property_type */
$property_type = get_class(current($property_objects));

$url = new URL($this, sprintf('%s/%s/%s', $object::getResourceURI(), $object->getGUID(), $property_type::getResourceURI()));
$request = new Request($this, $url, Request::METHOD_PUT);
$url = new URL(
$this,
sprintf(
'%s/%s/%s',
$object::getResourceURI(),
$object->getGUID(),
$property_type::getResourceURI()
)
);

$method = Request::METHOD_PUT;
$request = new Request($this, $url, $method);

$property_array = [];

/** @var Object[] $property_objects */
foreach ($property_objects as $property_object) {
$property_array[] = $property_object->toStringArray(false);
}

$root_node_name = Helpers::pluralize($property_type::getRootNodeName());
$request->setBody(Helpers::arrayToXML([$root_node_name => $property_array]));

$request->setBody(Helpers::arrayToXML([$root_node_name => $property_array]));
$request->send();

$response = $request->getResponse();

foreach ($response->getElements() as $element_index => $element) {
if ($response->getErrorsForElement($element_index) === null) {
$property_objects[$element_index]->fromStringArray($element);
$property_objects[$element_index]->setClean();
$property_objects[ $element_index ]->fromStringArray($element);
$property_objects[ $element_index ]->setClean();
}
}

Expand Down
33 changes: 29 additions & 4 deletions src/XeroPHP/Models/PayrollUK/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace XeroPHP\Models\PayrollUK;

use XeroPHP\Models\PayrollUK\Employee\Address;
use XeroPHP\Models\PayrollUK\Employee\Employment;
use XeroPHP\Remote;
use XeroPHP\Traits\TitleCaseKeysBeforeSave;

Expand Down Expand Up @@ -85,17 +86,21 @@ public static function isPageable()
public static function getProperties()
{
return [
'employeeID' => [true, self::PROPERTY_TYPE_GUID, null, false, false],
'title' => [true, self::PROPERTY_TYPE_STRING, null, false, false],
'employeeID' => [false, self::PROPERTY_TYPE_GUID, null, false, false],

// Required when creating, not brought back in root GET
'title' => [false, self::PROPERTY_TYPE_STRING, null, false, false],
'gender' => [false, self::PROPERTY_TYPE_STRING, null, false, false],
'address' => [false, self::PROPERTY_TYPE_OBJECT, 'PayrollUK\\Employee\\Address', false, false],

'firstName' => [true, self::PROPERTY_TYPE_STRING, null, false, false],
'lastName' => [true, self::PROPERTY_TYPE_STRING, null, false, false],
'dateOfBirth' => [true, self::PROPERTY_TYPE_DATE, '\\DateTimeInterface', false, false],
'gender' => [true, self::PROPERTY_TYPE_STRING, null, false, false],
'address' => [true, self::PROPERTY_TYPE_OBJECT, 'PayrollUK\\Employee\\Address', false, false],
'email' => [false, self::PROPERTY_TYPE_STRING, null, false, false],
'phoneNumber' => [false, self::PROPERTY_TYPE_STRING, null, false, false],
'updatedDateUTC' => [false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTimeInterface', false, false],
'createdDateUTC' => [false, self::PROPERTY_TYPE_TIMESTAMP, '\\DateTimeInterface', false, false],
'employment' => [false, self::PROPERTY_TYPE_OBJECT, 'PayrollUK\\Employee\\Employment', false, true],
];
}

Expand Down Expand Up @@ -259,6 +264,26 @@ public function setPhoneNumber(string $value)
return $this;
}

/**
* @return Employment
*/
public function getEmployment()
{
return $this->_data[ 'employment' ];
}

/**
* @param Employment $value
* @return Employment
*/
public function setEmployment(Employment $value)
{
$this->propertyUpdated('employment', $value);
$this->_data[ 'employment' ] = $value;

return $value;
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit 996ba1f

Please sign in to comment.