Skip to content

Commit 7597687

Browse files
committed
feature #51073 [Intl] Add support for ISO 3166-1 numeric codes (benr77)
This PR was merged into the 6.4 branch. Discussion ---------- [Intl] Add support for ISO 3166-1 numeric codes | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #50860 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Add support for ISO-3166-1 numeric codes. As well as the existing Alpha2 and Alpha3 codes, this PR adds support for the numeric country codes as per https://www.iso.org/obp/ui/#search/code/. Commits ------- 972acf2 [Intl] Add support for ISO-3166-1 numeric codes
2 parents 3a2b8cb + 972acf2 commit 7597687

File tree

5 files changed

+1025
-0
lines changed

5 files changed

+1025
-0
lines changed

src/Symfony/Component/Intl/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add support for ISO-3166-1 numeric codes with `Countries::getNumericCode()`, `Countries::getNumericCodes()`,
8+
`Countries::numericCodeExists()` and `Countries::getAlpha2FromNumeric()`
9+
410
6.3
511
---
612

src/Symfony/Component/Intl/Countries.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,22 @@ public static function getAlpha3Codes(): array
5252
return self::readEntry(['Alpha2ToAlpha3'], 'meta');
5353
}
5454

55+
/**
56+
* Returns all available numeric country codes (3 digits).
57+
*
58+
* Countries are returned as ISO 3166 numeric three-digit country codes.
59+
*
60+
* This list only contains "officially assigned ISO 3166-1 numeric" country codes.
61+
*
62+
* Returns an array with Alpha2 country codes as keys, and numeric codes as values.
63+
*
64+
* @return array<string, string>
65+
*/
66+
public static function getNumericCodes(): array
67+
{
68+
return self::readEntry(['Alpha2ToNumeric'], 'meta');
69+
}
70+
5571
public static function getAlpha3Code(string $alpha2Code): string
5672
{
5773
return self::readEntry(['Alpha2ToAlpha3', $alpha2Code], 'meta');
@@ -62,6 +78,17 @@ public static function getAlpha2Code(string $alpha3Code): string
6278
return self::readEntry(['Alpha3ToAlpha2', $alpha3Code], 'meta');
6379
}
6480

81+
public static function getNumericCode(string $alpha2Code): string
82+
{
83+
return self::readEntry(['Alpha2ToNumeric', $alpha2Code], 'meta');
84+
}
85+
86+
public static function getAlpha2FromNumeric(string $numericCode): string
87+
{
88+
// Use an underscore prefix to force numeric strings with leading zeros to remain as strings
89+
return self::readEntry(['NumericToAlpha2', '_'.$numericCode], 'meta');
90+
}
91+
6592
public static function exists(string $alpha2Code): bool
6693
{
6794
try {
@@ -84,6 +111,17 @@ public static function alpha3CodeExists(string $alpha3Code): bool
84111
}
85112
}
86113

114+
public static function numericCodeExists(string $numericCode): bool
115+
{
116+
try {
117+
self::getAlpha2FromNumeric($numericCode);
118+
119+
return true;
120+
} catch (MissingResourceException $e) {
121+
return false;
122+
}
123+
}
124+
87125
/**
88126
* Gets the country name from its alpha2 code.
89127
*

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,33 @@ class RegionDataGenerator extends AbstractDataGenerator
6161
'ZZ' => true, // Unknown Region
6262
];
6363

64+
// @see https://en.wikipedia.org/wiki/ISO_3166-1_numeric#Withdrawn_codes
65+
private const WITHDRAWN_CODES = [
66+
128, // Canton and Enderbury Islands
67+
200, // Czechoslovakia
68+
216, // Dronning Maud Land
69+
230, // Ethiopia
70+
249, // France, Metropolitan
71+
278, // German Democratic Republic
72+
280, // Germany, Federal Republic of
73+
396, // Johnston Island
74+
488, // Midway Islands
75+
530, // Netherlands Antilles
76+
532, // Netherlands Antilles
77+
536, // Neutral Zone
78+
582, // Pacific Islands (Trust Territory)
79+
590, // Panama
80+
658, // Saint Kitts-Nevis-Anguilla
81+
720, // Yemen, Democratic
82+
736, // Sudan
83+
810, // USSR
84+
849, // United States Miscellaneous Pacific Islands
85+
872, // Wake Island
86+
886, // Yemen Arab Republic
87+
890, // Yugoslavia, Socialist Federal Republic of
88+
891, // Serbia and Montenegro
89+
];
90+
6491
/**
6592
* Collects all available language codes.
6693
*
@@ -133,10 +160,21 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, strin
133160
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
134161
asort($alpha3ToAlpha2);
135162

163+
$alpha2ToNumeric = $this->generateAlpha2ToNumericMapping($metadataBundle);
164+
$numericToAlpha2 = [];
165+
foreach ($alpha2ToNumeric as $alpha2 => $numeric) {
166+
// Add underscore prefix to force keys with leading zeros to remain as string keys.
167+
$numericToAlpha2['_'.$numeric] = $alpha2;
168+
}
169+
170+
asort($numericToAlpha2);
171+
136172
return [
137173
'Regions' => $this->regionCodes,
138174
'Alpha2ToAlpha3' => $alpha2ToAlpha3,
139175
'Alpha3ToAlpha2' => $alpha3ToAlpha2,
176+
'Alpha2ToNumeric' => $alpha2ToNumeric,
177+
'NumericToAlpha2' => $numericToAlpha2,
140178
];
141179
}
142180

@@ -159,10 +197,12 @@ protected function generateRegionNames(ArrayAccessibleResourceBundle $localeBund
159197
private function generateAlpha2ToAlpha3Mapping(array $countries, ArrayAccessibleResourceBundle $metadataBundle): array
160198
{
161199
$aliases = iterator_to_array($metadataBundle['alias']['territory']);
200+
162201
$alpha2ToAlpha3 = [];
163202

164203
foreach ($aliases as $alias => $data) {
165204
$country = $data['replacement'];
205+
166206
if (2 === \strlen($country) && 3 === \strlen($alias) && 'overlong' === $data['reason']) {
167207
if (isset(self::PREFERRED_ALPHA2_TO_ALPHA3_MAPPING[$country])) {
168208
// Validate to prevent typos
@@ -190,4 +230,35 @@ private function generateAlpha2ToAlpha3Mapping(array $countries, ArrayAccessible
190230

191231
return $alpha2ToAlpha3;
192232
}
233+
234+
private function generateAlpha2ToNumericMapping(ArrayAccessibleResourceBundle $metadataBundle): array
235+
{
236+
$aliases = iterator_to_array($metadataBundle['alias']['territory']);
237+
238+
$alpha2ToNumeric = [];
239+
240+
foreach ($aliases as $alias => $data) {
241+
if (!is_numeric($alias)) {
242+
continue;
243+
}
244+
245+
if (\in_array($alias, self::WITHDRAWN_CODES)) {
246+
continue;
247+
}
248+
249+
if (isset(self::DENYLIST[$data['replacement']])) {
250+
continue;
251+
}
252+
253+
if ('deprecated' === $data['reason']) {
254+
continue;
255+
}
256+
257+
$alpha2ToNumeric[$data['replacement']] = (string) $alias;
258+
}
259+
260+
ksort($alpha2ToNumeric);
261+
262+
return $alpha2ToNumeric;
263+
}
193264
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy