Skip to content

[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

Open
wants to merge 8 commits into
base: 7.4
Choose a base branch
from

Conversation

Crovitche-1623
Copy link

@Crovitche-1623 Crovitche-1623 commented Aug 15, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Close #61365
License MIT

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

There are currencies that do not have legal tender and therefore no available date ranges. That's why only the currencies that has a legal value within the country borders can be checked.

@carsonbot
Copy link

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

@Crovitche-1623 Crovitche-1623 changed the title [DRAFT] feat: Add Currencies::isActive() to ensure the currency is active in at least 1 country [Intl] Add Currencies::isActive() to ensure the currency is active in at least 1 country Aug 15, 2025
@Crovitche-1623 Crovitche-1623 marked this pull request as ready for review August 15, 2025 09:33
@carsonbot carsonbot added this to the 7.4 milestone Aug 15, 2025
@Crovitche-1623
Copy link
Author

I think it's pretty much ready for a first review @javiereguiluz.

Copy link
Contributor

@ro0NL ro0NL left a 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)
?

@@ -139,6 +139,40 @@ public static function forNumericCode(int $numericCode): array
return self::readEntry(['NumericToAlpha3', (string) $numericCode], 'meta');
}

public static function isActive(string $tenderCurrency): bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt we check by country code and currency code? eg. USD is active, but not in all countries.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we could do that but that would require the country code in every situation (which is not always desirable).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original goal of this PR was to exclude obsolete currencies (not at least in 1 country). But I can add a parameter to specify the country if it's necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, but im wondering what real life usecase that would solve?

with getCurrencyCodesForCountry we could put the preferred countries on top in e.g. a dropdown menu (given a country is known)

Copy link
Author

@Crovitche-1623 Crovitche-1623 Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, in a select with all the currencies available (without the obsolete ones).

E.g. you may want to define prices in a specific currency without specifying the country you want to sell in.

But I think the best is to add all methods to handle all the use cases:

  • Currencies::existsInAtLeastOneRegion(string $currency, bool $isActive = false): bool
  • Currencies::existsInRegion(string $currency, string $region, bool $isActive = false): bool
  • Currencies::forRegion(string $region, bool $isActive = false): array --> return an array of string for the given region

WDYT ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds reasonable 👍 i would avoid "region" terminology, the surfacing public api uses "country (code)"

Copy link
Author

@Crovitche-1623 Crovitche-1623 Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Even though the ICU dataset uses region tags for countries ? Because there is a difference between regions and countries. Countries are juridically organisation (for instance France) but regions are for territorial areas (for instance the Martinique Island (MQ) that belongs to France)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, we have Countries and Timezones::forCountryCode already

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok !

@Crovitche-1623
Copy link
Author

perhaps an alternative API could be getCurrencyCodesForCountry(string $country, ?bool $active = null) + existsForCountry(string $country, ?bool $active = null)

?

I will add this ASAP 👍🏻

@Crovitche-1623
Copy link
Author

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.

@Crovitche-1623 Crovitche-1623 changed the title [Intl] Add Currencies::isActive() to ensure the currency is active in at least 1 country [Intl] Add methods to filter currencies more precisely Aug 15, 2025
string $country,
string $currency,
bool $activeOnly = true,
string $date = 'today',
Copy link
Contributor

@ro0NL ro0NL Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?\DateTimeImmutable $date = null?

providing new \DateTimeImmutable('today') is little effort, while providing more flexibility

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍

public static function existsInCountry(
string $country,
string $currency,
bool $activeOnly = true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nullable boolean $active, to account for both active + inactive?

Copy link
Author

@Crovitche-1623 Crovitche-1623 Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, just use false. I named the parameter activeOnly because I thought I would make no sense to retrieve the inactive ones.
WDYT ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I'm also fine with the nullable parameter.

* @param bool $activeOnly exclude currencies that are not active on the given $date in countries
* @param string $date the date on which the check will be performed if $activeOnly is set to true
*/
public static function existsInAtLeastOneCountry(
Copy link
Contributor

@ro0NL ro0NL Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels a bit weird naming compared to exists(), which should also checks existence in at least 1 country

in that sense im still fine with public API existsActive (or adding nullable boolean $active parameter to current exists() method)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, renaming the method to exists is also fine for me 👍

*
* @throws MissingResourceException if the given $country does not exist
*
* @return array<non-empty-string, array{from?: non-empty-string, to?: non-empty-string, tender?: bool}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not sure this is the proper return type compared to getCurrencyCodes and forNumeric, unexpected at least i think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Intl] Methods to check if a given currency is obsolete/active
3 participants