Skip to content

Commit efac5e7

Browse files
feat: handle unknown currencies from an ISO 4217 POV
1 parent dd07065 commit efac5e7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Symfony/Component/Intl/Currencies.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public static function forCountry(string $country, ?bool $legalTender = true, ?b
183183
*/
184184
public static function isValidInCountry(string $country, string $currency, ?bool $legalTender = true, ?bool $active = true, \DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC'))): bool
185185
{
186+
if (!self::exists($currency)) {
187+
throw new \InvalidArgumentException("The currency $currency does not exist.");
188+
}
189+
186190
try {
187191
$currencyMetadata = self::readEntry(['Map', $country, $currency], 'meta');
188192
} catch (MissingResourceException) {
@@ -240,6 +244,10 @@ private static function isDateActive(string $country, string $currency, array $c
240244
*/
241245
public static function isValidInAnyCountry(string $currency, ?bool $legalTender = true, ?bool $active = true, \DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC'))): bool
242246
{
247+
if (!self::exists($currency)) {
248+
throw new \InvalidArgumentException("The currency $currency does not exist.");
249+
}
250+
243251
foreach (self::readEntry(['Map'], 'meta') as $countryCode => $country) {
244252
foreach ($country as $currencyCode => $currencyMetadata) {
245253
if ($currencyCode !== $currency) {

src/Symfony/Component/Intl/Tests/CurrenciesTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,4 +882,20 @@ public function testAntarticaHasNoCurrenciesIn2025()
882882
{
883883
$this->assertSame([], Currencies::forCountry('AQ', null, true, new \DateTimeImmutable('2025-01-01', new \DateTimeZone('Etc/UTC'))));
884884
}
885+
886+
public function testIsValidInCountryWithUnknownCurrencyThrowsException()
887+
{
888+
$this->expectException(\InvalidArgumentException::class);
889+
$this->expectExceptionMessage('The currency UNKNOWN-CURRENCY-FROM-ISO-4217 does not exist.');
890+
891+
Currencies::isValidInCountry('CH', 'UNKNOWN-CURRENCY-FROM-ISO-4217');
892+
}
893+
894+
public function testIsValidInAnyCountryWithUnknownCurrencyThrowsException()
895+
{
896+
$this->expectException(\InvalidArgumentException::class);
897+
$this->expectExceptionMessage('The currency UNKNOWN-CURRENCY-FROM-ISO-4217 does not exist.');
898+
899+
Currencies::isValidInAnyCountry('UNKNOWN-CURRENCY-FROM-ISO-4217');
900+
}
885901
}

0 commit comments

Comments
 (0)