mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Merge pull request #3508 from najdanovicivan/entity-asArray-cast
Codeigniter\Entity - Fix ToArray datamap
This commit is contained in:
commit
dfbab7363d
@ -150,15 +150,21 @@ class Entity implements \JsonSerializable
|
||||
$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 @@ class Entity implements \JsonSerializable
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
@ -628,11 +628,10 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
||||
$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 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
||||
$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 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
||||
$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 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
||||
};
|
||||
}
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user