diff --git a/src/Symfony/Component/Intl/Currencies.php b/src/Symfony/Component/Intl/Currencies.php index ce7fe5e2a84b..6f887e1ca139 100644 --- a/src/Symfony/Component/Intl/Currencies.php +++ b/src/Symfony/Component/Intl/Currencies.php @@ -58,27 +58,30 @@ public static function getName(string $currency, ?string $displayLocale = null): /** * @return string[] */ - public static function getNames(?string $displayLocale = null): array + public static function getNames(?string $displayLocale = null, bool $obsoleteCurrencies = false): array { - // ==================================================================== - // For reference: It is NOT possible to return names indexed by - // numeric code here, because some numeric codes map to multiple - // 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS") - // ==================================================================== - $names = self::readEntry(['Names'], $displayLocale); if ($names instanceof \Traversable) { $names = iterator_to_array($names); } - array_walk($names, function (&$value) { - $value = $value[self::INDEX_NAME]; - }); + $result = []; + foreach ($names as $code => $value) { + $currencyName = $value[self::INDEX_NAME]; + + // If obsoleteCurrencies=false, skip obsolete ones + if (!$obsoleteCurrencies && self::isObsolete($code)) { + continue; + } + + $result[$code] = $currencyName; + } - return self::asort($names, $displayLocale); + return self::asort($result, $displayLocale); } + /** * @throws MissingResourceException if the currency code does not exist */ @@ -143,4 +146,17 @@ protected static function getPath(): string { return Intl::getDataDirectory().'/'.Intl::CURRENCY_DIR; } + /** + * Check whether a currency is obsolete based on ICU metadata. + * @param string $code ISO 4217 currency code (e.g. "USD", "BEF"). + */ + public static function isObsolete(string $code): bool + { + try { + self::readEntry(['Meta', $code], 'meta'); + return false; + } catch (MissingResourceException $e) { + return true; + } + } }