Skip to content

Commit a887be5

Browse files
[Intl] Document isValidInCountry(), isValidInAnyCountry(), forCountry
Close #21329
1 parent 08b8bda commit a887be5

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

components/intl.rst

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

312312
$isValidCurrency = Currencies::exists($currencyCode);
313313

314+
If the currencies need to be filtered, there are also helpers to query and
315+
validate currencies in relation to countries. These methods use ICU metadata
316+
(``tender``, ``from`` and ``to`` dates) to determine whether a currency is
317+
`legal tender`_ and/or active at a given point in time.
318+
319+
.. code-block:: php
320+
321+
use Symfony\Component\Intl\Currencies;
322+
323+
// Get the list of legal and active currencies for a country (defaults to "today")
324+
$codes = Currencies::forCountry('FR');
325+
// ['EUR']
326+
327+
// Include non-legal currencies too, and check them at a given date
328+
$codesAll = Currencies::forCountry(
329+
'ES',
330+
legalTender: null,
331+
active: true,
332+
date: new \DateTimeImmutable('1982-01-01')
333+
);
334+
// ['ESP', 'ESB']
335+
336+
// Validate a currency for a given country
337+
$isOk = Currencies::isValidInCountry('CH', 'CHF');
338+
// true
339+
340+
// Check if a currency is valid in *any* country on a specific date
341+
$isGlobal = Currencies::isValidInAnyCountry(
342+
'USD',
343+
legalTender: true,
344+
active: true,
345+
date: new \DateTimeImmutable('2005-01-01')
346+
);
347+
// true
348+
349+
Note that some currencies (especially non-legal-tender ones) do not have validity ranges defined.
350+
In such cases, a :class:`\RuntimeException` will be thrown.
351+
In addition, an :class:`\InvalidArgumentException` will be thrown if the specified currency is invalid.
352+
314353
.. _component-intl-timezones:
315354

316355
Timezones
@@ -429,6 +468,7 @@ Learn more
429468
.. _`ISO 3166-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
430469
.. _`ISO 3166-1 alpha-3`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
431470
.. _`ISO 3166-1 numeric`: https://en.wikipedia.org/wiki/ISO_3166-1_numeric
471+
.. _`legal tender`: https://en.wikipedia.org/wiki/Legal_tender
432472
.. _`UTC/GMT time offsets`: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
433473
.. _`daylight saving time (DST)`: https://en.wikipedia.org/wiki/Daylight_saving_time
434474
.. _`ISO 639-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_639-1

0 commit comments

Comments
 (0)