Skip to content

Commit

Permalink
Added support for custom fields for contacts, added support for Money…
Browse files Browse the repository at this point in the history
…bird API different types of nested objects (as array or as object)
  • Loading branch information
stephangroen committed Dec 15, 2015
1 parent 004ec4d commit 45744ff
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
14 changes: 13 additions & 1 deletion src/Picqer/Financials/Moneybird/Entities/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* Class Contact
* @package Picqer\Financials\Moneybird
*/
class Contact extends Model {
class Contact extends Model
{

use FindAll, FindOne, Storable, Removable, Synchronizable;

Expand Down Expand Up @@ -67,4 +68,15 @@ class Contact extends Model {
* @var string
*/
protected $namespace = 'contact';

/**
* @var array
*/
protected $multipleNestedEntities = [
'custom_fields' => [
'entity' => 'ContactCustomField',
'type' => self::NESTING_TYPE_NESTED_OBJECTS,
],
];

}
19 changes: 19 additions & 0 deletions src/Picqer/Financials/Moneybird/Entities/ContactCustomField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace Picqer\Financials\Moneybird\Entities;

use Picqer\Financials\Moneybird\Model;

/**
* Class ContactCustomField
* @package Picqer\Financials\Moneybird\Entities
*/
class ContactCustomField extends Model {

/**
* @var array
*/
protected $fillable = [
'id',
'value',
];

}
5 changes: 4 additions & 1 deletion src/Picqer/Financials/Moneybird/Entities/SalesInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ class SalesInvoice extends Model {
* @var array
*/
protected $multipleNestedEntities = [
'details' => 'SalesInvoiceDetail'
'details' => [
'entity' => 'SalesInvoiceDetail',
'type' => self::NESTING_TYPE_ARRAY_OF_OBJECTS,
],
];

/**
Expand Down
31 changes: 24 additions & 7 deletions src/Picqer/Financials/Moneybird/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
abstract class Model
{

const NESTING_TYPE_ARRAY_OF_OBJECTS = 0;
const NESTING_TYPE_NESTED_OBJECTS = 1;

/**
* @var Connection
*/
Expand Down Expand Up @@ -44,11 +47,16 @@ abstract class Model
protected $singleNestedEntities = [];

/**
* Array containing the name of the attribute that contains nested objects as key and an array with the entity name
* and json representation type
*
* JSON representation of an array of objects (NESTING_TYPE_ARRAY_OF_OBJECTS) : [ {}, {} ]
* JSON representation of nested objects (NESTING_TYPE_NESTED_OBJECTS): { "0": {}, "1": {} }
*
* @var array
*/
protected $multipleNestedEntities = [];


/**
* Model constructor.
* @param Connection $connection
Expand Down Expand Up @@ -193,6 +201,7 @@ public function jsonWithNamespace()
private function getArrayWithNestedObjects($useAttributesAppend = true)
{
$result = [];
$multipleNestedEntities = $this->getMultipleNestedEntities();

foreach ($this->attributes as $attributeName => $attributeValue) {
if (! is_object($attributeValue)) {
Expand All @@ -203,13 +212,20 @@ private function getArrayWithNestedObjects($useAttributesAppend = true)
$result[$attributeName] = $attributeValue->attributes;
}

if (array_key_exists($attributeName, $this->getMultipleNestedEntities())) {
if (array_key_exists($attributeName, $multipleNestedEntities)) {
if ($useAttributesAppend) {
$attributeName .= '_attributes';
$attributeNameToUse = $attributeName . '_attributes';
} else {
$attributeNameToUse = $attributeName;
}
$result[$attributeName] = [];
foreach ($attributeValue as $attributeObject) {
$result[$attributeName][] = $attributeObject->attributes;

$result[$attributeNameToUse] = [];
foreach ($attributeValue as $attributeEntity) {
$result[$attributeNameToUse][] = $attributeEntity->attributes;

if ($multipleNestedEntities[$attributeName]['type'] === self::NESTING_TYPE_NESTED_OBJECTS) {
$result[$attributeNameToUse] = (object)$result[$attributeNameToUse];
}
}
}
}
Expand All @@ -236,6 +252,7 @@ public function makeFromResponse($response)
/**
* Recreate this object with the response from the API
* @param $response
* @return $this
*/
public function selfFromResponse($response)
{
Expand All @@ -249,7 +266,7 @@ public function selfFromResponse($response)

foreach ($this->getMultipleNestedEntities() as $key => $value)
{
$entityName = 'Picqer\Financials\Moneybird\Entities\\' . $value;
$entityName = 'Picqer\Financials\Moneybird\Entities\\' . $value['entity'];
$instaniatedEntity = new $entityName($this->connection);
$this->$key = $instaniatedEntity->collectionFromResult($response[$key]);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Picqer/Financials/Moneybird/Moneybird.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Picqer\Financials\Moneybird\Entities\Administration;
use Picqer\Financials\Moneybird\Entities\Contact;
use Picqer\Financials\Moneybird\Entities\ContactCustomField;
use Picqer\Financials\Moneybird\Entities\CustomField;
use Picqer\Financials\Moneybird\Entities\DocumentStyle;
use Picqer\Financials\Moneybird\Entities\FinancialAccount;
Expand Down Expand Up @@ -61,6 +62,14 @@ public function contact()
return new Contact($this->connection);
}

/**
* @return ContactCustomField
*/
public function contactCustomField()
{
return new ContactCustomField($this->connection);
}

/**
* @return CustomField
*/
Expand Down

0 comments on commit 45744ff

Please sign in to comment.