Skip to content

Commit c02ea86

Browse files
Merge branch '5.4' into 6.2
* 5.4: [Mime] regenerate test certificates Fix tests [Validator] Fix support of Enum to `ConstraintValidator::formatValue`
2 parents 5a37e79 + ca71f55 commit c02ea86

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

ConstraintValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ protected function formatValue(mixed $value, int $format = 0): string
8888
return $value->format('Y-m-d H:i:s');
8989
}
9090

91+
if ($value instanceof \UnitEnum) {
92+
return $value->name;
93+
}
94+
9195
if (\is_object($value)) {
9296
if (($format & self::OBJECT_TO_STRING) && $value instanceof \Stringable) {
9397
return $value->__toString();

Tests/ConstraintValidatorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
17+
use Symfony\Component\Validator\Tests\Fixtures\TestEnum;
1718

1819
class ConstraintValidatorTest extends TestCase
1920
{
@@ -48,6 +49,10 @@ public static function formatValueProvider()
4849
[class_exists(\IntlDateFormatter::class) ? 'Jan 1, 1970, 3:00 PM' : '1970-01-01 15:00:00', (new \DateTimeImmutable('1970-01-01T23:00:00'))->setTimezone(new \DateTimeZone('America/New_York')), ConstraintValidator::PRETTY_DATE],
4950
];
5051

52+
if (\PHP_VERSION_ID >= 80100) {
53+
$data[] = ['FirstCase', TestEnum::FirstCase];
54+
}
55+
5156
date_default_timezone_set($defaultTimezone);
5257

5358
return $data;

Tests/Fixtures/TestEnum.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Validator\Tests\Fixtures;
13+
14+
enum TestEnum
15+
{
16+
case FirstCase;
17+
case SecondCase;
18+
}

0 commit comments

Comments
 (0)