Skip to content

Commit 7de6017

Browse files
maniabamichalsn
authored andcommitted
fix: handle UnitEnum in Entity::normalizeValue() for proper normalization
1 parent 6a1543a commit 7de6017

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 <admin@codeigniter.com>
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
@@ -45,6 +45,7 @@ Bugs Fixed
4545
- **Database:** Fixed a bug where ``BaseConnection::listTables()`` could return a sparse array when using cached table names after a table was dropped.
4646
- **Database:** Fixed a bug where the PostgreSQL driver's ``increment()`` and ``decrement()`` methods were not working for numeric columns.
4747
- **Database:** Fixed a bug where the SQLSRV driver's decrement method was adding instead of subtracting the decrement value when ``$castTextToInt`` was false.
48+
- **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.
4849
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
4950
- **Language:** Fixed a bug where ``Language::getLine()`` returned the literal dot-notation key instead of the nested array value when the requested key resolved to an intermediate array three or more levels deep.
5051
- **Toolbar:** Fixed a bug where the Logs collector raised an undefined property error when using a third-party PSR-3 logger.

0 commit comments

Comments
 (0)