diff --git a/src/Validator/Constraints/TimezoneValidator.php b/src/Validator/Constraints/TimezoneValidator.php index d047a99..6c913c3 100644 --- a/src/Validator/Constraints/TimezoneValidator.php +++ b/src/Validator/Constraints/TimezoneValidator.php @@ -30,7 +30,8 @@ public function validate(mixed $value, Constraint $constraint): void $value = $this->getValue($value); - if (!in_array($value, \DateTimeZone::listIdentifiers(), true)) { + // Mirror \DateTimeZone, which also accepts backward-compatible aliases (US/Hawaii). + if (!in_array($value, \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC), true)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) ->setCode(Timezone::NO_SUCH_TIMEZONE_ERROR) diff --git a/tests/Validator/Constraints/TimezoneValidatorTest.php b/tests/Validator/Constraints/TimezoneValidatorTest.php index 7c023be..a9c6eaf 100644 --- a/tests/Validator/Constraints/TimezoneValidatorTest.php +++ b/tests/Validator/Constraints/TimezoneValidatorTest.php @@ -32,6 +32,21 @@ public function testExpectsStringCompatibleType(): void $this->validator->validate(new \stdClass(), new Timezone()); } + public function testAcceptsBackwardCompatibleAlias(): void + { + $aliases = array_diff( + \DateTimeZone::listIdentifiers(\DateTimeZone::ALL_WITH_BC), + \DateTimeZone::listIdentifiers(), + ); + if ([] === $aliases) { + self::markTestSkipped('The timezone database of this platform has no backward-compatible alias.'); + } + + $this->validator->validate(reset($aliases), new Timezone()); + + $this->assertNoViolation(); + } + public static function providerValidValues(): iterable { yield [null]; @@ -48,5 +63,10 @@ public static function providerInvalidValues(): iterable Timezone::NO_SUCH_TIMEZONE_ERROR, 'The value {{ value }} is not a valid timezone.', ]; + yield [ + 'US/Atlantis', + Timezone::NO_SUCH_TIMEZONE_ERROR, + 'The value {{ value }} is not a valid timezone.', + ]; } }