Test Setup Failed
Push — master ( 485b33...0980dd )
by Tom
06:34 queued 10s
created

Locales   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 3
dl 0
loc 126
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B load() 0 21 6
A all() 0 4 1
A current() 0 4 2
A has() 0 4 1
A get() 0 4 1
A add() 0 4 1
A forget() 0 4 1
A getLocaleSeparator() 0 4 2
A getCountryLocale() 0 4 1
A isLocaleCountryBased() 0 4 1
A getLanguageFromCountryBasedLocale() 0 4 1
A toArray() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 8 4
A offsetUnset() 0 4 1
1
<?php
2
3
namespace Dimsav\Translatable;
4
5
use ArrayAccess;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
8
use Illuminate\Contracts\Config\Repository as ConfigContract;
9
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
10
11
class Locales implements Arrayable, ArrayAccess
12
{
13
    /**
14
     * @var ConfigContract
15
     */
16
    protected $config;
17
18
    /**
19
     * @var TranslatorContract
20
     */
21
    protected $translator;
22
23
    /**
24
     * @var array
25
     */
26
    protected $locales = [];
27
28
    public function __construct(ConfigContract $config, TranslatorContract $translator)
29
    {
30
        $this->config = $config;
31
        $this->translator = $translator;
32
33
        $this->load();
34
    }
35
36
    public function load(): void
37
    {
38
        $localesConfig = (array) $this->config->get('translatable.locales', []);
39
40
        if (empty($localesConfig)) {
41
            throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" and that the locales configuration is defined.');
42
        }
43
44
        $this->locales = [];
45
        foreach ($localesConfig as $key => $locale) {
46
            if (is_string($key) && is_array($locale)) {
47
                $this->locales[$key] = $key;
48
                foreach ($locale as $country) {
49
                    $countryLocale = $this->getCountryLocale($key, $country);
50
                    $this->locales[$countryLocale] = $countryLocale;
51
                }
52
            } else {
53
                $this->locales[$locale] = $locale;
54
            }
55
        }
56
    }
57
58
    public function all(): array
59
    {
60
        return array_values($this->locales);
61
    }
62
63
    public function current()
64
    {
65
        return $this->config->get('translatable.locale') ?: $this->translator->getLocale();
66
    }
67
68
    public function has(string $locale): bool
69
    {
70
        return isset($this->locales[$locale]);
71
    }
72
73
    public function get(string $locale): ?string
74
    {
75
        return $this->locales[$locale] ?? null;
76
    }
77
78
    public function add(string $locale): void
79
    {
80
        $this->locales[$locale] = $locale;
81
    }
82
83
    public function forget(string $locale): void
84
    {
85
        unset($this->locales[$locale]);
86
    }
87
88
    public function getLocaleSeparator(): string
89
    {
90
        return $this->config->get('translatable.locale_separator') ?: '-';
91
    }
92
93
    public function getCountryLocale(string $locale, string $country): string
94
    {
95
        return $locale.$this->getLocaleSeparator().$country;
96
    }
97
98
    public function isLocaleCountryBased(string $locale): bool
99
    {
100
        return strpos($locale, $this->getLocaleSeparator()) !== false;
101
    }
102
103
    public function getLanguageFromCountryBasedLocale(string $locale): string
104
    {
105
        return explode($this->getLocaleSeparator(), $locale)[0];
106
    }
107
108
    public function toArray(): array
109
    {
110
        return $this->all();
111
    }
112
113
    public function offsetExists($key): bool
114
    {
115
        return $this->has($key);
116
    }
117
118
    public function offsetGet($key): ?string
119
    {
120
        return $this->get($key);
121
    }
122
123
    public function offsetSet($key, $value)
124
    {
125
        if (is_string($key) && is_string($value)) {
126
            $this->add($this->getCountryLocale($key, $value));
127
        } elseif (is_string($value)) {
128
            $this->add($value);
129
        }
130
    }
131
132
    public function offsetUnset($key)
133
    {
134
        $this->forget($key);
135
    }
136
}
137