1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Country Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Country Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Rinvex\Country; |
19
|
|
|
|
20
|
|
|
use Closure; |
21
|
|
|
|
22
|
|
|
class CountryLoader |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The countries array. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected static $countries; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the country by it's ISO 3166-1 alpha-2. |
33
|
|
|
* |
34
|
|
|
* @param string $key |
35
|
|
|
* @param bool $hydrate |
36
|
|
|
* |
37
|
|
|
* @return \Rinvex\Country\Country|array |
38
|
|
|
*/ |
39
|
|
|
public static function country($key, $hydrate = true) |
40
|
|
|
{ |
41
|
|
|
$key = mb_strtolower($key); |
42
|
|
|
|
43
|
|
|
if (! isset(static::$countries[$key])) { |
44
|
|
|
static::$countries[$key] = json_decode(static::getFile(__DIR__.'/../resources/data/'.$key.'.json'), true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $hydrate ? new Country(static::$countries[$key]) : static::$countries[$key]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get all countries short-listed. |
52
|
|
|
* |
53
|
|
|
* @param bool $longlist |
54
|
|
|
* @param bool $hydrate |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public static function countries($longlist = false, $hydrate = false) |
59
|
|
|
{ |
60
|
|
|
$list = $longlist ? 'longlist' : 'shortlist'; |
61
|
|
|
|
62
|
|
|
if (! isset(static::$countries[$list])) { |
63
|
|
|
static::$countries[$list] = json_decode(static::getFile(__DIR__.'/../resources/data/'.$list.'.json'), true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $hydrate ? array_map(function ($country) { |
67
|
|
|
return new Country($country); |
68
|
|
|
}, static::$countries[$list]) : static::$countries[$list]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Filter items by the given key value pair. |
73
|
|
|
* |
74
|
|
|
* @param string $key |
75
|
|
|
* @param mixed $operator |
76
|
|
|
* @param mixed $value |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public static function where($key, $operator, $value = null) |
81
|
|
|
{ |
82
|
|
|
if (func_num_args() === 2) { |
83
|
|
|
$value = $operator; |
84
|
|
|
$operator = '='; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (! isset(static::$countries['longlist'])) { |
88
|
|
|
static::$countries['longlist'] = json_decode(static::getFile(__DIR__.'/../resources/data/longlist.json'), true); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return static::filter(static::$countries['longlist'], static::operatorForWhere($key, $operator, $value)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get an operator checker callback. |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @param string $operator |
99
|
|
|
* @param mixed $value |
100
|
|
|
* |
101
|
|
|
* @return \Closure |
102
|
|
|
*/ |
103
|
|
|
protected static function operatorForWhere($key, $operator, $value) |
104
|
|
|
{ |
105
|
|
|
return function ($item) use ($key, $operator, $value) { |
106
|
|
|
$retrieved = static::get($item, $key); |
107
|
|
|
|
108
|
|
|
switch ($operator) { |
109
|
|
|
default: |
110
|
|
|
case '=': |
111
|
|
|
case '==': return $retrieved == $value; |
|
|
|
|
112
|
|
|
case '!=': |
113
|
|
|
case '<>': return $retrieved != $value; |
|
|
|
|
114
|
|
|
case '<': return $retrieved < $value; |
|
|
|
|
115
|
|
|
case '>': return $retrieved > $value; |
|
|
|
|
116
|
|
|
case '<=': return $retrieved <= $value; |
|
|
|
|
117
|
|
|
case '>=': return $retrieved >= $value; |
|
|
|
|
118
|
|
|
case '===': return $retrieved === $value; |
|
|
|
|
119
|
|
|
case '!==': return $retrieved !== $value; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
}; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Run a filter over each of the items. |
126
|
|
|
* |
127
|
|
|
* @param array $items |
128
|
|
|
* @param callable|null $callback |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
protected static function filter($items, callable $callback = null) |
133
|
|
|
{ |
134
|
|
|
if ($callback) { |
135
|
|
|
return array_filter($items, $callback, ARRAY_FILTER_USE_BOTH); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return array_filter($items); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get an item from an array or object using "dot" notation. |
143
|
|
|
* |
144
|
|
|
* @param mixed $target |
145
|
|
|
* @param string|array $key |
146
|
|
|
* @param mixed $default |
147
|
|
|
* |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
protected static function get($target, $key, $default = null) |
151
|
|
|
{ |
152
|
|
|
if (is_null($key)) { |
153
|
|
|
return $target; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$key = is_array($key) ? $key : explode('.', $key); |
157
|
|
|
|
158
|
|
|
while (($segment = array_shift($key)) !== null) { |
159
|
|
|
if ($segment === '*') { |
160
|
|
|
if (! is_array($target)) { |
161
|
|
|
return $default instanceof Closure ? $default() : $default; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$result = static::pluck($target, $key); |
165
|
|
|
|
166
|
|
|
return in_array('*', $key) ? static::collapse($result) : $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if (is_array($target) && array_key_exists($segment, $target)) { |
170
|
|
|
$target = $target[$segment]; |
171
|
|
|
} elseif (is_object($target) && isset($target->{$segment})) { |
172
|
|
|
$target = $target->{$segment}; |
173
|
|
|
} else { |
174
|
|
|
return $default instanceof Closure ? $default() : $default; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $target; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Pluck an array of values from an array. |
183
|
|
|
* |
184
|
|
|
* @param array $array |
185
|
|
|
* @param string|array $value |
186
|
|
|
* @param string|array|null $key |
187
|
|
|
* |
188
|
|
|
* @return array |
189
|
|
|
*/ |
190
|
|
|
protected static function pluck($array, $value, $key = null) |
191
|
|
|
{ |
192
|
|
|
$results = []; |
193
|
|
|
|
194
|
|
|
$value = is_string($value) ? explode('.', $value) : $value; |
195
|
|
|
|
196
|
|
|
$key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
197
|
|
|
|
198
|
|
|
foreach ($array as $item) { |
199
|
|
|
$itemValue = static::get($item, $value); |
200
|
|
|
|
201
|
|
|
// If the key is "null", we will just append the value to the array and keep |
202
|
|
|
// looping. Otherwise we will key the array using the value of the key we |
203
|
|
|
// received from the developer. Then we'll return the final array form. |
204
|
|
|
if (is_null($key)) { |
205
|
|
|
$results[] = $itemValue; |
206
|
|
|
} else { |
207
|
|
|
$itemKey = static::get($item, $key); |
208
|
|
|
|
209
|
|
|
$results[$itemKey] = $itemValue; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $results; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Collapse an array of arrays into a single array. |
218
|
|
|
* |
219
|
|
|
* @param array $array |
220
|
|
|
* |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
protected static function collapse($array) |
224
|
|
|
{ |
225
|
|
|
$results = []; |
226
|
|
|
|
227
|
|
|
foreach ($array as $values) { |
228
|
|
|
if (! is_array($values)) { |
229
|
|
|
continue; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$results = array_merge($results, $values); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $results; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get contents of the given file path. |
240
|
|
|
* |
241
|
|
|
* @param string $filePath |
242
|
|
|
* |
243
|
|
|
* @throws \Rinvex\Country\CountryLoaderException |
244
|
|
|
* |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
|
|
protected static function getFile($filePath) |
248
|
|
|
{ |
249
|
|
|
if (! file_exists($filePath)) { |
250
|
|
|
throw CountryLoaderException::invalidCountry(); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return file_get_contents($filePath); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.