Skip to content

Commit

Permalink
Merge pull request #2112 from vibbow/fix_entity_isset
Browse files Browse the repository at this point in the history
Update `Entity.php` `__isset` method
  • Loading branch information
lonnieezell authored Aug 28, 2019
2 parents 30dbf66 + 30653f5 commit b9cef44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
14 changes: 13 additions & 1 deletion system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,12 @@ public function toArray(bool $onlyChanged = false, bool $cast = true): array
{
foreach ($this->datamap as $from => $to)
{
$return[$from] = $this->__get($to);
if (array_key_exists($to, $return)) {
$return[$from] = $this->__get($to);
}
}
}

$this->_cast = true;
return $return;
}
Expand Down Expand Up @@ -424,6 +427,15 @@ public function __unset(string $key)
*/
public function __isset(string $key): bool
{
$key = $this->mapProperty($key);

$method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $key)));

if (method_exists($this, $method))
{
return true;
}

return isset($this->attributes[$key]);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/system/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ public function testAsArray()
'bar' => ':bar',
'default' => 'sumfin',
'created_at' => null,
'createdAt' => null,
]);
}

Expand Down Expand Up @@ -673,6 +674,17 @@ public function testHasChangedWholeEntity()
$this->assertTrue($entity->hasChanged());
}

public function testIssetKeyMap()
{
$entity = $this->getEntity();

$entity->created_at = '12345678';
$this->assertTrue(isset($entity->createdAt));

$entity->bar = 'foo';
$this->assertTrue(isset($entity->FakeBar));
}

protected function getEntity()
{
return new class extends Entity
Expand All @@ -691,6 +703,10 @@ protected function getEntity()
'created_at' => null,
];

protected $datamap = [
'createdAt' => 'created_at',
];

public function setBar($value)
{
$this->attributes['bar'] = "bar:{$value}";
Expand All @@ -703,6 +719,10 @@ public function getBar()
return "{$this->attributes['bar']}:bar";
}

public function getFakeBar()
{
return "{$this->attributes['bar']}:bar";
}
};
}

Expand Down

0 comments on commit b9cef44

Please sign in to comment.