1 | <?php |
||
13 | class CountryExtension extends \Twig_Extension |
||
14 | { |
||
15 | public function __construct() |
||
16 | { |
||
17 | if (!class_exists('\Locale')) { |
||
18 | throw new \RuntimeException('The country extension is needed to use intl-based filters.'); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | public function getFilters() |
||
31 | |||
32 | /** |
||
33 | * Return the country name using the Locale class. |
||
34 | * |
||
35 | * @param string $isoCode Country ISO 3166-1 alpha 2 code |
||
36 | * @param null|string $locale Locale code |
||
37 | * |
||
38 | * @return null|string Country name |
||
39 | */ |
||
40 | public function getCountryName($isoCode, $locale = null) |
||
41 | { |
||
42 | if ($isoCode === null) { |
||
43 | return; |
||
44 | } |
||
45 | if ($locale) { |
||
|
|||
46 | \Locale::setDefault($locale); |
||
47 | } |
||
48 | |||
49 | return Intl::getRegionBundle()->getCountryName($isoCode); |
||
50 | } |
||
51 | |||
52 | public function getName() |
||
56 | } |
||
57 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: