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