Skip to content

Commit

Permalink
Get attributes as flat array for JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
dkulyk committed Feb 2, 2016
1 parent 944ae78 commit 9f0f394
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Devio/Propertier/Propertier.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,32 @@ public function setAttribute($key, $value)
return parent::setAttribute($key, $value);
}

/**
* Get an attribute array of all arrayable attributes.
*
* @return array
*/
public function getArrayableAttributes()
{
$attributes = parent::getArrayableAttributes();
if ($this->isPropertiesRelationAccessible()) {
$query = $this->propertierQuery();

$visible = $this->getVisible();
$hidden = $this->getHidden();
/* @var Collection $property */
foreach ($this->properties as $property) {
if (in_array($property->name, $hidden, true) || (count($visible) > 0 && in_array($property->name, $visible, true))) {
continue;
}
$value = $query->getValue($property->name);
$attributes[$property->name] = $value instanceof Arrayable ? $value->toArray() : $value;
}
}

return $attributes;
}

/**
* Handling propertier method calls to the manager class.
*
Expand All @@ -180,4 +206,4 @@ public function __call($method, $parameters)

return parent::__call($method, $parameters);
}
}
}

0 comments on commit 9f0f394

Please sign in to comment.