Skip to content

Commit

Permalink
Implement a prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed May 27, 2016
1 parent 4d2f412 commit a42e2c4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions src/Model/Directives.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,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 [];
}
}
24 changes: 21 additions & 3 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion tests/src/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function getAvailableProperties()
['links', null],
['attributes', null],
['relationships', null],
['meta', null]
['meta', null],
['private-attributes', null]
];
}

Expand Down

0 comments on commit a42e2c4

Please sign in to comment.