Skip to content

Commit 839c4c9

Browse files
committed
fix: handle UnitEnum in Entity::normalizeValue() for proper normalization
1 parent 5a24c0c commit 839c4c9

4 files changed

Lines changed: 47 additions & 7 deletions

File tree

system/Entity/Entity.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -458,22 +458,22 @@ private function normalizeValue(mixed $data): mixed
458458
// Check for Entity instance (use raw values, recursive)
459459
if ($data instanceof self) {
460460
$objectData = $data->toRawArray(false, true);
461+
} elseif ($data instanceof UnitEnum) {
462+
return [
463+
'__class' => $data::class,
464+
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
465+
];
461466
} elseif ($data instanceof JsonSerializable) {
462467
$objectData = $data->jsonSerialize();
463-
} elseif (method_exists($data, 'toArray')) {
464-
$objectData = $data->toArray();
465468
} elseif ($data instanceof Traversable) {
466469
$objectData = iterator_to_array($data);
467470
} elseif ($data instanceof DateTimeInterface) {
468471
return [
469472
'__class' => $data::class,
470473
'__datetime' => $data->format(DATE_RFC3339_EXTENDED),
471474
];
472-
} elseif ($data instanceof UnitEnum) {
473-
return [
474-
'__class' => $data::class,
475-
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
476-
];
475+
} elseif (method_exists($data, 'toArray')) {
476+
$objectData = $data->toArray();
477477
} else {
478478
$objectData = get_object_vars($data);
479479

tests/_support/Enum/StateEnum.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Enum;
15+
16+
enum StateEnum: string
17+
{
18+
case DRAFT = 'draft';
19+
case PUBLISHED = 'published';
20+
21+
public function toArray(): array
22+
{
23+
return self::cases();
24+
}
25+
}

tests/system/Entity/EntityTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Tests\Support\Entity\Cast\NotExtendsBaseCast;
3636
use Tests\Support\Enum\ColorEnum;
3737
use Tests\Support\Enum\RoleEnum;
38+
use Tests\Support\Enum\StateEnum;
3839
use Tests\Support\Enum\StatusEnum;
3940
use Tests\Support\SomeEntity;
4041

@@ -1045,6 +1046,19 @@ public function testCastEnumSetWithUnitEnumObject(): void
10451046
$this->assertSame(ColorEnum::RED, $entity->color);
10461047
}
10471048

1049+
/**
1050+
* @see https://github.com/codeigniter4/CodeIgniter4/issues/10136
1051+
*/
1052+
public function testInjectRawDataWithEnumThatHasToArrayMethod(): void
1053+
{
1054+
$entity = new class () extends Entity {};
1055+
1056+
$entity->injectRawData(['state' => StateEnum::DRAFT]);
1057+
1058+
$this->assertSame(StateEnum::DRAFT, $entity->toRawArray()['state']);
1059+
$this->assertFalse($entity->hasChanged('state'));
1060+
}
1061+
10481062
public function testAsArray(): void
10491063
{
10501064
$entity = $this->getEntity();

user_guide_src/source/changelogs/v4.7.3.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Bugs Fixed
4040
- **CLI:** Fixed a bug where ``CLI::generateDimensions()`` leaked ``stty`` error output (e.g., ``stty: 'standard input': Inappropriate ioctl for device``) to stderr when stdin was not a TTY.
4141
- **Commands:** Fixed a bug in the ``env`` command where passing options only would cause the command to throw a ``TypeError`` instead of showing the current environment.
4242
- **Common:** Fixed a bug where the ``command()`` helper function did not properly clean up output buffers, which could lead to risky tests when exceptions were thrown.
43+
- **Entity:** Fixed a bug where ``Entity::normalizeValue()`` did not handle ``UnitEnum`` before checking for ``toArray()``, causing enums implementing ``toArray()`` to be incorrectly normalized as generic objects instead of enums.
4344
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
4445
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.
4546

0 commit comments

Comments
 (0)