|
1
|
|
|
<?php namespace Arcanedev\Localization\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
4
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
5
|
|
|
use JsonSerializable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Locale |
|
9
|
|
|
* |
|
10
|
|
|
* @package Arcanedev\Localization\Entities |
|
11
|
|
|
* @author ARCANEDEV <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Locale implements Arrayable, Jsonable, JsonSerializable |
|
14
|
|
|
{ |
|
15
|
|
|
/* ----------------------------------------------------------------- |
|
16
|
|
|
| Constants |
|
17
|
|
|
| ----------------------------------------------------------------- |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
const DIRECTION_LEFT_TO_RIGHT = 'ltr'; |
|
21
|
|
|
const DIRECTION_RIGHT_TO_LEFT = 'rtl'; |
|
22
|
|
|
|
|
23
|
|
|
/* ----------------------------------------------------------------- |
|
24
|
|
|
| Properties |
|
25
|
|
|
| ----------------------------------------------------------------- |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Locale key. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $key; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Locale name. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $name; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Locale script. |
|
44
|
|
|
* |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
private $script; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Locale direction. |
|
51
|
|
|
* |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $direction; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Locale native. |
|
58
|
|
|
* |
|
59
|
|
|
* @var string |
|
60
|
|
|
*/ |
|
61
|
|
|
private $native; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Locale regional. |
|
65
|
|
|
* |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
private $regional; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Default locale. |
|
72
|
|
|
* |
|
73
|
|
|
* @var bool |
|
74
|
|
|
*/ |
|
75
|
|
|
private $default = false; |
|
76
|
|
|
|
|
77
|
|
|
/* ----------------------------------------------------------------- |
|
78
|
|
|
| Constructor |
|
79
|
|
|
| ----------------------------------------------------------------- |
|
80
|
|
|
*/ |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Create Locale instance. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* @param array $data |
|
87
|
|
|
*/ |
|
88
|
381 |
|
public function __construct($key, array $data) |
|
89
|
|
|
{ |
|
90
|
381 |
|
$this->setKey($key); |
|
91
|
381 |
|
$this->setName($data['name']); |
|
92
|
381 |
|
$this->setScript($data['script']); |
|
93
|
381 |
|
$this->setDirection($data['dir']); |
|
94
|
381 |
|
$this->setNative($data['native']); |
|
95
|
381 |
|
$this->setRegional($data['regional'] ?? ''); |
|
96
|
381 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
/* ----------------------------------------------------------------- |
|
99
|
|
|
| Getters & Setters |
|
100
|
|
|
| ----------------------------------------------------------------- |
|
101
|
|
|
*/ |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get local key. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
381 |
|
public function key() |
|
109
|
|
|
{ |
|
110
|
381 |
|
return $this->key; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set locale key. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $key |
|
117
|
|
|
* |
|
118
|
|
|
* @return self |
|
119
|
|
|
*/ |
|
120
|
381 |
|
private function setKey($key) |
|
121
|
|
|
{ |
|
122
|
381 |
|
$this->key = $key; |
|
123
|
381 |
|
$this->setDefault(); |
|
124
|
|
|
|
|
125
|
381 |
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get locale name. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
12 |
|
public function name() |
|
134
|
|
|
{ |
|
135
|
12 |
|
return $this->name; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set name. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $name |
|
142
|
|
|
* |
|
143
|
|
|
* @return self |
|
144
|
|
|
*/ |
|
145
|
381 |
|
private function setName($name) |
|
146
|
|
|
{ |
|
147
|
381 |
|
$this->name = $name; |
|
148
|
|
|
|
|
149
|
381 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get locale Script. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
12 |
|
public function script() |
|
158
|
|
|
{ |
|
159
|
12 |
|
return $this->script; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Set Script. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $script |
|
166
|
|
|
* |
|
167
|
|
|
* @return self |
|
168
|
|
|
*/ |
|
169
|
381 |
|
private function setScript($script) |
|
170
|
|
|
{ |
|
171
|
381 |
|
$this->script = $script; |
|
172
|
|
|
|
|
173
|
381 |
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get locale direction. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string |
|
180
|
|
|
*/ |
|
181
|
18 |
|
public function direction() |
|
182
|
|
|
{ |
|
183
|
18 |
|
if (empty($this->direction)) { |
|
184
|
3 |
|
$rtlScripts = ['Arab', 'Hebr', 'Mong', 'Tfng', 'Thaa']; |
|
185
|
|
|
|
|
186
|
3 |
|
$this->setDirection( |
|
187
|
3 |
|
in_array($this->script, $rtlScripts) |
|
188
|
|
|
? self::DIRECTION_RIGHT_TO_LEFT |
|
189
|
3 |
|
: self::DIRECTION_LEFT_TO_RIGHT |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
18 |
|
return $this->direction; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Set Direction. |
|
198
|
|
|
* |
|
199
|
|
|
* @param string $direction |
|
200
|
|
|
* |
|
201
|
|
|
* @return self |
|
202
|
|
|
*/ |
|
203
|
381 |
|
private function setDirection($direction) |
|
204
|
|
|
{ |
|
205
|
381 |
|
if ( ! empty($direction)) |
|
206
|
381 |
|
$this->direction = strtolower($direction); |
|
207
|
|
|
|
|
208
|
381 |
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Get locale native. |
|
213
|
|
|
* |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
18 |
|
public function native() |
|
217
|
|
|
{ |
|
218
|
18 |
|
return $this->native; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Set Native. |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $native |
|
225
|
|
|
* |
|
226
|
|
|
* @return self |
|
227
|
|
|
*/ |
|
228
|
381 |
|
private function setNative($native) |
|
229
|
|
|
{ |
|
230
|
381 |
|
$this->native = $native; |
|
231
|
|
|
|
|
232
|
381 |
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get locale regional. |
|
237
|
|
|
* |
|
238
|
|
|
* @return string |
|
239
|
|
|
*/ |
|
240
|
381 |
|
public function regional() |
|
241
|
|
|
{ |
|
242
|
381 |
|
return $this->regional; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Set Regional. |
|
247
|
|
|
* |
|
248
|
|
|
* @param string $regional |
|
249
|
|
|
* |
|
250
|
|
|
* @return self |
|
251
|
|
|
*/ |
|
252
|
381 |
|
private function setRegional($regional) |
|
253
|
|
|
{ |
|
254
|
381 |
|
$this->regional = $regional; |
|
255
|
|
|
|
|
256
|
381 |
|
return $this; |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Check if it is a default locale. |
|
261
|
|
|
* |
|
262
|
|
|
* @return bool |
|
263
|
|
|
*/ |
|
264
|
3 |
|
public function isDefault() |
|
265
|
|
|
{ |
|
266
|
3 |
|
return $this->default; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Set locale default. |
|
271
|
|
|
* |
|
272
|
|
|
* @return self |
|
273
|
|
|
*/ |
|
274
|
381 |
|
private function setDefault() |
|
275
|
|
|
{ |
|
276
|
381 |
|
$this->default = ($this->key === config('app.locale')); |
|
277
|
|
|
|
|
278
|
381 |
|
return $this; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
/* ----------------------------------------------------------------- |
|
282
|
|
|
| Main Methods |
|
283
|
|
|
| ----------------------------------------------------------------- |
|
284
|
|
|
*/ |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Create Locale instance. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $key |
|
290
|
|
|
* @param array $data |
|
291
|
|
|
* |
|
292
|
|
|
* @return self |
|
293
|
|
|
*/ |
|
294
|
381 |
|
public static function make($key, array $data) |
|
295
|
|
|
{ |
|
296
|
381 |
|
return new self($key, $data); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/* ----------------------------------------------------------------- |
|
300
|
|
|
| Other Methods |
|
301
|
|
|
| ----------------------------------------------------------------- |
|
302
|
|
|
*/ |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* Get the locale entity as an array. |
|
306
|
|
|
* |
|
307
|
|
|
* @return array |
|
308
|
|
|
*/ |
|
309
|
6 |
|
public function toArray() |
|
310
|
|
|
{ |
|
311
|
|
|
return [ |
|
312
|
6 |
|
'key' => $this->key(), |
|
313
|
6 |
|
'name' => $this->name(), |
|
314
|
6 |
|
'script' => $this->script(), |
|
315
|
6 |
|
'dir' => $this->direction(), |
|
316
|
6 |
|
'native' => $this->native(), |
|
317
|
6 |
|
'regional' => $this->regional(), |
|
318
|
|
|
]; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Convert the object to its JSON representation. |
|
323
|
|
|
* |
|
324
|
|
|
* @param int $options |
|
325
|
|
|
* |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
3 |
|
public function toJson($options = 0) |
|
329
|
|
|
{ |
|
330
|
3 |
|
return json_encode($this->toArray(), $options); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Specify data which should be serialized to JSON. |
|
335
|
|
|
* |
|
336
|
|
|
* @return array |
|
337
|
|
|
*/ |
|
338
|
3 |
|
public function jsonSerialize() |
|
339
|
|
|
{ |
|
340
|
3 |
|
return $this->toArray(); |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|