diff --git a/system/Entity.php b/system/Entity.php index 85b2ad84946b..fdc9f9dd43bf 100644 --- a/system/Entity.php +++ b/system/Entity.php @@ -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]); } diff --git a/tests/system/EntityTest.php b/tests/system/EntityTest.php index 61cb74068133..ac6fe65e1db3 100644 --- a/tests/system/EntityTest.php +++ b/tests/system/EntityTest.php @@ -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"; + } }; }