Skip to content
Open
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
14 changes: 7 additions & 7 deletions system/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,22 @@ 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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since ordering has changed, it might be worthwhile to add in the phpdoc the order of resolution?

return [
'__class' => $data::class,
'__enum' => $data instanceof BackedEnum ? $data->value : $data->name,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness, can we additionally test a UnitEnum with toArray()? Currently, it's just a backed enum with toArray().

];
} elseif ($data instanceof JsonSerializable) {
$objectData = $data->jsonSerialize();
} elseif (method_exists($data, 'toArray')) {
$objectData = $data->toArray();
} elseif ($data instanceof Traversable) {
$objectData = iterator_to_array($data);
} elseif ($data instanceof DateTimeInterface) {
return [
'__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,
];
} elseif (method_exists($data, 'toArray')) {
$objectData = $data->toArray();
Comment on lines +475 to +476
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change creates a change in behavior for classes implementing both toArray() and either Traversable or DateTimeInterface. Previously, toArray() won. But now, this changes the picture. If this change is desirable, we should document this in the changelog as well. A couple of tests locking this new behavior would be nice, too.

} else {
$objectData = get_object_vars($data);

Expand Down
25 changes: 25 additions & 0 deletions tests/_support/Enum/StateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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;

enum StateEnum: string
{
case DRAFT = 'draft';
case PUBLISHED = 'published';

public function toArray(): array
{
return self::cases();
}
Comment thread
maniaba marked this conversation as resolved.
}
14 changes: 14 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,19 @@ public function testCastEnumSetWithUnitEnumObject(): void
$this->assertSame(ColorEnum::RED, $entity->color);
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/10136
*/
public function testInjectRawDataWithEnumThatHasToArrayMethod(): void
{
$entity = new class () extends Entity {};

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

$this->assertSame(StateEnum::DRAFT, $entity->toRawArray()['state']);
$this->assertFalse($entity->hasChanged('state'));
}
Comment thread
maniaba marked this conversation as resolved.

public function testAsArray(): void
{
$entity = $this->getEntity();
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Bugs Fixed
- **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.
- **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.
- **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.
- **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.
- **Kint:** Fixed a bug where stale Content Security Policy nonces were reused in worker mode, causing browser CSP violations for Debug Toolbar assets.
- **Validation:** Fixed a bug where ``Validation::getValidated()`` dropped fields whose validated value was explicitly ``null``.

Expand Down
Loading