Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 848f60e

Browse files
Support ISO 3166-1 Alpha-3 country codes
1 parent 3498259 commit 848f60e

File tree

9 files changed

+1262
-9
lines changed

9 files changed

+1262
-9
lines changed

src/Symfony/Component/Intl/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* excluded language code `root`
8+
* added to both `Countries` and `Languages` the methods `getAlpha3Codes`, `getAlpha3Code`, `getAlpha2Code`, `alpha3CodeExists`, `getAlpha3Name` and `getAlpha3Names`
89

910
4.3.0
1011
-----

src/Symfony/Component/Intl/Countries.php

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,44 @@ public static function getCountryCodes(): array
3939
}
4040

4141
/**
42-
* @param string $country Alpha2 country code
42+
* Returns all available countries (3 letters).
43+
*
44+
* Countries are returned as uppercase ISO 3166 three-letter country codes.
45+
*
46+
* This list only contains "officially assigned ISO 3166-1 alpha-3" country codes.
47+
*
48+
* @return string[] an array of canonical ISO 3166 alpha-3 country codes
4349
*/
44-
public static function exists(string $country): bool
50+
public static function getAlpha3Codes(): array
51+
{
52+
return self::readEntry(['Alpha2ToAlpha3'], 'meta');
53+
}
54+
55+
public static function getAlpha3Code(string $alpha2Code): string
56+
{
57+
return self::readEntry(['Alpha2ToAlpha3', $alpha2Code], 'meta');
58+
}
59+
60+
public static function getAlpha2Code(string $alpha3Code): string
61+
{
62+
return self::readEntry(['Alpha3ToAlpha2', $alpha3Code], 'meta');
63+
}
64+
65+
public static function exists(string $alpha2Code): bool
4566
{
4667
try {
47-
self::readEntry(['Names', $country]);
68+
self::readEntry(['Names', $alpha2Code]);
69+
70+
return true;
71+
} catch (MissingResourceException $e) {
72+
return false;
73+
}
74+
}
75+
76+
public static function alpha3CodeExists(string $alpha3Code): bool
77+
{
78+
try {
79+
self::getAlpha2Code($alpha3Code);
4880

4981
return true;
5082
} catch (MissingResourceException $e) {
@@ -53,15 +85,25 @@ public static function exists(string $country): bool
5385
}
5486

5587
/**
56-
* Gets the country name from alpha2 code.
88+
* Gets the country name from its alpha2 code.
5789
*
58-
* @throws MissingResourceException if the country code does not exists
90+
* @throws MissingResourceException if the country code does not exist
5991
*/
6092
public static function getName(string $country, string $displayLocale = null): string
6193
{
6294
return self::readEntry(['Names', $country], $displayLocale);
6395
}
6496

97+
/**
98+
* Gets the country name from its alpha3 code.
99+
*
100+
* @throws MissingResourceException if the country code does not exist
101+
*/
102+
public static function getAlpha3Name(string $alpha3Code, string $displayLocale = null): string
103+
{
104+
return self::getName(self::getAlpha2Code($alpha3Code), $displayLocale);
105+
}
106+
65107
/**
66108
* Gets the list of country names indexed with alpha2 codes as keys.
67109
*
@@ -72,6 +114,24 @@ public static function getNames($displayLocale = null): array
72114
return self::asort(self::readEntry(['Names'], $displayLocale), $displayLocale);
73115
}
74116

117+
/**
118+
* Gets the list of country names indexed with alpha3 codes as keys.
119+
*
120+
* Same as method getNames, but with alpha3 codes instead of alpha2 codes as keys.
121+
*
122+
* @return string[]
123+
*/
124+
public static function getAlpha3Names($displayLocale = null): array
125+
{
126+
$alpha2Names = self::getNames($displayLocale);
127+
$alpha3Names = [];
128+
foreach ($alpha2Names as $alpha2Code => $name) {
129+
$alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
130+
}
131+
132+
return $alpha3Names;
133+
}
134+
75135
protected static function getPath(): string
76136
{
77137
return Intl::getDataDirectory().'/'.Intl::REGION_DIR;

src/Symfony/Component/Intl/Data/Generator/LanguageDataGenerator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,16 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, $temp
162162

163163
sort($this->languageCodes);
164164

165+
$alpha2ToAlpha3 = $this->generateAlpha2ToAlpha3Mapping($metadataBundle);
166+
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
167+
asort($alpha3ToAlpha2);
168+
165169
return [
166170
'Version' => $rootBundle['Version'],
167171
'Languages' => $this->languageCodes,
168172
'Aliases' => array_column(iterator_to_array($metadataBundle['alias']['language']), 'replacement'),
169-
'Alpha2ToAlpha3' => $this->generateAlpha2ToAlpha3Mapping($metadataBundle),
173+
'Alpha2ToAlpha3' => $alpha2ToAlpha3,
174+
'Alpha3ToAlpha2' => $alpha3ToAlpha2,
170175
];
171176
}
172177

src/Symfony/Component/Intl/Data/Generator/RegionDataGenerator.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
*/
2828
class RegionDataGenerator extends AbstractDataGenerator
2929
{
30+
/**
31+
* Source https://www.iso.org/obp/ui/#iso:pub:PUB500001:en
32+
*/
33+
private static $preferredAlpha2ToAlpha3Mapping = [
34+
'DE' => 'DEU',
35+
'FR' => 'FRA',
36+
'MM' => 'MMR',
37+
'TL' => 'TLS',
38+
'YE' => 'YEM',
39+
];
40+
3041
private static $blacklist = [
3142
// Exceptional reservations
3243
'AC' => true, // Ascension Island
@@ -82,6 +93,7 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
8293
protected function compileTemporaryBundles(BundleCompilerInterface $compiler, $sourceDir, $tempDir)
8394
{
8495
$compiler->compile($sourceDir.'/region', $tempDir);
96+
$compiler->compile($sourceDir.'/misc/metadata.txt', $tempDir);
8597
}
8698

8799
/**
@@ -125,14 +137,25 @@ protected function generateDataForRoot(BundleEntryReaderInterface $reader, $temp
125137
protected function generateDataForMeta(BundleEntryReaderInterface $reader, $tempDir)
126138
{
127139
$rootBundle = $reader->read($tempDir, 'root');
140+
$metadataBundle = $reader->read($tempDir, 'metadata');
128141

129142
$this->regionCodes = array_unique($this->regionCodes);
130143

144+
$alpha2ToAlpha3 = $this->generateAlpha3($metadataBundle);
145+
131146
sort($this->regionCodes);
132147

148+
$alpha3ToAlpha2 = [];
149+
foreach ($this->regionCodes as $alpha2Code) {
150+
$alpha3code = $alpha2ToAlpha3[$alpha2Code];
151+
$alpha3ToAlpha2[$alpha3code] = $alpha2Code;
152+
}
153+
133154
return [
134155
'Version' => $rootBundle['Version'],
135156
'Regions' => $this->regionCodes,
157+
'Alpha2ToAlpha3' => $alpha2ToAlpha3,
158+
'Alpha3ToAlpha2' => $alpha3ToAlpha2,
136159
];
137160
}
138161

@@ -154,4 +177,38 @@ protected function generateRegionNames(ArrayAccessibleResourceBundle $localeBund
154177

155178
return $regionNames;
156179
}
180+
181+
protected function generateAlpha3(ArrayAccessibleResourceBundle $metadataBundle)
182+
{
183+
$alpha2Codes = array_flip($this->regionCodes);
184+
$alpha2ToAlpha3 = [];
185+
foreach ($metadataBundle['alias']['territory'] as $alias => $data) {
186+
if (3 !== \strlen($alias) || 'overlong' !== $data['reason'] || ctype_digit($alias)) {
187+
continue;
188+
}
189+
190+
$alpha2Code = $data['replacement'];
191+
if (!isset($alpha2Codes[$alpha2Code])) {
192+
continue;
193+
}
194+
195+
if (!isset($alpha2ToAlpha3[$alpha2Code])) {
196+
$alpha2ToAlpha3[$alpha2Code] = $alias;
197+
continue;
198+
}
199+
200+
// Found a second alias for the same country
201+
if (isset(self::$preferredAlpha2ToAlpha3Mapping[$alpha2Code])) {
202+
$preferred = self::$preferredAlpha2ToAlpha3Mapping[$alpha2Code];
203+
// Only use the preferred mapping if it actually is in the mapping
204+
if ($alias === $preferred) {
205+
$alpha2ToAlpha3[$alpha2Code] = $preferred;
206+
}
207+
}
208+
}
209+
210+
asort($alpha2ToAlpha3);
211+
212+
return $alpha2ToAlpha3;
213+
}
157214
}

src/Symfony/Component/Intl/Languages.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public static function exists(string $language): bool
5050
}
5151

5252
/**
53-
* Gets the language name from alpha2 code.
53+
* Gets the language name from its alpha2 code.
5454
*
55-
* @throws MissingResourceException if the language code does not exists
55+
* @throws MissingResourceException if the language code does not exist
5656
*/
5757
public static function getName(string $language, string $displayLocale = null): string
5858
{
@@ -79,6 +79,73 @@ public static function getAlpha3Code(string $language): string
7979
return self::readEntry(['Alpha2ToAlpha3', $language], 'meta');
8080
}
8181

82+
/**
83+
* Returns the ISO 639-1 two-letter code of a language, given a three letter code.
84+
*
85+
* @throws MissingResourceException if the language has no corresponding three-letter code
86+
*/
87+
public static function getAlpha2Code(string $language): string
88+
{
89+
return self::readEntry(['Alpha3ToAlpha2', $language], 'meta');
90+
}
91+
92+
/**
93+
* Returns all available languages as three-letter codes.
94+
*
95+
* Languages are returned as lowercase ISO 639-2 three-letter language codes.
96+
*
97+
* @return string[] an array of canonical ISO 639-2 language codes
98+
*/
99+
public static function getAlpha3Codes(): array
100+
{
101+
return self::readEntry(['Alpha2ToAlpha3'], 'meta');
102+
}
103+
104+
/**
105+
* @param string $language ISO 639-2 three-letter language code
106+
*/
107+
public static function alpha3CodeExists(string $language): bool
108+
{
109+
try {
110+
self::getAlpha2Code($language);
111+
112+
return true;
113+
} catch (MissingResourceException $e) {
114+
return false;
115+
}
116+
}
117+
118+
/**
119+
* Gets the language name from its ISO 639-2 three-letter code.
120+
*
121+
* @throws MissingResourceException if the country code does not exists
122+
*/
123+
public static function getAlpha3Name(string $language, string $displayLocale = null): string
124+
{
125+
return self::getName(self::getAlpha2Code($language), $displayLocale);
126+
}
127+
128+
/**
129+
* Gets the list of language names indexed with ISO 639-2 three-letter codes as keys.
130+
*
131+
* Same as method getNames, but with ISO 639-2 three-letter codes instead of ISO 639-1 codes as keys.
132+
*
133+
* @return string[]
134+
*/
135+
public static function getAlpha3Names($displayLocale = null): array
136+
{
137+
$alpha2Names = self::getNames($displayLocale);
138+
$alpha3Names = [];
139+
foreach ($alpha2Names as $alpha2Code => $name) {
140+
try {
141+
$alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
142+
} catch (MissingResourceException $e) {
143+
}
144+
}
145+
146+
return $alpha3Names;
147+
}
148+
82149
protected static function getPath(): string
83150
{
84151
return Intl::getDataDirectory().'/'.Intl::LANGUAGE_DIR;

0 commit comments

Comments
 (0)