8000 [Validator] Supports backed enum in constraint groups by alamirault · Pull Request #45570 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Supports backed enum in constraint groups #45570

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

Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate `Constraint::$errorNames`, use `Constraint::ERROR_NAMES` instead
* Supports PHP backed enumerations in constraint groups

6.0
---
Expand Down
30 changes: 24 additions & 6 deletions src/Symfony/Component/Validator/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ public static function getErrorName(string $errorCode): string
* getRequiredOptions() to return the names of these options. If any
* option is not set here, an exception is thrown.
*
* @param mixed $options The options (as associative array)
* or the value for the default
* option (any other type)
* @param string[] $groups An array of validation groups
* @param mixed $payload Domain-specific data attached to a constraint
* @param mixed $options The options (as associative array)
* or the value for the default
* option (any other type)
* @param array<string|\BackedEnum> $groups An array of validation groups
* @param mixed $payload Domain-specific data attached to a constraint
*
* @throws InvalidOptionsException When you pass the names of non-existing
* options
Expand Down Expand Up @@ -197,14 +197,32 @@ protected function normalizeOptions(mixed $options): array
public function __set(string $option, mixed $value)
{
if ('groups' === $option) {
$this->groups = (array) $value;
$this->groups = $this->normalizeGroups((array) $value);
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather turn this into $this->groups = array_map('strval', (array) $value); and see php/php-src#8825 implemented in PHP 8.2


return;
}

throw new InvalidOptionsException(sprintf('The option "%s" does not exist in constraint "%s".', $option, static::class), [$option]);
}

/**
* @param array<string|\BackedEnum> $groups
*
* @return array<string>
*/
protected function normalizeGroups(array $groups): array
{
$normalized = [];
foreach ($groups as $group) {
if ($group instanceof \BackedEnum) {
$group = $group->value;
}
$normalized[] = $group;
}

return $normalized;
}

/**
* Returns the value of a lazily initialized option.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Validator/Tests/ConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Exception\MissingOptionsException;
use Symfony\Component\Validator\Tests\Fixtures\BackedEnumA;
use Symfony\Component\Validator\Tests\Fixtures\ClassConstraint;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA;
use Symfony\Component\Validator\Tests\Fixtures\ConstraintB;
Expand Down Expand Up @@ -133,6 +134,20 @@ public function testGroupsAreConvertedToArray()
$this->assertEquals(['Foo'], $constraint->groups);
}

public function testGroupsBackedEnumsAreConvertedToValue()
{
$constraint = new ConstraintA([], ['foo', BackedEnumA::READ]);

$this->assertSame(['foo', 'read'], $constraint->groups);
}

public function testOptionGroupsBackedEnumsAreConvertedToValue()
{
$constraint = new ConstraintA(['groups' => ['foo', BackedEnumA::READ]]);

$this->assertSame(['foo', 'read'], $constraint->groups);
}

public function testAddDefaultGroupAddsGroup()
{
$constraint = new ConstraintA(['groups' => 'Default']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Validator\Tests\Fixtures\BackedEnumA;

class GreaterThanTest extends TestCase
{
Expand All @@ -38,6 +39,11 @@ public function testAttributes()
self::assertSame('b', $cConstraint->propertyPath);
self::assertSame('myMessage', $cConstraint->message);
self::assertSame(['foo'], $cConstraint->groups);

[$dConstraint] = $metadata->properties['d']->getConstraints();
self::assertSame(4711, $dConstraint->value);
self::assertNull($dConstraint->propertyPath);
self::assertSame(['read', 'write'], $dConstraint->groups);
}
}

Expand All @@ -51,4 +57,7 @@ class GreaterThanDummy

#[GreaterThan(propertyPath: 'b', message: 'myMessage', groups: ['foo'])]
private $c;

#[GreaterThan(value: 4711, groups: [BackedEnumA::READ, 'write'])]
private $d;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\Validator\Tests\Fixtures;

enum BackedEnumA: string
{
case READ = 'read';
}
0