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;
|
$this->_cast = $cast;
|
||||||
$return = [];
|
$return = [];
|
||||||
|
|
||||||
// we need to loop over our properties so that we
|
$keys = array_keys($this->attributes);
|
||||||
// allow our magic methods a chance to do their thing.
|
$keys = array_filter($keys, function ($key) {
|
||||||
foreach ($this->attributes as $key => $value)
|
return strpos($key, '_') !== 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (is_array($this->datamap))
|
||||||
{
|
{
|
||||||
if (strpos($key, '_') === 0)
|
$keys = array_diff($keys, $this->datamap);
|
||||||
{
|
$keys = array_unique(array_merge($keys, array_keys($this->datamap)));
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// we need to loop over our properties so that we
|
||||||
|
// allow our magic methods a chance to do their thing.
|
||||||
|
foreach ($keys as $key)
|
||||||
|
{
|
||||||
if ($onlyChanged && ! $this->hasChanged($key))
|
if ($onlyChanged && ! $this->hasChanged($key))
|
||||||
{
|
{
|
||||||
continue;
|
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;
|
$this->_cast = true;
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
@ -631,7 +631,6 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
|||||||
'foo' => null,
|
'foo' => null,
|
||||||
'bar' => ':bar',
|
'bar' => ':bar',
|
||||||
'default' => 'sumfin',
|
'default' => 'sumfin',
|
||||||
'created_at' => null,
|
|
||||||
'createdAt' => null,
|
'createdAt' => null,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@ -647,13 +646,11 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
|||||||
'foo' => null,
|
'foo' => null,
|
||||||
'bar' => ':bar',
|
'bar' => ':bar',
|
||||||
'default' => 'sumfin',
|
'default' => 'sumfin',
|
||||||
'created_at' => null,
|
|
||||||
'createdAt' => null,
|
'createdAt' => null,
|
||||||
'entity' => [
|
'entity' => [
|
||||||
'foo' => null,
|
'foo' => null,
|
||||||
'bar' => ':bar',
|
'bar' => ':bar',
|
||||||
'default' => 'sumfin',
|
'default' => 'sumfin',
|
||||||
'created_at' => null,
|
|
||||||
'createdAt' => null,
|
'createdAt' => null,
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
@ -666,13 +663,24 @@ class EntityTest extends \CodeIgniter\Test\CIUnitTestCase
|
|||||||
$result = $entity->toArray();
|
$result = $entity->toArray();
|
||||||
|
|
||||||
$this->assertEquals($result, [
|
$this->assertEquals($result, [
|
||||||
'foo' => null,
|
|
||||||
'simple' => ':oo',
|
|
||||||
'bar' => null,
|
'bar' => null,
|
||||||
'orig' => ':oo',
|
'orig' => ':oo',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testAsArraySwapped()
|
||||||
|
{
|
||||||
|
$entity = $this->getSwappedEntity();
|
||||||
|
|
||||||
|
$result = $entity->toArray();
|
||||||
|
|
||||||
|
$this->assertEquals($result, [
|
||||||
|
'bar' => 'foo',
|
||||||
|
'foo' => 'bar',
|
||||||
|
'original_bar' => 'bar',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
public function testToArraySkipAttributesWithUnderscoreInFirstCharacter()
|
public function testToArraySkipAttributesWithUnderscoreInFirstCharacter()
|
||||||
{
|
{
|
||||||
$entity = new class extends Entity
|
$entity = new class extends Entity
|
||||||
@ -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
|
protected function getCastEntity($data = null) : Entity
|
||||||
{
|
{
|
||||||
return new class($data) extends Entity
|
return new class($data) extends Entity
|
||||||
|
Loading…
x
Reference in New Issue
Block a user