Skip to content

[Intl][Currency] fetch active currencies in currency type by default #61400

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

Draft
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Draft
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
38 changes: 27 additions & 11 deletions src/Symfony/Component/Intl/Currencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;
}
}
}
Loading