|
15 | 15 | use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
|
16 | 16 | use Symfony\Component\Intl\Data\Util\ArrayAccessibleResourceBundle;
|
17 | 17 | use Symfony\Component\Intl\Data\Util\LocaleScanner;
|
| 18 | +use Symfony\Component\Intl\Exception\RuntimeException; |
18 | 19 |
|
19 | 20 | /**
|
20 | 21 | * The rule for compiling the region bundle.
|
|
28 | 29 | class RegionDataGenerator extends AbstractDataGenerator
|
29 | 30 | {
|
30 | 31 | /**
|
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. |
32 | 33 | */
|
33 | 34 | private static $preferredAlpha2ToAlpha3Mapping = [
|
| 35 | + 'CD' => 'COD', |
34 | 36 | 'DE' => 'DEU',
|
35 | 37 | 'FR' => 'FRA',
|
36 | 38 | 'MM' => 'MMR',
|
@@ -141,15 +143,11 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, strin
|
141 | 143 |
|
142 | 144 | $this->regionCodes = array_unique($this->regionCodes);
|
143 | 145 |
|
144 |
| - $alpha2ToAlpha3 = $this->generateAlpha3($metadataBundle); |
145 |
| - |
146 | 146 | sort($this->regionCodes);
|
147 | 147 |
|
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); |
153 | 151 |
|
154 | 152 | return [
|
155 | 153 | 'Version' => $rootBundle['Version'],
|
@@ -178,31 +176,32 @@ protected function generateRegionNames(ArrayAccessibleResourceBundle $localeBund
|
178 | 176 | return $regionNames;
|
179 | 177 | }
|
180 | 178 |
|
181 |
| - protected function generateAlpha3(ArrayAccessibleResourceBundle $metadataBundle) |
| 179 | + private function generateAlpha2ToAlpha3Mapping(array $countries, ArrayAccessibleResourceBundle $metadataBundle): array |
182 | 180 | {
|
183 |
| - $alpha2Codes = array_flip($this->regionCodes); |
| 181 | + $aliases = iterator_to_array($metadataBundle['alias']['territory']); |
184 | 182 | $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 | 183 |
|
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; |
206 | 205 | }
|
207 | 206 | }
|
208 | 207 | }
|
|
0 commit comments