Completed
Push — master ( 4e8d1d...8d6f18 )
by Philipp
03:00
created

ConfigList   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
B lookup() 0 29 8
A getAliases() 0 13 3
A getSubdomains() 0 7 2
A BCP47() 0 10 2
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);
0 ignored issues
show
Bug introduced by
It seems like $locale can also be of type null; however, parameter $locale of Pmochine\LaravelTongue\Misc\ConfigList::BCP47() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
            return $this->BCP47(/** @scrutinizer ignore-type */ $locale, $locales);
Loading history...
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 $locale
46
     * @param  array $loacles [the list in the config file]
47
     */
48
    protected function BCP47(string $locale, array $locales): string
49
    {
50
        $bcp47 = data_get($locales, "{$locale}.regional");
51
52
        if (!$bcp47) {
53
            return $locale;
54
        } //locale is the "minimum" of BCP 47
55
56
        //regional value needs to replace underscore
57
        return str_replace('_', '-', $bcp47);
58
    }
59
60
    /**
61
     * @param   string  $subdomain  [like "admin"]
62
     *
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
     *
80
     * @return  array|string
81
     */
82
    protected function getAliases(string $subdomain = null)
83
    {
84
        $domains = Config::aliases();
85
86
        if (is_null($subdomain)) {
87
            return $domains;
88
        }
89
90
        if (array_key_exists($subdomain, $domains)) {
91
            return $domains[$subdomain];
92
        }
93
94
        return '';
95
    }
96
}
97