1 | <?php |
||
2 | /** |
||
3 | * @link https://dukt.net/analytics/ |
||
4 | * @copyright Copyright (c) 2022, Dukt |
||
5 | * @license https://github.com/dukt/analytics/blob/master/LICENSE.md |
||
6 | */ |
||
7 | |||
8 | namespace dukt\analytics\services; |
||
9 | |||
10 | use Craft; |
||
11 | use yii\base\Component; |
||
12 | |||
13 | class Geo extends Component |
||
14 | { |
||
15 | // Public Methods |
||
16 | // ========================================================================= |
||
17 | |||
18 | /** |
||
19 | * Get continents. |
||
20 | * |
||
21 | * @return array |
||
22 | */ |
||
23 | public function getContinents(): array |
||
24 | { |
||
25 | return $this->_getData('continents'); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * Get subcontinents. |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | public function getSubContinents(): array |
||
34 | { |
||
35 | return $this->_getData('subContinents'); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get Continent Code |
||
40 | * |
||
41 | * @param string $label |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function getContinentCode($label) |
||
46 | { |
||
47 | foreach ($this->_getData('continents') as $continent) { |
||
48 | if ($continent['label'] === $label) { |
||
49 | return $continent['code']; |
||
50 | } |
||
51 | } |
||
52 | |||
53 | return null; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Get Sub-Continent Code |
||
58 | * |
||
59 | * @param string $label |
||
60 | * |
||
61 | * @return mixed |
||
62 | */ |
||
63 | public function getSubContinentCode($label) |
||
64 | { |
||
65 | foreach ($this->_getData('subContinents') as $subContinent) { |
||
66 | if ($subContinent['label'] === $label) { |
||
67 | return $subContinent['code']; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return null; |
||
72 | } |
||
73 | |||
74 | // Private Methods |
||
75 | // ========================================================================= |
||
76 | |||
77 | /** |
||
78 | * Get Data |
||
79 | * |
||
80 | * @param $name |
||
81 | * |
||
82 | * @return array |
||
83 | * @internal param string $label |
||
84 | * |
||
85 | */ |
||
86 | private function _getData($name): array |
||
87 | { |
||
88 | $jsonData = file_get_contents(Craft::getAlias('@dukt/analytics/etc/data/'.$name.'.json')); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
89 | |||
90 | return json_decode($jsonData, true); |
||
91 | } |
||
92 | } |
||
93 |