@@ -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 )) {
0 commit comments