/
CountryExtension.php
50 lines (41 loc) · 1.04 KB
/
CountryExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Craue\FormFlowDemoBundle\Twig\Extension;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Intl;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2013-2020 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
class CountryExtension extends AbstractExtension {
/**
* {@inheritDoc}
*/
public function getFunctions() {
return [
new TwigFunction('country', [$this, 'getCountry']),
];
}
/**
* {@inheritDoc}
*/
public function getName() {
return 'country';
}
/**
* @param string|null $key
* @return string|null
*/
public function getCountry($key) {
if (empty($key)) {
return null;
}
// TODO remove as soon as Symfony >= 4.3 is required
if (!class_exists('Symfony\Component\Intl\Countries')) {
return Intl::getRegionBundle()->getCountryName($key, \Locale::getDefault());
}
return Countries::exists($key) ? Countries::getName($key, \Locale::getDefault()) : null;
}
}