1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Countries\Package\Data; |
4
|
|
|
|
5
|
|
|
use PragmaRX\Countries\Package\Services\Helper; |
6
|
|
|
use PragmaRX\Countries\Package\Services\Hydrator; |
7
|
|
|
use Psr\SimpleCache\CacheInterface as CacheContract; |
8
|
|
|
use PragmaRX\Countries\Package\Services\Cache\Service as Cache; |
9
|
|
|
|
10
|
|
|
class Repository |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Timezones. |
14
|
|
|
* |
15
|
|
|
* @var |
16
|
|
|
*/ |
17
|
|
|
public $timezones; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Countries json. |
21
|
|
|
* |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
public $countriesJson; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Countries. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $countries = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Hydrator. |
35
|
|
|
* |
36
|
|
|
* @var Hydrator |
37
|
|
|
*/ |
38
|
|
|
public $hydrator; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Helper. |
42
|
|
|
* |
43
|
|
|
* @var Helper |
44
|
|
|
*/ |
45
|
|
|
private $helper; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Cache. |
49
|
|
|
* |
50
|
|
|
* @var Cache |
51
|
|
|
*/ |
52
|
|
|
private $cache; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var object |
56
|
|
|
*/ |
57
|
|
|
private $config; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Repository constructor. |
61
|
|
|
* |
62
|
|
|
* @param CacheContract $cache |
63
|
|
|
* @param Hydrator $hydrator |
64
|
|
|
* @param Helper $helper |
65
|
|
|
* @param object $config |
66
|
|
|
*/ |
67
|
|
|
public function __construct(CacheContract $cache, Hydrator $hydrator, Helper $helper, $config) |
68
|
|
|
{ |
69
|
|
|
$this->cache = $cache; |
|
|
|
|
70
|
|
|
|
71
|
|
|
$this->hydrator = $hydrator; |
72
|
|
|
|
73
|
|
|
$this->helper = $helper; |
74
|
|
|
|
75
|
|
|
$this->config = $config; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Call magic method. |
80
|
|
|
* |
81
|
|
|
* @param $name |
82
|
|
|
* @param array $arguments |
83
|
|
|
* @return mixed |
84
|
|
|
*/ |
85
|
|
|
public function __call($name, array $arguments = []) |
86
|
|
|
{ |
87
|
|
|
return \call_user_func_array([$this->all(), $name], $arguments); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Call a method currencies collection. |
92
|
|
|
* |
93
|
|
|
* @param $name |
94
|
|
|
* @param $arguments |
95
|
|
|
* @return bool|mixed |
96
|
|
|
*/ |
97
|
|
|
public function call($name, $arguments) |
98
|
|
|
{ |
99
|
|
|
if ($value = $this->cache->get($cacheKey = $this->cache->makeKey([$name, $arguments]))) { |
100
|
|
|
return $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$result = \call_user_func_array([$this, $name], $arguments); |
104
|
|
|
|
105
|
|
|
if ($this->config->get('hydrate.before')) { |
106
|
|
|
$result = $this->hydrator->hydrate($result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->cache->set($cacheKey, $result, $this->config->get('cache.duration')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return Helper |
114
|
|
|
*/ |
115
|
|
|
public function getHelper() |
116
|
|
|
{ |
117
|
|
|
return $this->helper; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Hydrator getter. |
122
|
|
|
* |
123
|
|
|
* @return Hydrator |
124
|
|
|
*/ |
125
|
|
|
public function getHydrator() |
126
|
|
|
{ |
127
|
|
|
return $this->hydrator; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Boot the repository. |
132
|
|
|
* @return static |
133
|
|
|
*/ |
134
|
|
|
public function boot() |
135
|
|
|
{ |
136
|
|
|
$this->loadCountries(); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Load countries. |
143
|
|
|
* |
144
|
|
|
* @return \PragmaRX\Coollection\Package\Coollection |
145
|
|
|
*/ |
146
|
|
|
public function loadCountries() |
147
|
|
|
{ |
148
|
|
|
$this->countriesJson = $this->loadCountriesJson(); |
149
|
|
|
|
150
|
|
|
$overload = $this->helper->loadJsonFiles($this->helper->dataDir('countries/overload'))->mapWithKeys(function ($country, $code) { |
151
|
|
|
return [upper($code) => $country]; |
152
|
|
|
}); |
153
|
|
|
|
154
|
|
|
$this->countriesJson = $this->countriesJson->overwrite($overload); |
155
|
|
|
|
156
|
|
|
return $this->countriesJson; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Load countries json file. |
161
|
|
|
* @return \PragmaRX\Coollection\Package\Coollection |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
public function loadCountriesJson() |
165
|
|
|
{ |
166
|
|
|
$data = $this->helper->loadJson( |
167
|
|
|
$fileName = $this->helper->dataDir('countries/default/_all_countries.json') |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
if ($data->isEmpty()) { |
171
|
|
|
throw new \Exception("Could not load countries from {$fileName}"); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $data; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Load currency json file. |
179
|
|
|
* |
180
|
|
|
* @param $code |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function loadCurrenciesForCountry($code) |
184
|
|
|
{ |
185
|
|
|
$currencies = $this->helper->loadJson( |
186
|
|
|
$this->helper->dataDir('currencies/default/'.strtolower($code).'.json') |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
return $currencies; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Get all countries. |
194
|
|
|
* |
195
|
|
|
* @return \PragmaRX\Coollection\Package\Coollection |
196
|
|
|
*/ |
197
|
|
|
public function all() |
198
|
|
|
{ |
199
|
|
|
return countriesCollect($this->countriesJson); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get all currencies. |
204
|
|
|
* |
205
|
|
|
* @return \PragmaRX\Coollection\Package\Coollection |
206
|
|
|
*/ |
207
|
|
|
public function currencies() |
208
|
|
|
{ |
209
|
|
|
$currencies = $this->helper->loadJsonFiles($this->helper->dataDir('currencies/default'))->mapWithKeys(function ($country, $code) { |
210
|
|
|
return [upper($code) => $country]; |
211
|
|
|
}); |
212
|
|
|
|
213
|
|
|
$overload = $this->helper->loadJsonFiles($this->helper->dataDir('currencies/overload'))->mapWithKeys(function ($country, $code) { |
214
|
|
|
return [upper($code) => $country]; |
215
|
|
|
}); |
216
|
|
|
|
217
|
|
|
return $currencies->overwrite($overload); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Make flags array for a coutry. |
222
|
|
|
* |
223
|
|
|
* @param $country |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function makeAllFlags($country) |
227
|
|
|
{ |
228
|
|
|
return [ |
229
|
|
|
// https://www.flag-sprites.com/ |
230
|
|
|
// https://github.com/LeoColomb/flag-sprites |
231
|
|
|
'sprite' => '<span class="flag flag-'.($cca3 = strtolower($country['cca3'])).'"></span>', |
232
|
|
|
|
233
|
|
|
// https://github.com/lipis/flag-icon-css |
234
|
|
|
'flag-icon' => '<span class="flag-icon flag-icon-'.($iso_a2 = strtolower($country['iso_a2'])).'"></span>', |
235
|
|
|
'flag-icon-squared' => '<span class="flag-icon flag-icon-'.$iso_a2.' flag-icon-squared"></span>', |
236
|
|
|
|
237
|
|
|
// https://github.com/lafeber/world-flags-sprite |
238
|
|
|
'world-flags-sprite' => '<span class="flag '.$cca3.'"></span>', |
239
|
|
|
|
240
|
|
|
// Internal svg file |
241
|
|
|
'svg' => $this->getFlagSvg($country['cca3']), |
242
|
|
|
|
243
|
|
|
'svg_path' => $this->getFlagSvgPath($country['cca3']), |
244
|
|
|
]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Read the flag SVG file. |
249
|
|
|
* |
250
|
|
|
* @param $country |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getFlagSvg($country) |
254
|
|
|
{ |
255
|
|
|
return $this->helper->loadFile( |
256
|
|
|
$this->getFlagSvgPath($country) |
257
|
|
|
); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get the SVG file path. |
262
|
|
|
* |
263
|
|
|
* @param $country |
264
|
|
|
* @return string |
265
|
|
|
*/ |
266
|
|
|
public function getFlagSvgPath($country) |
267
|
|
|
{ |
268
|
|
|
return $this->helper->dataDir('flags/'.strtolower($country).'.svg'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get country geometry. |
273
|
|
|
* |
274
|
|
|
* @param $country |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getGeometry($country) |
278
|
|
|
{ |
279
|
|
|
return $this->helper->loadFile( |
280
|
|
|
$this->helper->dataDir('geo/'.strtolower($country).'.geo.json') |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get country topology. |
286
|
|
|
* |
287
|
|
|
* @param $country |
288
|
|
|
* @return string |
289
|
|
|
*/ |
290
|
|
|
public function getTopology($country) |
291
|
|
|
{ |
292
|
|
|
return $this->helper->loadFile( |
293
|
|
|
$this->helper->dataDir('topo/'.strtolower($country).'.topo.json') |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Hydrate a country element. |
299
|
|
|
* |
300
|
|
|
* @param $collection |
301
|
|
|
* @param null $elements |
302
|
|
|
* @return \PragmaRX\Coollection\Package\Coollection |
303
|
|
|
*/ |
304
|
|
|
public function hydrate($collection, $elements = null) |
305
|
|
|
{ |
306
|
|
|
return $this->hydrator->hydrate($collection, $elements); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Find a country timezone. |
311
|
|
|
* |
312
|
|
|
* @param $countryCode |
313
|
|
|
* @return null |
314
|
|
|
*/ |
315
|
|
|
public function findTimezones($countryCode) |
316
|
|
|
{ |
317
|
|
|
return $this->helper->loadJson( |
318
|
|
|
$this->helper->dataDir('timezones/countries/default/'.strtolower($countryCode).'.json') |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Find a country timezone. |
324
|
|
|
* |
325
|
|
|
* @param $zoneId |
326
|
|
|
* @return \PragmaRX\Coollection\Package\Coollection |
327
|
|
|
*/ |
328
|
|
|
public function findTimezoneTime($zoneId) |
329
|
|
|
{ |
330
|
|
|
return $this->helper->loadJson( |
331
|
|
|
$this->helper->dataDir("timezones/timezones/default/{$zoneId}.json") |
332
|
|
|
); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.