From 326851a6ed16bbcabfa3c9fd2b43777510f3612e Mon Sep 17 00:00:00 2001 From: Xenofon Spafaridis Date: Fri, 27 May 2016 18:44:24 +0300 Subject: [PATCH] Implement a prototype (cherry picked from commit a42e2c4c1ebb0af81e6711c981b2e107748fd004) --- .travis.yml | 2 +- src/Model/Directives.php | 10 ++++++++++ src/Resource.php | 24 +++++++++++++++++++++--- tests/src/ResourceTest.php | 3 ++- 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index c126af4..e1b5c20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ before_script: script: - find src/ -name '*.php' -print0 | xargs -0 -L 1 php -l - find tests/ -name '*.php' -print0 | xargs -0 -L 1 php -l - - php vendon/bin/phpunit + - php vendor/bin/phpunit after_script: - php vendor/bin/coveralls -v - php vendor/bin/codacycoverage clover build/logs/clover.xml diff --git a/src/Model/Directives.php b/src/Model/Directives.php index d383cc4..84e1f31 100644 --- a/src/Model/Directives.php +++ b/src/Model/Directives.php @@ -517,4 +517,14 @@ function ($attribute) use ($escape) { return $query; } + + /** + * Returns the an array of current resource's attribute names which are private + * **MAY** be overwritten + * @return string[] + */ + public static function getPrivateAttributes() + { + return []; + } } diff --git a/src/Resource.php b/src/Resource.php index 5deb1bd..66341f3 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -81,7 +81,7 @@ public function __construct($type, $id) */ public function __set($name, $value) { - if (in_array($name, ['links', 'attributes', 'relationships', 'meta'])) { + if (in_array($name, ['links', 'attributes', 'relationships', 'meta', 'private-attributes'])) { if (!isset($this->{$name}) || $this->{$name} === null) { $this->{$name} = new \stdClass(); } @@ -106,7 +106,7 @@ public function __get($name) { if (in_array($name, ['id', 'type'])) { return $this->{$name}; - } elseif (in_array($name, ['links', 'attributes', 'relationships', 'meta'])) { + } elseif (in_array($name, ['links', 'attributes', 'relationships', 'meta', 'private-attributes'])) { return ( isset($this->{$name}) ? $this->{$name} @@ -237,6 +237,20 @@ public static function parseFromRecord( unset($record->{Resource::META_MEMBER}); } + //Parse private attributes first + $privateAttributes = new \stdClass(); + + foreach ($modelClass::getPrivateAttributes() as $attribute) { + if (property_exists($record, $attribute)) { + $privateAttributes->{$attribute} = $record->{$attribute}; + unset($record->{$attribute}); + } + } + + if (count((array) $privateAttributes)) { + $resource->attributes = $privateAttributes; + } + if ($flagAttributes) { $resource->attributes = new \stdClass(); } @@ -396,6 +410,10 @@ public static function parseFromRecord( public function jsonSerialize() { - return get_object_vars($this); + $vars = get_object_vars($this); + + unset($vars['private-attributes']); + + return $vars; } } diff --git a/tests/src/ResourceTest.php b/tests/src/ResourceTest.php index 1b45918..ba2357d 100644 --- a/tests/src/ResourceTest.php +++ b/tests/src/ResourceTest.php @@ -34,7 +34,8 @@ public function getAvailableProperties() ['links', null], ['attributes', null], ['relationships', null], - ['meta', null] + ['meta', null], + ['private-attributes', null] ]; }