Skip to content

Commit 32ca58b

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor tweaks [Intl] Document `isValidInCountry()`, `isValidInAnyCountry()`, `forCountry()`
2 parents 1eb2074 + e217d39 commit 32ca58b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

components/intl.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,52 @@ to catching the exception, you can also check if a given currency code is valid:
307307

308308
$isValidCurrency = Currencies::exists($currencyCode);
309309

310+
By default, the previous currency methods return all currencies, including
311+
those that are no longer in use. Symfony provides several methods to filter
312+
currencies so you can work only with those that are actually valid and in use
313+
for a given country.
314+
315+
These methods use ICU metadata (``tender``, ``from`` and ``to`` dates) to
316+
determine whether a currency is `legal tender`_ and/or active at a specific
317+
point in time::
318+
319+
use Symfony\Component\Intl\Currencies;
320+
321+
// get the list of today's legal and active currencies for a country
322+
$codes = Currencies::forCountry('FR');
323+
// ['EUR']
324+
325+
// include non-legal currencies too, and check them at a given date
326+
$codesAll = Currencies::forCountry(
327+
'ES',
328+
legalTender: null,
329+
active: true,
330+
date: new \DateTimeImmutable('1982-01-01')
331+
);
332+
// ['ESP', 'ESB']
333+
334+
// check if a currency is valid today for a country
335+
$isOk = Currencies::isValidInCountry('CH', 'CHF');
336+
// true
337+
338+
// check if a currency is valid in any country on a specific date
339+
$isGlobal = Currencies::isValidInAnyCountry(
340+
'USD',
341+
legalTender: true,
342+
active: true,
343+
date: new \DateTimeImmutable('2005-01-01')
344+
);
345+
// true
346+
347+
Note that some currencies (especially non-legal-tender ones) do not have validity
348+
ranges defined. In such cases, a ``RuntimeException`` will be thrown. In addition,
349+
an ``InvalidArgumentException`` will be thrown if the specified currency is invalid.
350+
351+
.. versionadded:: 7.4
352+
353+
The ``forCountry()``, ``isValidInCountry()`` and ``isValidInAnyCountry()``
354+
methods were introduced in Symfony 7.4.
355+
310356
.. _component-intl-timezones:
311357

312358
Timezones
@@ -425,6 +471,7 @@ Learn more
425471
.. _`ISO 3166-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
426472
.. _`ISO 3166-1 alpha-3`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
427473
.. _`ISO 3166-1 numeric`: https://en.wikipedia.org/wiki/ISO_3166-1_numeric
474+
.. _`legal tender`: https://en.wikipedia.org/wiki/Legal_tender
428475
.. _`UTC/GMT time offsets`: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
429476
.. _`daylight saving time (DST)`: https://en.wikipedia.org/wiki/Daylight_saving_time
430477
.. _`ISO 639-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_639-1

0 commit comments

Comments
 (0)