Skip to content

Commit f7b56b8

Browse files
ro0NLfabpot
authored andcommitted
[Intl][Validator] Handle alias locales/timezones
1 parent 80b4019 commit f7b56b8

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

Constraints/LocaleValidator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14-
use Symfony\Component\Intl\Intl;
1514
use Symfony\Component\Intl\Locales;
1615
use Symfony\Component\Validator\Constraint;
1716
use Symfony\Component\Validator\ConstraintValidator;
17+
use Symfony\Component\Validator\Exception\LogicException;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1919
use Symfony\Component\Validator\Exception\UnexpectedValueException;
2020

@@ -42,13 +42,17 @@ public function validate($value, Constraint $constraint)
4242
throw new UnexpectedValueException($value, 'string');
4343
}
4444

45+
if (!class_exists(Locales::class)) {
46+
throw new LogicException('The "symfony/intl" component is required to use the Locale constraint.');
47+
}
48+
4549
$inputValue = (string) $value;
4650
$value = $inputValue;
4751
if ($constraint->canonicalize) {
4852
$value = \Locale::canonicalize($value);
4953
}
5054

51-
if (!Locales::exists($value) && !\in_array($value, Locales::getAliases(), true)) {
55+
if (!Locales::exists($value)) {
5256
$this->context->buildViolation($constraint->message)
5357
->setParameter('{{ value }}', $this->formatValue($inputValue))
5458
->setCode(Locale::NO_SUCH_LOCALE_ERROR)

Constraints/TimezoneValidator.php

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,10 @@ public function validate($value, Constraint $constraint)
5454
return;
5555
}
5656

57-
if ($constraint->countryCode) {
58-
$phpTimezoneIds = @\DateTimeZone::listIdentifiers($constraint->zone, $constraint->countryCode) ?: [];
59-
try {
60-
$intlTimezoneIds = Timezones::forCountryCode($constraint->countryCode);
61-
} catch (MissingResourceException $e) {
62-
$intlTimezoneIds = [];
63-
}
64-
} else {
65-
$phpTimezoneIds = \DateTimeZone::listIdentifiers($constraint->zone);
66-
$intlTimezoneIds = self::getIntlTimezones($constraint->zone);
67-
}
68-
69-
if (\in_array($value, $phpTimezoneIds, true) || \in_array($value, $intlTimezoneIds, true)) {
57+
if (
58+
\in_array($value, self::getPhpTimezones($constraint->zone, $constraint->countryCode), true) ||
59+
\in_array($value, self::getIntlTimezones($constraint->zone, $constraint->countryCode), true)
60+
) {
7061
return;
7162
}
7263

@@ -106,8 +97,29 @@ protected function formatValue($value, $format = 0)
10697
return array_search($value, (new \ReflectionClass(\DateTimeZone::class))->getConstants(), true) ?: $value;
10798
}
10899

109-
private static function getIntlTimezones(int $zone): array
100+
private static function getPhpTimezones(int $zone, string $countryCode = null): array
110101
{
102+
if (null !== $countryCode) {
103+
return @\DateTimeZone::listIdentifiers($zone, $countryCode) ?: [];
104+
}
105+
106+
return \DateTimeZone::listIdentifiers($zone);
107+
}
108+
109+
private static function getIntlTimezones(int $zone, string $countryCode = null): array
110+
{
111+
if (!class_exists(Timezones::class)) {
112+
return [];
113+
}
114+
115+
if (null !== $countryCode) {
116+
try {
117+
return Timezones::forCountryCode($countryCode);
118+
} catch (MissingResourceException $e) {
119+
return [];
120+
}
121+
}
122+
111123
$timezones = Timezones::getIds();
112124

113125
if (\DateTimeZone::ALL === (\DateTimeZone::ALL & $zone)) {

Tests/Constraints/LocaleValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ public function getValidLocales()
111111
['pt', ['canonicalize' => true]],
112112
['pt_PT', ['canonicalize' => true]],
113113
['zh_Hans', ['canonicalize' => true]],
114-
['fil_PH', ['canonicalize' => true]],
114+
['tl_PH', ['canonicalize' => true]],
115+
['fil_PH', ['canonicalize' => true]], // alias for "tl_PH"
115116
];
116117
}
117118

0 commit comments

Comments
 (0)