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
|
268 |
|
public function __construct($key, array $data) |
89
|
|
|
{ |
90
|
268 |
|
$this->setKey($key); |
91
|
268 |
|
$this->setName($data['name']); |
92
|
268 |
|
$this->setScript($data['script']); |
93
|
268 |
|
$this->setDirection($data['dir']); |
94
|
268 |
|
$this->setNative($data['native']); |
95
|
268 |
|
$this->setRegional($data['regional'] ?? ''); |
96
|
268 |
|
} |
97
|
|
|
|
98
|
|
|
/* ----------------------------------------------------------------- |
99
|
|
|
| Getters & Setters |
100
|
|
|
| ----------------------------------------------------------------- |
101
|
|
|
*/ |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get local key. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
268 |
|
public function key() |
109
|
|
|
{ |
110
|
268 |
|
return $this->key; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set locale key. |
115
|
|
|
* |
116
|
|
|
* @param string $key |
117
|
|
|
* |
118
|
|
|
* @return self |
119
|
|
|
*/ |
120
|
268 |
|
private function setKey($key) |
121
|
|
|
{ |
122
|
268 |
|
$this->key = $key; |
123
|
268 |
|
$this->setDefault(); |
124
|
|
|
|
125
|
268 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get locale name. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
8 |
|
public function name() |
134
|
|
|
{ |
135
|
8 |
|
return $this->name; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set name. |
140
|
|
|
* |
141
|
|
|
* @param string $name |
142
|
|
|
* |
143
|
|
|
* @return self |
144
|
|
|
*/ |
145
|
268 |
|
private function setName($name) |
146
|
|
|
{ |
147
|
268 |
|
$this->name = $name; |
148
|
|
|
|
149
|
268 |
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get locale Script. |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
8 |
|
public function script() |
158
|
|
|
{ |
159
|
8 |
|
return $this->script; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Set Script. |
164
|
|
|
* |
165
|
|
|
* @param string $script |
166
|
|
|
* |
167
|
|
|
* @return self |
168
|
|
|
*/ |
169
|
268 |
|
private function setScript($script) |
170
|
|
|
{ |
171
|
268 |
|
$this->script = $script; |
172
|
|
|
|
173
|
268 |
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Get locale direction. |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
12 |
|
public function direction() |
182
|
|
|
{ |
183
|
12 |
|
if (empty($this->direction)) { |
184
|
2 |
|
$this->setDirection( |
185
|
2 |
|
in_array($this->script, ['Arab', 'Hebr', 'Mong', 'Tfng', 'Thaa']) ? self::DIRECTION_RIGHT_TO_LEFT : self::DIRECTION_LEFT_TO_RIGHT |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
12 |
|
return $this->direction; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set Direction. |
194
|
|
|
* |
195
|
|
|
* @param string $direction |
196
|
|
|
* |
197
|
|
|
* @return self |
198
|
|
|
*/ |
199
|
268 |
|
private function setDirection($direction) |
200
|
|
|
{ |
201
|
268 |
|
if ( ! empty($direction)) |
202
|
268 |
|
$this->direction = strtolower($direction); |
203
|
|
|
|
204
|
268 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Get locale native. |
209
|
|
|
* |
210
|
|
|
* @return string |
211
|
|
|
*/ |
212
|
12 |
|
public function native() |
213
|
|
|
{ |
214
|
12 |
|
return $this->native; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set Native. |
219
|
|
|
* |
220
|
|
|
* @param string $native |
221
|
|
|
* |
222
|
|
|
* @return self |
223
|
|
|
*/ |
224
|
268 |
|
private function setNative($native) |
225
|
|
|
{ |
226
|
268 |
|
$this->native = $native; |
227
|
|
|
|
228
|
268 |
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get locale regional. |
233
|
|
|
* |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
268 |
|
public function regional() |
237
|
|
|
{ |
238
|
268 |
|
return $this->regional; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set Regional. |
243
|
|
|
* |
244
|
|
|
* @param string $regional |
245
|
|
|
* |
246
|
|
|
* @return self |
247
|
|
|
*/ |
248
|
268 |
|
private function setRegional($regional) |
249
|
|
|
{ |
250
|
268 |
|
$this->regional = $regional; |
251
|
|
|
|
252
|
268 |
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Check if it is a default locale. |
257
|
|
|
* |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
2 |
|
public function isDefault() |
261
|
|
|
{ |
262
|
2 |
|
return $this->default; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set locale default. |
267
|
|
|
* |
268
|
|
|
* @return self |
269
|
|
|
*/ |
270
|
268 |
|
private function setDefault() |
271
|
|
|
{ |
272
|
268 |
|
$this->default = ($this->key === config('app.locale')); |
273
|
|
|
|
274
|
268 |
|
return $this; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/* ----------------------------------------------------------------- |
278
|
|
|
| Main Methods |
279
|
|
|
| ----------------------------------------------------------------- |
280
|
|
|
*/ |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Create Locale instance. |
284
|
|
|
* |
285
|
|
|
* @param string $key |
286
|
|
|
* @param array $data |
287
|
|
|
* |
288
|
|
|
* @return self |
289
|
|
|
*/ |
290
|
268 |
|
public static function make($key, array $data) |
291
|
|
|
{ |
292
|
268 |
|
return new self($key, $data); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/* ----------------------------------------------------------------- |
296
|
|
|
| Other Methods |
297
|
|
|
| ----------------------------------------------------------------- |
298
|
|
|
*/ |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Get the locale entity as an array. |
302
|
|
|
* |
303
|
|
|
* @return array |
304
|
|
|
*/ |
305
|
4 |
|
public function toArray() |
306
|
|
|
{ |
307
|
|
|
return [ |
308
|
4 |
|
'key' => $this->key(), |
309
|
4 |
|
'name' => $this->name(), |
310
|
4 |
|
'script' => $this->script(), |
311
|
4 |
|
'dir' => $this->direction(), |
312
|
4 |
|
'native' => $this->native(), |
313
|
4 |
|
'regional' => $this->regional(), |
314
|
|
|
]; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Convert the object to its JSON representation. |
319
|
|
|
* |
320
|
|
|
* @param int $options |
321
|
|
|
* |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
2 |
|
public function toJson($options = 0) |
325
|
|
|
{ |
326
|
2 |
|
return json_encode($this->toArray(), $options); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Specify data which should be serialized to JSON. |
331
|
|
|
* |
332
|
|
|
* @return array |
333
|
|
|
*/ |
334
|
2 |
|
public function jsonSerialize() |
335
|
|
|
{ |
336
|
2 |
|
return $this->toArray(); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|