1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Helldar\LaravelLangPublisher\Support; |
4
|
|
|
|
5
|
|
|
use Helldar\LaravelLangPublisher\Concerns\Has; |
6
|
|
|
use Helldar\LaravelLangPublisher\Constants\Locales as LocalesList; |
7
|
|
|
use Helldar\LaravelLangPublisher\Facades\Config as ConfigSupport; |
8
|
|
|
use Helldar\LaravelLangPublisher\Facades\Path as PathSupport; |
9
|
|
|
use Helldar\LaravelLangPublisher\Facades\Reflection as ReflectionSupport; |
10
|
|
|
use Helldar\Support\Facades\Helpers\Ables\Arrayable; |
11
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\Directory; |
12
|
|
|
use Helldar\Support\Facades\Helpers\Filesystem\File; |
13
|
|
|
|
14
|
|
|
final class Locales |
15
|
|
|
{ |
16
|
|
|
use Has; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* List of available locations. |
20
|
|
|
* |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public function available(): array |
24
|
25 |
|
{ |
25
|
|
|
$locales = $this->all(); |
26
|
25 |
|
|
27
|
|
|
return $this->filter($locales); |
28
|
25 |
|
} |
29
|
|
|
|
30
|
25 |
|
/** |
31
|
|
|
* List of installed locations. |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function installed(): array |
36
|
|
|
{ |
37
|
|
|
return Arrayable::of() |
38
|
18 |
|
->merge($this->findJson(), $this->findPhp()) |
39
|
|
|
->filter(function ($locale) { |
40
|
18 |
|
return $this->isAvailable($locale); |
41
|
|
|
}) |
42
|
18 |
|
->unique() |
43
|
18 |
|
->sort() |
44
|
|
|
->values() |
45
|
18 |
|
->get(); |
46
|
|
|
} |
47
|
18 |
|
|
48
|
|
|
/** |
49
|
18 |
|
* Retrieving a list of protected locales. |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function protects(): array |
54
|
|
|
{ |
55
|
|
|
return Arrayable::of([ |
56
|
|
|
$this->getDefault(), |
57
|
1 |
|
$this->getFallback(), |
58
|
|
|
]) |
59
|
1 |
|
->unique() |
60
|
|
|
->get(); |
61
|
1 |
|
} |
62
|
1 |
|
|
63
|
1 |
|
/** |
64
|
|
|
* Getting a complete list of available localizations. |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public function all(): array |
69
|
|
|
{ |
70
|
|
|
$locales = ReflectionSupport::getConstants(LocalesList::class); |
71
|
|
|
|
72
|
35 |
|
return Arrayable::of($locales) |
73
|
|
|
->sort() |
74
|
35 |
|
->values() |
75
|
|
|
->get(); |
76
|
35 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks if a localization is available. |
80
|
|
|
* |
81
|
|
|
* @param string $locale |
82
|
|
|
* |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function isAvailable(string $locale): bool |
86
|
32 |
|
{ |
87
|
|
|
return $this->in($locale, $this->available()); |
88
|
32 |
|
} |
89
|
|
|
|
90
|
32 |
|
/** |
91
|
|
|
* Checks if a localization is protected. |
92
|
|
|
* |
93
|
|
|
* @param string $locale |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isProtected(string $locale): bool |
98
|
|
|
{ |
99
|
|
|
return $this->in($locale, $this->protects()); |
100
|
8 |
|
} |
101
|
|
|
|
102
|
8 |
|
/** |
103
|
|
|
* Checks if a localization is installed. |
104
|
8 |
|
* |
105
|
|
|
* @param string $locale |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function isInstalled(string $locale): bool |
110
|
|
|
{ |
111
|
|
|
return $this->in($locale, $this->installed()); |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
/** |
115
|
|
|
* Getting the default localization name. |
116
|
3 |
|
* |
117
|
|
|
* @return string |
118
|
3 |
|
*/ |
119
|
|
|
public function getDefault(): string |
120
|
|
|
{ |
121
|
|
|
return ConfigSupport::defaultLocale(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Getting the fallback localization name. |
126
|
9 |
|
* |
127
|
|
|
* @return string |
128
|
9 |
|
*/ |
129
|
|
|
public function getFallback(): string |
130
|
9 |
|
{ |
131
|
|
|
return ConfigSupport::fallbackLocale(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
protected function filter(array $locales): array |
135
|
|
|
{ |
136
|
|
|
$ignores = ConfigSupport::ignores(); |
137
|
|
|
|
138
|
7 |
|
return Arrayable::of($locales) |
139
|
|
|
->unique() |
140
|
7 |
|
->filter(function ($locale) use ($ignores) { |
141
|
|
|
return $this->isProtected($locale) || ! $this->in($locale, $ignores); |
142
|
7 |
|
}) |
143
|
|
|
->values() |
144
|
|
|
->get(); |
145
|
25 |
|
} |
146
|
|
|
|
147
|
25 |
|
protected function in(string $locale, array $locales): bool |
148
|
|
|
{ |
149
|
25 |
|
return in_array($locale, $locales, true); |
150
|
25 |
|
} |
151
|
|
|
|
152
|
25 |
|
protected function findJson(): array |
153
|
25 |
|
{ |
154
|
25 |
|
$files = File::names($this->resourcesPath(), null, true); |
155
|
|
|
|
156
|
|
|
return Arrayable::of($files) |
157
|
18 |
|
->filter(function (string $filename) { |
158
|
|
|
return $this->hasJson($filename); |
159
|
18 |
|
}) |
160
|
|
|
->map(static function (string $filename) { |
161
|
18 |
|
return PathSupport::filename($filename); |
162
|
|
|
}) |
163
|
18 |
|
->get(); |
164
|
18 |
|
} |
165
|
18 |
|
|
166
|
|
|
protected function findPhp(): array |
167
|
|
|
{ |
168
|
18 |
|
return Directory::names($this->resourcesPath()); |
169
|
|
|
} |
170
|
18 |
|
|
171
|
|
|
protected function resourcesPath(): string |
172
|
18 |
|
{ |
173
|
18 |
|
return ConfigSupport::resources(); |
174
|
18 |
|
} |
175
|
|
|
} |
176
|
|
|
|