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

Skip to content

[Intl] Ensure data consistency between alpha and numeric codes #60711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, strin
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
asort($alpha3ToAlpha2);

$alpha2ToNumeric = $this->generateAlpha2ToNumericMapping($metadataBundle);
$alpha2ToNumeric = $this->generateAlpha2ToNumericMapping(array_flip($this->regionCodes), $metadataBundle);
$numericToAlpha2 = [];
foreach ($alpha2ToNumeric as $alpha2 => $numeric) {
// Add underscore prefix to force keys with leading zeros to remain as string keys.
Expand Down Expand Up @@ -231,7 +231,7 @@ private function generateAlpha2ToAlpha3Mapping(array $countries, ArrayAccessible
return $alpha2ToAlpha3;
}

private function generateAlpha2ToNumericMapping(ArrayAccessibleResourceBundle $metadataBundle): array
private function generateAlpha2ToNumericMapping(array $countries, ArrayAccessibleResourceBundle $metadataBundle): array
{
$aliases = iterator_to_array($metadataBundle['alias']['territory']);

Expand All @@ -250,6 +250,10 @@ private function generateAlpha2ToNumericMapping(ArrayAccessibleResourceBundle $m
continue;
}

if (!isset($countries[$data['replacement']])) {
continue;
}

if ('deprecated' === $data['reason']) {
continue;
}
Expand Down
72 changes: 0 additions & 72 deletions src/Symfony/Component/Intl/Resources/data/regions/meta.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 14 additions & 37 deletions src/Symfony/Component/Intl/Tests/CountriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,6 @@ class CountriesTest extends ResourceBundleTestCase
];

private const ALPHA2_TO_NUMERIC = [
'AA' => '958',
'AD' => '020',
'AE' => '784',
'AF' => '004',
Expand Down Expand Up @@ -715,18 +714,6 @@ class CountriesTest extends ResourceBundleTestCase
'PW' => '585',
'PY' => '600',
'QA' => '634',
'QM' => '959',
'QN' => '960',
'QP' => '962',
'QQ' => '963',
'QR' => '964',
'QS' => '965',
'QT' => '966',
'QV' => '968',
'QW' => '969',
'QX' => '970',
'QY' => '971',
'QZ' => '972',
'RE' => '638',
'RO' => '642',
'RS' => '688',
Expand Down Expand Up @@ -784,36 +771,26 @@ class CountriesTest extends ResourceBundleTestCase
'VU' => '548',
'WF' => '876',
'WS' => '882',
'XC' => '975',
'XD' => '976',
'XE' => '977',
'XF' => '978',
'XG' => '979',
'XH' => '980',
'XI' => '981',
'XJ' => '982',
'XL' => '984',
'XM' => '985',
'XN' => '986',
'XO' => '987',
'XP' => '988',
'XQ' => '989',
'XR' => '990',
'XS' => '991',
'XT' => '992',
'XU' => '993',
'XV' => '994',
'XW' => '995',
'XX' => '996',
'XY' => '997',
'XZ' => '998',
'YE' => '887',
'YT' => '175',
'ZA' => '710',
'ZM' => '894',
'ZW' => '716',
];

public function testAllGettersGenerateTheSameDataSetCount()
{
$alpha2Count = count(Countries::getCountryCodes());
$alpha3Count = count(Countries::getAlpha3Codes());
$numericCodesCount = count(Countries::getNumericCodes());
$namesCount = count(Countries::getNames());

// we base all on Name count since it is the first to be generated
$this->assertEquals($namesCount, $alpha2Count, 'Alpha 2 count does not match');
$this->assertEquals($namesCount, $alpha3Count, 'Alpha 3 count does not match');
$this->assertEquals($namesCount, $numericCodesCount, 'Numeric codes count does not match');
}

public function testGetCountryCodes()
{
$this->assertSame(self::COUNTRIES, Countries::getCountryCodes());
Expand Down Expand Up @@ -992,7 +969,7 @@ public function testGetNumericCode()
public function testNumericCodeExists()
{
$this->assertTrue(Countries::numericCodeExists('250'));
$this->assertTrue(Countries::numericCodeExists('982'));
$this->assertTrue(Countries::numericCodeExists('008'));
$this->assertTrue(Countries::numericCodeExists('716'));
$this->assertTrue(Countries::numericCodeExists('036'));
$this->assertFalse(Countries::numericCodeExists('667'));
Expand Down