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