[Intl] Validate region preferred alpha code mapping
This commit is contained in:
parent
26cc652a79
commit
345305904b
@ -15,6 +15,7 @@ use Symfony\Component\Intl\Data\Bundle\Compiler\BundleCompilerInterface;
|
|||||||
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
|
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
|
||||||
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
|
use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
|
||||||
use Symfony\Component\Intl\Data\Util\LocaleScanner;
|
use Symfony\Component\Intl\Data\Util\LocaleScanner;
|
||||||
|
use Symfony\Component\Intl\Exception\RuntimeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The rule for compiling the region bundle.
|
* The rule for compiling the region bundle.
|
||||||
@ -28,9 +29,10 @@ use Symfony\Component\Intl\Data\Util\LocaleScanner;
|
|||||||
class RegionDataGenerator extends AbstractDataGenerator
|
class RegionDataGenerator extends AbstractDataGenerator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Source: https://www.iso.org/obp/ui/#iso:pub:PUB500001:en.
|
* Source: https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes.
|
||||||
*/
|
*/
|
||||||
private static $preferredAlpha2ToAlpha3Mapping = [
|
private static $preferredAlpha2ToAlpha3Mapping = [
|
||||||
|
'CD' => 'COD',
|
||||||
'DE' => 'DEU',
|
'DE' => 'DEU',
|
||||||
'FR' => 'FRA',
|
'FR' => 'FRA',
|
||||||
'MM' => 'MMR',
|
'MM' => 'MMR',
|
||||||
@ -141,15 +143,11 @@ class RegionDataGenerator extends AbstractDataGenerator
|
|||||||
|
|
||||||
$this->regionCodes = array_unique($this->regionCodes);
|
$this->regionCodes = array_unique($this->regionCodes);
|
||||||
|
|
||||||
$alpha2ToAlpha3 = $this->generateAlpha3($metadataBundle);
|
|
||||||
|
|
||||||
sort($this->regionCodes);
|
sort($this->regionCodes);
|
||||||
|
|
||||||
$alpha3ToAlpha2 = [];
|
$alpha2ToAlpha3 = $this->generateAlpha2ToAlpha3Mapping(array_flip($this->regionCodes), $metadataBundle);
|
||||||
foreach ($this->regionCodes as $alpha2Code) {
|
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
|
||||||
$alpha3code = $alpha2ToAlpha3[$alpha2Code];
|
asort($alpha3ToAlpha2);
|
||||||
$alpha3ToAlpha2[$alpha3code] = $alpha2Code;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [
|
return [
|
||||||
'Version' => $rootBundle['Version'],
|
'Version' => $rootBundle['Version'],
|
||||||
@ -178,31 +176,32 @@ class RegionDataGenerator extends AbstractDataGenerator
|
|||||||
return $regionNames;
|
return $regionNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateAlpha3(ArrayAccessibleResourceBundle $metadataBundle)
|
private function generateAlpha2ToAlpha3Mapping(array $countries, ArrayAccessibleResourceBundle $metadataBundle): array
|
||||||
{
|
{
|
||||||
$alpha2Codes = array_flip($this->regionCodes);
|
$aliases = iterator_to_array($metadataBundle['alias']['territory']);
|
||||||
$alpha2ToAlpha3 = [];
|
$alpha2ToAlpha3 = [];
|
||||||
foreach ($metadataBundle['alias']['territory'] as $alias => $data) {
|
|
||||||
if (3 !== \strlen($alias) || 'overlong' !== $data['reason'] || ctype_digit($alias)) {
|
foreach ($aliases as $alias => $data) {
|
||||||
continue;
|
$country = $data['replacement'];
|
||||||
|
if (2 === \strlen($country) && 3 === \strlen($alias) && 'overlong' === $data['reason']) {
|
||||||
|
if (isset(self::$preferredAlpha2ToAlpha3Mapping[$country])) {
|
||||||
|
// Validate to prevent typos
|
||||||
|
if (!isset($aliases[self::$preferredAlpha2ToAlpha3Mapping[$country]])) {
|
||||||
|
throw new RuntimeException('The statically set three-letter mapping '.self::$preferredAlpha2ToAlpha3Mapping[$country].' for the country code '.$country.' seems to be invalid. Typo?');
|
||||||
}
|
}
|
||||||
|
|
||||||
$alpha2Code = $data['replacement'];
|
$alpha3 = self::$preferredAlpha2ToAlpha3Mapping[$country];
|
||||||
if (!isset($alpha2Codes[$alpha2Code])) {
|
$alpha2 = $aliases[$alpha3]['replacement'];
|
||||||
continue;
|
|
||||||
|
if ($country !== $alpha2) {
|
||||||
|
throw new RuntimeException('The statically set three-letter mapping '.$alpha3.' for the country code '.$country.' seems to be an alias for '.$alpha2.'. Wrong mapping?');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($alpha2ToAlpha3[$alpha2Code])) {
|
$alpha2ToAlpha3[$country] = $alpha3;
|
||||||
$alpha2ToAlpha3[$alpha2Code] = $alias;
|
} elseif (isset($alpha2ToAlpha3[$country])) {
|
||||||
continue;
|
throw new RuntimeException('Multiple three-letter mappings exist for the country code '.$country.'. Please add one of them to the property $preferredAlpha2ToAlpha3Mapping.');
|
||||||
}
|
} elseif (isset($countries[$country]) && self::isValidCountryCode($alias)) {
|
||||||
|
$alpha2ToAlpha3[$country] = $alias;
|
||||||
// Found a second alias for the same country
|
|
||||||
if (isset(self::$preferredAlpha2ToAlpha3Mapping[$alpha2Code])) {
|
|
||||||
$preferred = self::$preferredAlpha2ToAlpha3Mapping[$alpha2Code];
|
|
||||||
// Only use the preferred mapping if it actually is in the mapping
|
|
||||||
if ($alias === $preferred) {
|
|
||||||
$alpha2ToAlpha3[$alpha2Code] = $preferred;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user