Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ private function normalizeValue(mixed $data): mixed
// Check for Entity instance (use raw values, recursive)
if ($data instanceof self) {
$objectData = $data->toRawArray(false, true);
} elseif ($data instanceof UnitEnum) {
return [
'__class' => $data::class,
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
];
} elseif ($data instanceof JsonSerializable) {
$objectData = $data->jsonSerialize();
} elseif (method_exists($data, 'toArray')) {
Expand All @@ -469,11 +474,6 @@ private function normalizeValue(mixed $data): mixed
'__class' => $data::class,
'__datetime' => $data->format(DATE_RFC3339_EXTENDED),
];
} elseif ($data instanceof UnitEnum) {
return [
'__class' => $data::class,
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
];
} else {
$objectData = get_object_vars($data);

Expand Down
29 changes: 29 additions & 0 deletions tests/_support/Enum/StateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace Tests\Support\Enum;

/**
* An enum that also defines toArray(), used to test that UnitEnum handling
* takes precedence over toArray() in Entity::normalizeValue().
*/
enum StateEnum: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';

public function toArray(): array
{
return array_column(self::cases(), 'value');
}
}
19 changes: 19 additions & 0 deletions tests/system/Entity/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Tests\Support\Entity\Cast\NotExtendsBaseCast;
use Tests\Support\Enum\ColorEnum;
use Tests\Support\Enum\RoleEnum;
use Tests\Support\Enum\StateEnum;
use Tests\Support\Enum\StatusEnum;
use Tests\Support\SomeEntity;

Expand Down Expand Up @@ -1045,6 +1046,24 @@ public function testCastEnumSetWithUnitEnumObject(): void
$this->assertSame(ColorEnum::RED, $entity->color);
}

public function testInjectRawDataWithEnumThatHasToArrayMethod(): void
{
// Regression test for https://github.com/codeigniter4/CodeIgniter4/issues/10136
// Enums implementing toArray() must still be handled by the UnitEnum branch in
// normalizeValue(), so hasChanged() does not incorrectly report a change after
// injectRawData() stores the same enum value.
$entity = new class () extends Entity {};

$entity->injectRawData(['state' => StateEnum::DRAFT]);

// toRawArray() returns raw attributes, so the enum object is returned as-is.
$this->assertSame(StateEnum::DRAFT, $entity->toRawArray()['state']);

// The key assertion: normalizeValue() must treat the enum as a UnitEnum
// (not call toArray() on it), so the original and current normalized forms match.
$this->assertFalse($entity->hasChanged('state'));
}

public function testAsArray(): void
{
$entity = $this->getEntity();
Expand Down
Loading