Skip to content

fix(doctrine): support integer-backed enums in BackedEnumFilter #7127

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

Open
wants to merge 1 commit into
base: 4.1
Choose a base branch
from
Open
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: 9 additions & 1 deletion src/Doctrine/Common/Filter/BackedEnumFilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait BackedEnumFilterTrait
use PropertyHelperTrait;

/**
* @var array<string, string>
* @var array<string, class-string>
Copy link
Author

Choose a reason for hiding this comment

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

I believe class-string to be more appropriate here, I can revert this if needed.

*/
private array $enumTypes;

Expand Down Expand Up @@ -80,6 +80,14 @@ abstract protected function isBackedEnumField(string $property, string $resource

private function normalizeValue($value, string $property): mixed
{
$firstCase = $this->enumTypes[$property]::cases()[0] ?? null;
if (
\is_int($firstCase?->value)
&& false !== filter_var($value, \FILTER_VALIDATE_INT)
) {
$value = (int) $value;
}

$values = array_map(fn (\BackedEnum $case) => $case->value, $this->enumTypes[$property]::cases());

if (\in_array($value, $values, true)) {
Expand Down
Loading