Skip to content

Commit 473e8fd

Browse files
ro0NLfabpot
authored andcommitted
[Intl][Validator] Handle alias locales/timezones
1 parent 4827499 commit 473e8fd

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

Locales.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function exists(string $locale): bool
4444

4545
return true;
4646
} catch (MissingResourceException $e) {
47-
return false;
47+
return \in_array($locale, self::getAliases(), true);
4848
}
4949
}
5050

@@ -53,7 +53,15 @@ public static function exists(string $locale): bool
5353
*/
5454
public static function getName(string $locale, string $displayLocale = null): string
5555
{
56-
return self::readEntry(['Names', $locale], $displayLocale);
56+
try {
57+
return self::readEntry(['Names', $locale], $displayLocale);
58+
} catch (MissingResourceException $e) {
59+
if (false === $aliased = array_search($locale, self::getAliases(), true)) {
60+
throw $e;
61+
}
62+
63+
return self::readEntry(['Names', $aliased], $displayLocale);
64+
}
5765
}
5866

5967
/**

Tests/LocalesTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,16 @@ public function testGetNameWithInvalidLocale()
9393
Locales::getName('foo');
9494
}
9595

96+
public function testGetNameWithAliasLocale()
97+
{
98+
$this->assertSame(Locales::getName('tl_PH'), Locales::getName('fil_PH'));
99+
}
100+
96101
public function testExists()
97102
{
98103
$this->assertTrue(Locales::exists('nl_NL'));
104+
$this->assertTrue(Locales::exists('tl_PH'));
105+
$this->assertTrue(Locales::exists('fil_PH')); // alias for "tl_PH"
99106
$this->assertFalse(Locales::exists('zxx_ZZ'));
100107
}
101108
}

Tests/TimezonesTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,14 +530,23 @@ public function testGetNameDefaultLocale()
530530
/**
531531
* @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
532532
*/
533-
public function testGetNameWithInvalidTimezoneId()
533+
public function testGetNameWithInvalidTimezone()
534534
{
535535
Timezones::getName('foo');
536536
}
537537

538+
/**
539+
* @expectedException \Symfony\Component\Intl\Exception\MissingResourceException
540+
*/
541+
public function testGetNameWithAliasTimezone()
542+
{
543+
Timezones::getName('US/Pacific'); // alias in icu (not compiled), name unavailable in php
544+
}
545+
538546
public function testExists()
539547
{
540548
$this->assertTrue(Timezones::exists('Europe/Amsterdam'));
549+
$this->assertTrue(Timezones::exists('US/Pacific')); // alias in icu (not compiled), identifier available in php
541550
$this->assertFalse(Timezones::exists('Etc/Unknown'));
542551
}
543552

@@ -547,6 +556,9 @@ public function testGetRawOffset()
547556
$this->assertSame(0, Timezones::getRawOffset('Etc/UTC'));
548557
$this->assertSame(-10800, Timezones::getRawOffset('America/Buenos_Aires'));
549558
$this->assertSame(20700, Timezones::getRawOffset('Asia/Katmandu'));
559+
560+
// ensure we support identifiers available in php (not compiled from icu)
561+
Timezones::getRawOffset('US/Pacific');
550562
}
551563

552564
/**

Timezones.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ public static function exists(string $timezone): bool
3636

3737
return true;
3838
} catch (MissingResourceException $e) {
39-
return false;
39+
try {
40+
new \DateTimeZone($timezone);
41+
42+
return true;
43+
} catch (\Exception $e) {
44+
return false;
45+
}
4046
}
4147
}
4248

4349
/**
44-
* @throws MissingResourceException if the timezone identifier does not exists
50+
* @throws MissingResourceException if the timezone identifier does not exist or is an alias
4551
*/
4652
public static function getName(string $timezone, string $displayLocale = null): string
4753
{
@@ -57,7 +63,7 @@ public static function getNames(string $displayLocale = null): array
5763
}
5864

5965
/**
60-
* @throws \Exception if the timezone identifier does not exists
66+
* @throws \Exception if the timezone identifier does not exist
6167
* @throws RuntimeException if there's no timezone DST transition information available
6268
*/
6369
public static function getRawOffset(string $timezone, int $timestamp = null): int
@@ -92,7 +98,7 @@ public static function getCountryCode(string $timezone): string
9298
}
9399

94100
/**
95-
* @throws MissingResourceException if the country code does not exists
101+
* @throws MissingResourceException if the country code does not exist
96102
*/
97103
public static function forCountryCode(string $country): array
98104
{

0 commit comments

Comments
 (0)