-
Notifications
You must be signed in to change notification settings - Fork 2k
fix: Entity::normalizeValue() must handle UnitEnum before toArray()
#10137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
| return [ | ||
| '__class' => $data::class, | ||
| '__enum' => $data instanceof BackedEnum ? $data->value : $data->name, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change creates a change in behavior for classes implementing both |
||
| } else { | ||
| $objectData = get_object_vars($data); | ||
|
|
||
|
|
||
| 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(); | ||
| } | ||
|
maniaba marked this conversation as resolved.
|
||
| } | ||
There was a problem hiding this comment.
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?