Skip to content

Commit cf05adc

Browse files
PhoneixSnicolas-grekas
authored andcommitted
[Validator] Fix support of Enum to ConstraintValidator::formatValue
1 parent 8700f0d commit cf05adc

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

ConstraintValidator.php

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

102+
if ($value instanceof \UnitEnum) {
103+
return $value->name;
104+
}
105+
102106
if (\is_object($value)) {
103107
if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
104108
return $value->__toString();

Tests/ConstraintValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static function formatValueProvider()
4141
['array', []],
4242
['object', $toString = new TestToStringObject()],
4343
['ccc', $toString, ConstraintValidator::OBJECT_TO_STRING],
44+
['FirstCase', $toString = TestEnum::FirstCase],
4445
['object', $dateTime = new \DateTimeImmutable('1971-02-02T08:00:00UTC')],
4546
[class_exists(\IntlDateFormatter::class) ? 'Oct 4, 2019, 11:02 AM' : '2019-10-04 11:02:03', new \DateTimeImmutable('2019-10-04T11:02:03+09:00'), ConstraintValidator::PRETTY_DATE],
4647
[class_exists(\IntlDateFormatter::class) ? 'Feb 2, 1971, 8:00 AM' : '1971-02-02 08:00:00', $dateTime, ConstraintValidator::PRETTY_DATE],
@@ -73,3 +74,9 @@ public function __toString()
7374
return 'ccc';
7475
}
7576
}
77+
78+
enum TestEnum
79+
{
80+
case FirstCase;
81+
case SecondCase;
82+
}

0 commit comments

Comments
 (0)