Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Entity.php __isset method #2112

Merged
merged 4 commits into from
Aug 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion system/Entity.php
Original file line number Diff line number Diff line change
@@ -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;
}
@@ -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]);
}

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

@@ -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
@@ -691,6 +703,10 @@ protected function getEntity()
'created_at' => null,
];

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

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

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