Skip to content

Commit

Permalink
Merge pull request codeigniter4#3508 from najdanovicivan/entity-asArr…
Browse files Browse the repository at this point in the history
…ay-cast

Codeigniter\Entity - Fix ToArray datamap
  • Loading branch information
MGatner authored Aug 25, 2020
2 parents 8ed399d + 7f5357c commit dfbab73
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 50 deletions.
42 changes: 12 additions & 30 deletions system/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
70 changes: 50 additions & 20 deletions tests/system/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}

Expand All @@ -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,
],
]);
}
Expand All @@ -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',
]);
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dfbab73

Please sign in to comment.