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