Skip to content

Commit c11bfa5

Browse files
Crovitche-1623javiereguiluz
authored andcommitted
[Intl] Document isValidInCountry(), isValidInAnyCountry(), forCountry()
1 parent 08b8bda commit c11bfa5

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

components/intl.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,43 @@ 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+
use Symfony\Component\Intl\Currencies;
320+
321+
// Get the list of legal and active currencies for a country (defaults to "today")
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+
// Validate a currency for a given 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 ranges defined.
348+
In such cases, a ``RuntimeException`` will be thrown.
349+
In addition, an ``InvalidArgumentException`` will be thrown if the specified currency is invalid.
350+
314351
.. _component-intl-timezones:
315352

316353
Timezones
@@ -429,6 +466,7 @@ Learn more
429466
.. _`ISO 3166-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
430467
.. _`ISO 3166-1 alpha-3`: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3
431468
.. _`ISO 3166-1 numeric`: https://en.wikipedia.org/wiki/ISO_3166-1_numeric
469+
.. _`legal tender`: https://en.wikipedia.org/wiki/Legal_tender
432470
.. _`UTC/GMT time offsets`: https://en.wikipedia.org/wiki/List_of_UTC_time_offsets
433471
.. _`daylight saving time (DST)`: https://en.wikipedia.org/wiki/Daylight_saving_time
434472
.. _`ISO 639-1 alpha-2`: https://en.wikipedia.org/wiki/ISO_639-1

0 commit comments

Comments
 (0)