Skip to content

Commit

Permalink
Merge pull request #60 from Evertt/get-attribute-change
Browse files Browse the repository at this point in the history
change __get method to also return mutators on non-existing attributes
  • Loading branch information
RemiCollin committed Sep 21, 2015
2 parents 0f3891c + 7cdac42 commit 9df7ea3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@ class Entity extends ValueObject implements Mappable, ArrayAccess, Jsonable, Jso
*/
public function __get($key)
{
if (! array_key_exists($key, $this->attributes))
{
return null;
}
if ($this->hasGetMutator($key))
{
$method = 'get'.$this->getMutatorMethod($key);

return $this->$method($this->attributes[$key]);
$attribute = null;

if (isset($this->attributes[$key]))
$attribute = $this->attributes[$key];

return $this->$method($attribute);
}
if (! array_key_exists($key, $this->attributes))
{
return null;
}
if ($this->attributes[$key] instanceof EntityProxy)
{
Expand Down
5 changes: 5 additions & 0 deletions tests/AnalogueTest/App/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public function __construct($name, User $user)
$this->name = $name;
$this->user = $user;
}

public function getPathAttribute()
{
return $this->image->path;
}
}
10 changes: 10 additions & 0 deletions tests/AnalogueTest/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ public function testSimpleEntityToArray()
$rm->store($role);
$role->toArray();
}

public function testGetMethodsForNonExistingAttributes()
{
$user = new User('John', new Role('Admin'));
$avatar = new Avatar('Picture', $user);
$image = new Image('my-picture.jpg');
$avatar->image = $image;

$this->assertEquals($avatar->path, 'my-picture.jpg');
}
}

0 comments on commit 9df7ea3

Please sign in to comment.