Skip to content

Commit 280fb11

Browse files
committed
[Intl] Validate region preferred alpha code mapping
1 parent dc69c66 commit 280fb11

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

Data/Generator/RegionDataGenerator.php

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
1616
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
1717
use Symfony\Component\Intl\Data\Util\LocaleScanner;
18+
use Symfony\Component\Intl\Exception\RuntimeException;
1819

1920
/**
2021
* The rule for compiling the region bundle.
@@ -28,9 +29,10 @@
2829
class RegionDataGenerator extends AbstractDataGenerator
2930
{
3031
/**
31-
* Source: https://www.iso.org/obp/ui/#iso:pub:PUB500001:en.
32+
* Source: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes.
3233
*/
3334
private static $preferredAlpha2ToAlpha3Mapping = [
35+
'CD' => 'COD',
3436
'DE' => 'DEU',
3537
'FR' => 'FRA',
3638
'MM' => 'MMR',
@@ -141,15 +143,11 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, $temp
141143

142144
$this->regionCodes = array_unique($this->regionCodes);
143145

144-
$alpha2ToAlpha3 = $this->generateAlpha3($metadataBundle);
145-
146146
sort($this->regionCodes);
147147

148-
$alpha3ToAlpha2 = [];
149-
foreach ($this->regionCodes as $alpha2Code) {
150-
$alpha3code = $alpha2ToAlpha3[$alpha2Code];
151-
$alpha3ToAlpha2[$alpha3code] = $alpha2Code;
152-
}
148+
$alpha2ToAlpha3 = $this->generateAlpha2ToAlpha3Mapping(array_flip($this->regionCodes), $metadataBundle);
149+
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
150+
asort($alpha3ToAlpha2);
153151

154152
return [
155153
'Version' => $rootBundle['Version'],
@@ -178,31 +176,32 @@ protected function generateRegionNames(ArrayAccessibleResourceBundle $localeBund
178176
return $regionNames;
179177
}
180178

181-
protected function generateAlpha3(ArrayAccessibleResourceBundle $metadataBundle)
179+
private function generateAlpha2ToAlpha3Mapping(array $countries, ArrayAccessibleResourceBundle $metadataBundle): array
182180
{
183-
$alpha2Codes = array_flip($this->regionCodes);
181+
$aliases = iterator_to_array($metadataBundle['alias']['territory']);
184182
$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-
}
199183

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;
184+
foreach ($aliases as $alias => $data) {
185+
$country = $data['replacement'];
186+
if (2 === \strlen($country) && 3 === \strlen($alias) && 'overlong' === $data['reason']) {
187+
if (isset(self::$preferredAlpha2ToAlpha3Mapping[$country])) {
188+
// Validate to prevent typos
189+
if (!isset($aliases[self::$preferredAlpha2ToAlpha3Mapping[$country]])) {
190+
throw new RuntimeException('The statically set three-letter mapping '.self::$preferredAlpha2ToAlpha3Mapping[$country].' for the country code '.$country.' seems to be invalid. Typo?');
191+
}
192+
193+
$alpha3 = self::$preferredAlpha2ToAlpha3Mapping[$country];
194+
$alpha2 = $aliases[$alpha3]['replacement'];
195+
196+
if ($country !== $alpha2) {
197+
throw new RuntimeException('The statically set three-letter mapping '.$alpha3.' for the country code '.$country.' seems to be an alias for '.$alpha2.'. Wrong mapping?');
198+
}
199+
200+
$alpha2ToAlpha3[$country] = $alpha3;
201+
} elseif (isset($alpha2ToAlpha3[$country])) {
202+
throw new RuntimeException('Multiple three-letter mappings exist for the country code '.$country.'. Please add one of them to the property $preferredAlpha2ToAlpha3Mapping.');
203+
} elseif (isset($countries[$country]) && self::isValidCountryCode($alias)) {
204+
$alpha2ToAlpha3[$country] = $alias;
206205
}
207206
}
208207
}

0 commit comments

Comments
 (0)