Skip to content

Commit 320009c

Browse files
committed
Merge branch '4.4'
* 4.4: [Mailer] simplified the way TLS/SSL/StartTls work [VarDumper] Add test dump image Allow exchange type headers binding Add types to private and final methods. [Messenger] InMemoryTransport handle acknowledged and rejected messages [Intl] Validate region preferred alpha code mapping Added ErrorHandler::call() method utility to turns any PHP warnings into `\ErrorException` [Intl] Full alpha3 language support [Monolog] Added ElasticsearchLogstashHandler
2 parents a1c063c + cba52ba commit 320009c

File tree

6 files changed

+1532
-62
lines changed

6 files changed

+1532
-62
lines changed

Data/Generator/LanguageDataGenerator.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,12 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, strin
162162

163163
sort($this->languageCodes);
164164

165-
$alpha2ToAlpha3 = $this->generateAlpha2ToAlpha3Mapping($metadataBundle);
166-
$alpha3ToAlpha2 = array_flip($alpha2ToAlpha3);
167-
asort($alpha3ToAlpha2);
168-
169165
return [
170166
'Version' => $rootBundle['Version'],
171167
'Languages' => $this->languageCodes,
172-
'Alpha2ToAlpha3' => $alpha2ToAlpha3,
173-
'Alpha3ToAlpha2' => $alpha3ToAlpha2,
168+
'Alpha3Languages' => $this->generateAlpha3Codes($this->languageCodes, $metadataBundle),
169+
'Alpha2ToAlpha3' => $this->generateAlpha2ToAlpha3Mapping($metadataBundle),
170+
'Alpha3ToAlpha2' => $this->generateAlpha3ToAlpha2Mapping($metadataBundle),
174171
];
175172
}
176173

@@ -179,6 +176,23 @@ private static function generateLanguageNames(ArrayAccessibleResourceBundle $loc
179176
return array_diff_key(iterator_to_array($localeBundle['Languages']), self::$blacklist);
180177
}
181178

179+
private function generateAlpha3Codes(array $languageCodes, ArrayAccessibleResourceBundle $metadataBundle): array
180+
{
181+
$alpha3Codes = array_flip(array_filter($languageCodes, static function (string $language): bool {
182+
return 3 === \strlen($language);
183+
}));
184+
185+
foreach ($metadataBundle['alias']['language'] as $alias => $data) {
186+
if (3 === \strlen($alias) && 'overlong' === $data['reason']) {
187+
$alpha3Codes[$alias] = true;
188+
}
189+
}
190+
191+
ksort($alpha3Codes);
192+
193+
return array_keys($alpha3Codes);
194+
}
195+
182196
private function generateAlpha2ToAlpha3Mapping(ArrayAccessibleResourceBundle $metadataBundle)
183197
{
184198
$aliases = iterator_to_array($metadataBundle['alias']['language']);
@@ -213,4 +227,20 @@ private function generateAlpha2ToAlpha3Mapping(ArrayAccessibleResourceBundle $me
213227

214228
return $alpha2ToAlpha3;
215229
}
230+
231+
private function generateAlpha3ToAlpha2Mapping(ArrayAccessibleResourceBundle $metadataBundle): array
232+
{
233+
$alpha3ToAlpha2 = [];
234+
235+
foreach ($metadataBundle['alias']['language'] as $alias => $data) {
236+
$language = $data['replacement'];
237+
if (2 === \strlen($language) && 3 === \strlen($alias) && 'overlong' === $data['reason']) {
238+
$alpha3ToAlpha2[$alias] = $language;
239+
}
240+
}
241+
242+
asort($alpha3ToAlpha2);
243+
244+
return $alpha3ToAlpha2;
245+
}
216246
}

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, strin
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
}

Languages.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public static function getAlpha2Code(string $language): string
9898
*/
9999
public static function getAlpha3Codes(): array
100100
{
101-
return self::readEntry(['Alpha2ToAlpha3'], 'meta');
101+
return self::readEntry(['Alpha3Languages'], 'meta');
102102
}
103103

104104
/**
@@ -111,7 +111,12 @@ public static function alpha3CodeExists(string $language): bool
111111

112112
return true;
113113
} catch (MissingResourceException $e) {
114-
return false;
114+
static $cache;
115+
if (null === $cache) {
116+
$cache = array_flip(self::getAlpha3Codes());
117+
}
118+
119+
return isset($cache[$language]);
115120
}
116121
}
117122

@@ -122,7 +127,15 @@ public static function alpha3CodeExists(string $language): bool
122127
*/
123128
public static function getAlpha3Name(string $language, string $displayLocale = null): string
124129
{
125-
return self::getName(self::getAlpha2Code($language), $displayLocale);
130+
try {
131+
return self::getName(self::getAlpha2Code($language), $displayLocale);
132+
} catch (MissingResourceException $e) {
133+
if (3 === \strlen($language)) {
134+
return self::getName($language, $displayLocale);
135+
}
136+
137+
throw $e;
138+
}
126139
}
127140

128141
/**
@@ -137,6 +150,10 @@ public static function getAlpha3Names($displayLocale = null): array
137150
$alpha2Names = self::getNames($displayLocale);
138151
$alpha3Names = [];
139152
foreach ($alpha2Names as $alpha2Code => $name) {
153+
if (3 === \strlen($alpha2Code)) {
154+
$alpha3Names[$alpha2Code] = $name;
155+
continue;
156+
}
140157
try {
141158
$alpha3Names[self::getAlpha3Code($alpha2Code)] = $name;
142159
} catch (MissingResourceException $e) {

0 commit comments

Comments
 (0)