1
|
|
|
<?php namespace Arcanedev\Localization\Entities; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class LocaleCollection |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\Localization\Entities |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class LocaleCollection extends Collection |
12
|
|
|
{ |
13
|
|
|
/* ----------------------------------------------------------------- |
14
|
|
|
| Properties |
15
|
|
|
| ----------------------------------------------------------------- |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Supported locales. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $supported = []; |
24
|
|
|
|
25
|
|
|
/* ----------------------------------------------------------------- |
26
|
|
|
| Getters & Setters |
27
|
|
|
| ----------------------------------------------------------------- |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set supported locales keys. |
32
|
|
|
* |
33
|
|
|
* @param array $supported |
34
|
|
|
* |
35
|
|
|
* @return self |
36
|
|
|
*/ |
37
|
8 |
|
public function setSupportedKeys(array $supported) |
38
|
|
|
{ |
39
|
8 |
|
$this->supported = $supported; |
40
|
|
|
|
41
|
8 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/* ----------------------------------------------------------------- |
45
|
|
|
| Main Methods |
46
|
|
|
| ----------------------------------------------------------------- |
47
|
|
|
*/ |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the first locale from the collection. |
51
|
|
|
* |
52
|
|
|
* @param callable|null $callback |
53
|
|
|
* @param mixed $default |
54
|
|
|
* |
55
|
|
|
* @return \Arcanedev\Localization\Entities\Locale|mixed |
56
|
|
|
*/ |
57
|
4 |
|
public function first(callable $callback = null, $default = null) |
58
|
|
|
{ |
59
|
4 |
|
return parent::first($callback, $default); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get supported locales collection. |
64
|
|
|
* |
65
|
|
|
* @return \Arcanedev\Localization\Entities\SupportedLocaleCollection |
66
|
|
|
*/ |
67
|
6 |
|
public function getSupported() |
68
|
|
|
{ |
69
|
6 |
|
return new SupportedLocaleCollection( |
70
|
6 |
|
$this->filter(function(Locale $locale) { |
71
|
6 |
|
return in_array($locale->key(), $this->supported); |
72
|
6 |
|
}) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Transform the collection with only locale's native name. |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Support\Collection |
80
|
|
|
*/ |
81
|
|
|
public function toNative() |
82
|
|
|
{ |
83
|
2 |
|
return $this->map(function (Locale $locale) { |
84
|
2 |
|
return $locale->native(); |
85
|
2 |
|
})->toBase(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Load from config. |
90
|
|
|
* |
91
|
|
|
* @return self |
92
|
|
|
*/ |
93
|
4 |
|
public function loadFromConfig() |
94
|
|
|
{ |
95
|
4 |
|
$this->loadFromArray( |
96
|
4 |
|
config('localization.locales', []) |
97
|
|
|
); |
98
|
4 |
|
$this->setSupportedKeys( |
99
|
4 |
|
config('localization.supported-locales', []) |
100
|
|
|
); |
101
|
|
|
|
102
|
4 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Load locales from array. |
107
|
|
|
* |
108
|
|
|
* @param array $locales |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
254 |
|
public function loadFromArray(array $locales) |
113
|
|
|
{ |
114
|
254 |
|
$this->items = []; |
115
|
|
|
|
116
|
254 |
|
foreach ($locales as $key => $locale) { |
117
|
254 |
|
$this->put($key, Locale::make($key, $locale)); |
118
|
|
|
} |
119
|
|
|
|
120
|
254 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|