1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pmochine\LaravelTongue\Misc; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Pmochine\LaravelTongue\Exceptions\SupportedLocalesNotDefined; |
7
|
|
|
|
8
|
|
|
class ConfigList |
9
|
|
|
{ |
10
|
|
|
public function lookup(string $key = null, string $locale = null) |
11
|
|
|
{ |
12
|
|
|
$locales = Config::supportedLocales(); |
13
|
|
|
|
14
|
|
|
if (empty($locales) || ! is_array($locales)) { |
15
|
|
|
throw new SupportedLocalesNotDefined(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
if (! $key) { |
19
|
|
|
return collect($locales); |
|
|
|
|
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
if ($key === 'BCP47') { |
23
|
|
|
return $this->BCP47($locale, $locales); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if ($key === 'subdomains') { |
27
|
|
|
return $this->getSubdomains($locale); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($key === 'aliases') { |
31
|
|
|
return $this->getAliases($locale); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (! Arr::has($locales, "{$locale}.{$key}")) { |
35
|
|
|
throw new SupportedLocalesNotDefined(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return data_get($locales, "{$locale}.{$key}"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets the BCP 47 Value of the regional |
43
|
|
|
* See for more: http://schneegans.de/lv/?tags=en&format=text. |
44
|
|
|
* |
45
|
|
|
* @param string|null $locale |
46
|
|
|
* @param array $loacles [the list in the config file] |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
protected function BCP47(string $locale = null, array $locales): ?string |
50
|
|
|
{ |
51
|
|
|
$bcp47 = data_get($locales, "{$locale}.regional"); |
52
|
|
|
|
53
|
|
|
if (! $bcp47) { |
54
|
|
|
return $locale; |
55
|
|
|
} //locale is the "minimum" of BCP 47 |
56
|
|
|
|
57
|
|
|
//regional value needs to replace underscore |
58
|
|
|
return str_replace('_', '-', $bcp47); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $subdomain [like "admin"] |
63
|
|
|
* @return array|bool |
64
|
|
|
*/ |
65
|
|
|
protected function getSubdomains(string $subdomain = null) |
66
|
|
|
{ |
67
|
|
|
if (is_null($subdomain)) { |
68
|
|
|
return Config::subdomains(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return in_array($subdomain, Config::subdomains()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets the array of the config, or gets the locale value of a subdomain. |
76
|
|
|
* Like: "gewinnen" -> "de". |
77
|
|
|
* |
78
|
|
|
* @param string $subdomain |
79
|
|
|
* @return array|string |
80
|
|
|
*/ |
81
|
|
|
protected function getAliases(string $subdomain = null) |
82
|
|
|
{ |
83
|
|
|
$domains = Config::aliases(); |
84
|
|
|
|
85
|
|
|
if (is_null($subdomain)) { |
86
|
|
|
return $domains; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (array_key_exists($subdomain, $domains)) { |
90
|
|
|
return $domains[$subdomain]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return ''; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|