1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusGeoPlugin\Twig; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusGeoPlugin\Helper\IpGeolocalizationHelperInterface; |
8
|
|
|
use Twig\Extension\AbstractExtension; |
9
|
|
|
use Twig\TwigFunction; |
10
|
|
|
|
11
|
|
|
final class GeoExtension extends AbstractExtension |
12
|
|
|
{ |
13
|
|
|
/** @var IpGeolocalizationHelperInterface */ |
14
|
|
|
private $ipGeolocalizationHelper; |
15
|
|
|
|
16
|
|
|
public function __construct( |
17
|
|
|
IpGeolocalizationHelperInterface $ipGeolocalizationHelper |
18
|
|
|
) { |
19
|
|
|
$this->ipGeolocalizationHelper = $ipGeolocalizationHelper; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return array|TwigFunction[] |
24
|
|
|
*/ |
25
|
|
|
public function getFunctions(): array |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
new TwigFunction('geo_continent_code', [$this, 'getContinentCode']), |
29
|
|
|
new TwigFunction('geo_country_code', [$this, 'getCountryCode']), |
30
|
|
|
new TwigFunction('geo_city_name', [$this, 'getCityName']), |
31
|
|
|
new TwigFunction('geo_postal_code', [$this, 'getPostalCode']) |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string|null |
37
|
|
|
*/ |
38
|
|
|
public function getContinentCode(): ?string |
39
|
|
|
{ |
40
|
|
|
return $this->ipGeolocalizationHelper->getContinentCode(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string|null |
45
|
|
|
*/ |
46
|
|
|
public function getCountryCode(): ?string |
47
|
|
|
{ |
48
|
|
|
return $this->ipGeolocalizationHelper->getCountryCode(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
|
public function getCityName(): ?string |
55
|
|
|
{ |
56
|
|
|
return $this->ipGeolocalizationHelper->getCityName(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string|null |
61
|
|
|
*/ |
62
|
|
|
public function getPostalCode(): ?string |
63
|
|
|
{ |
64
|
|
|
return $this->ipGeolocalizationHelper->getPostalCode(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getName(): string |
71
|
|
|
{ |
72
|
|
|
return 'geo'; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|