-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Intl] Add methods to filter currencies more precisely #61431
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
base: 7.4
Are you sure you want to change the base?
Conversation
Hey! To help keep things organized, we don't allow "Draft" pull requests. Could you please click the "ready for review" button or close this PR and open a new one when you are done? Note that a pull request does not have to be "perfect" or "ready for merge" when you first open it. We just want it to be ready for a first review. Cheers! Carsonbot |
Currencies::isActive()
to ensure the currency is active in at least 1 countryCurrencies::isActive()
to ensure the currency is active in at least 1 country
I think it's pretty much ready for a first review @javiereguiluz. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps an alternative API could be getCurrencyCodesForCountry(string $country, ?bool $active = null)
+ existsForCountry(string $country, ?bool $active = null)
?
I will add this ASAP 👍🏻 |
I added the requested methods + tender filter but I need to add fews tests to ensure all uses cases are covered. I also need to update the CHANGELOG. I'll continue this ASAP. |
Currencies::isActive()
to ensure the currency is active in at least 1 country…cases to achieve 100% code coverage
I’ve applied the requested changes, this should be ready for a second review. |
src/Symfony/Component/Intl/Data/Generator/CurrencyDataGenerator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Intl/Data/Generator/CurrencyDataGenerator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Intl/Data/Generator/CurrencyDataGenerator.php
Outdated
Show resolved
Hide resolved
$epochWithMicros = \sprintf('%d.%03d000', $seconds, $millisecondsRemainder); | ||
|
||
$datetime = \DateTimeImmutable::createFromFormat( | ||
'U.u', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's simplify this and remove the part about $millisecondsRemainder
in the end we have only day precision
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks (but CS things).
I didn't comment on all places where my comments apply, you should be able to infer the rest ;)
$this->assertSame( | ||
[], | ||
Currencies::forCountry('AQ', null, true, new \DateTimeImmutable('2025-01-01', new \DateTimeZone('Etc/UTC'))), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->assertSame( | |
[], | |
Currencies::forCountry('AQ', null, true, new \DateTimeImmutable('2025-01-01', new \DateTimeZone('Etc/UTC'))), | |
); | |
$this->assertSame([], Currencies::forCountry('AQ', null, true, new \DateTimeImmutable('2025-01-01', new \DateTimeZone('Etc/UTC')))); |
public static function forCountry( | ||
string $country, | ||
?bool $legalTender = true, | ||
?bool $active = true, | ||
\DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC')), | ||
): array { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public static function forCountry( | |
string $country, | |
?bool $legalTender = true, | |
?bool $active = true, | |
\DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC')), | |
): array { | |
public static function forCountry(string $country, ?bool $legalTender = true, ?bool $active = true, \DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC'))): array | |
{ |
@@ -139,6 +139,154 @@ public static function forNumericCode(int $numericCode): array | |||
return self::readEntry(['NumericToAlpha3', (string) $numericCode], 'meta'); | |||
} | |||
|
|||
/** | |||
* @param non-empty-string $country e.g. 'FR' | |||
* @param ?bool $legalTender if the currency must be a legal tender. Using null do not filter anything |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param ?bool $legalTender if the currency must be a legal tender. Using null do not filter anything | |
* @param ?bool $legalTender If the currency must be a legal tender; null to not filter anything |
/** | ||
* @param non-empty-string $country e.g. 'FR' | ||
* @param ?bool $legalTender if the currency must be a legal tender. Using null do not filter anything | ||
* @param ?bool $active Indicates whether the currency should always be active for the given $date. Using null do not filter anything |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param ?bool $active Indicates whether the currency should always be active for the given $date. Using null do not filter anything | |
* @param ?bool $active Indicates whether the currency should always be active for the given $date; null to not filter anything |
* @param non-empty-string $country e.g. 'FR' | ||
* @param ?bool $legalTender if the currency must be a legal tender. Using null do not filter anything | ||
* @param ?bool $active Indicates whether the currency should always be active for the given $date. Using null do not filter anything | ||
* @param \DateTimeInterface $date The date string on which the check will be performed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* @param \DateTimeInterface $date The date string on which the check will be performed | |
* @param \DateTimeInterface $date The date on which the check will be performed |
/** | ||
* @var \DateTimeImmutable $to The date format is always valid as it is generated using the CurrencyDataGenerator | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we prefer not polluting the code with such SA concerns:
/** | |
* @var \DateTimeImmutable $to The date format is always valid as it is generated using the CurrencyDataGenerator | |
*/ |
return true; | ||
} | ||
|
||
return $active === self::isActive($country, $currency, $currencyMetadata, $date); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return $active === self::isActive($country, $currency, $currencyMetadata, $date); | |
return self::isActive($country, $currency, $currencyMetadata, $date) === $active; |
* | ||
* @return non-empty-string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove the non-empty-string declarations, they're not really useful IMHO
* | |
* @return non-empty-string |
): bool { | ||
if (!\array_key_exists('from', $currencyMetadata)) { | ||
// Note: currencies that are not legal tender don't have often validity dates. | ||
throw new \RuntimeException("Cannot check whether the currency $currency is active or not in $country because they are no validity dates available."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return false
?
Description
In the ICU dataset, there are values that are no longer relevant / no longer used today (e.g. the BEF currency). This PR add a method that check the metadata in the ICU metadata to check if the currency is active in at least 1 country.
Limitations
$active
).