diff --git a/system/Entity.php b/system/Entity.php index f8fa6d010da7..6a588e715602 100644 --- a/system/Entity.php +++ b/system/Entity.php @@ -150,15 +150,21 @@ public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recu $this->_cast = $cast; $return = []; + $keys = array_keys($this->attributes); + $keys = array_filter($keys, function ($key) { + return strpos($key, '_') !== 0; + }); + + if (is_array($this->datamap)) + { + $keys = array_diff($keys, $this->datamap); + $keys = array_unique(array_merge($keys, array_keys($this->datamap))); + } + // we need to loop over our properties so that we // allow our magic methods a chance to do their thing. - foreach ($this->attributes as $key => $value) + foreach ($keys as $key) { - if (strpos($key, '_') === 0) - { - continue; - } - if ($onlyChanged && ! $this->hasChanged($key)) { continue; @@ -179,30 +185,6 @@ public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recu } } - // Loop over our mapped properties and add them to the list... - if (is_array($this->datamap)) - { - foreach ($this->datamap as $from => $to) - { - if (array_key_exists($to, $return)) - { - $return[$from] = $this->__get($to); - - if ($recursive) - { - if ($return[$from] instanceof Entity) - { - $return[$from] = $return[$from]->toArray($onlyChanged, $cast, $recursive); - } - elseif (is_callable([$return[$from], 'toArray'])) - { - $return[$from] = $return[$from]->toArray(); - } - } - } - } - } - $this->_cast = true; return $return; } diff --git a/tests/system/EntityTest.php b/tests/system/EntityTest.php index 9c2928943bbc..c47d3d7b329e 100644 --- a/tests/system/EntityTest.php +++ b/tests/system/EntityTest.php @@ -628,11 +628,10 @@ public function testAsArray() $result = $entity->toArray(); $this->assertEquals($result, [ - 'foo' => null, - 'bar' => ':bar', - 'default' => 'sumfin', - 'created_at' => null, - 'createdAt' => null, + 'foo' => null, + 'bar' => ':bar', + 'default' => 'sumfin', + 'createdAt' => null, ]); } @@ -644,17 +643,15 @@ public function testAsArrayRecursive() $result = $entity->toArray(false, true, true); $this->assertEquals($result, [ - 'foo' => null, - 'bar' => ':bar', - 'default' => 'sumfin', - 'created_at' => null, - 'createdAt' => null, - 'entity' => [ - 'foo' => null, - 'bar' => ':bar', - 'default' => 'sumfin', - 'created_at' => null, - 'createdAt' => null, + 'foo' => null, + 'bar' => ':bar', + 'default' => 'sumfin', + 'createdAt' => null, + 'entity' => [ + 'foo' => null, + 'bar' => ':bar', + 'default' => 'sumfin', + 'createdAt' => null, ], ]); } @@ -666,10 +663,21 @@ public function testAsArrayMapped() $result = $entity->toArray(); $this->assertEquals($result, [ - 'foo' => null, - 'simple' => ':oo', - 'bar' => null, - 'orig' => ':oo', + 'bar' => null, + 'orig' => ':oo', + ]); + } + + public function testAsArraySwapped() + { + $entity = $this->getSwappedEntity(); + + $result = $entity->toArray(); + + $this->assertEquals($result, [ + 'bar' => 'foo', + 'foo' => 'bar', + 'original_bar' => 'bar', ]); } @@ -916,6 +924,28 @@ protected function getSimple() }; } + protected function getSwappedEntity() : Entity + { + return new class extends Entity + { + protected $attributes = [ + 'foo' => 'foo', + 'bar' => 'bar', + ]; + + protected $_original = [ + 'foo' => 'foo', + 'bar' => 'bar', + ]; + + protected $datamap = [ + 'bar' => 'foo', + 'foo' => 'bar', + 'original_bar' => 'bar', + ]; + }; + } + protected function getCastEntity($data = null) : Entity { return new class($data) extends Entity