Localizer   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 5
A setSupportedLocales() 0 16 4
A getSupportedLocales() 0 4 1
A getSlagsSupportedLocales() 0 4 1
A isSupportedLocale() 0 4 1
A getDefaultLocale() 0 4 1
A setLocale() 0 6 2
A getLocale() 0 4 1
A isHideDefaultLocaleInURL() 0 4 1
A getTranslationFileName() 0 4 1
A getNamePrefix() 0 4 1
1
<?php
2
3
namespace VSamovarov\LaravelLocalizer;
4
5
use VSamovarov\LaravelLocalizer\Exceptions\ConfigLocalizerNotDefined;
6
7
class Localizer
8
{
9
    /**
10
     * Supported Locales
11
     *
12
     * @var array
13
     */
14
    protected $supportedLocales;
15
16
    /**
17
     * default Locales
18
     *
19
     * @var string
20
     */
21
22
    protected $defaultLocale;
23
24
    /**
25
     * If `hideDefaultLocaleInURL` is true, then a url without locale
26
     * is identical with the same url with default locale.
27
     *
28
     * @var bool
29
     */
30
    protected $hideDefaultLocaleInURL = true;
31
32
    /**
33
     * Имя фйала с переводами
34
     *
35
     * @var string
36
     */
37
    protected $translationFileName = 'routes';
38
39
    /**
40
     * Используется в атрибуте 'as'
41
     *
42
     * @var string
43
     */
44
    protected $localizerNamePrefix = 'localizer-';
45
46
    public function __construct($config)
47
    {
48
        if (empty($config) || !is_array($config)) {
49
            throw new ConfigLocalizerNotDefined('Не задана конфигурация');
50
        }
51
52
53
54
        $this->defaultLocale = current(array_keys($config['supportedLocales']));
55
        $this->hideDefaultLocaleInURL = !empty($config['hideDefaultLocaleInURL']);
56
57
        $this->translationFileName = empty($config['hideDefaultLocaleInURL']) ? $this->translationFileName : $config['hideDefaultLocaleInURL'];
58
        $this->localizerNamePrefix = empty($config['localizerNamePrefix']) ? $this->localizerNamePrefix : $config['localizerNamePrefix'];
59
60
        $this->setSupportedLocales($config['supportedLocales']);
61
        $this->setLocale($this->defaultLocale);
62
    }
63
64
    /**
65
     * Set supported Locales
66
     *
67
     * @return array
68
     */
69
    public function setSupportedLocales($supportedLocales): void
70
    {
71
        /**
72
         * Определяем префикс, который будет в URL
73
         * Он может зависеть от параметра $hideDefaultLocaleInURL и настроек
74
         */
75
        foreach ($supportedLocales as $key => $value) {
76
            $this->supportedLocales[$key] = array_merge(
77
                $value,
78
                [
79
                    'slug' => $key,
80
                    'prefix' => ($this->getDefaultLocale() === $key && $this->isHideDefaultLocaleInURL()) ? '' : $key
81
                ]
82
            );
83
        }
84
    }
85
86
    /**
87
     * Get supported Locales
88
     *
89
     * @return array
90
     */
91
    public function getSupportedLocales(): array
92
    {
93
        return $this->supportedLocales;
94
    }
95
96
    /**
97
     * Get slag Locales
98
     *
99
     * @return array
100
     */
101
    public function getSlagsSupportedLocales(): array
102
    {
103
        return array_column($this->supportedLocales, 'slug');
104
    }
105
106
    /**
107
     * is Supported Locale
108
     *
109
     * @param string $locale
110
     * @return boolean
111
     */
112
    public function isSupportedLocale($locale): bool
113
    {
114
        return isset($this->getSupportedLocales()[strval($locale)]);
115
    }
116
117
    /**
118
     * Получаем локаль по умолчанию
119
     *
120
     * @return string
121
     */
122
    public function getDefaultLocale()
123
    {
124
        return $this->defaultLocale;
125
    }
126
127
    /**
128
     * Установка локали
129
     *
130
     * @param string $locale
131
     * @return void
132
     */
133
    public function setLocale($locale): void
134
    {
135
        if ($this->isSupportedLocale($locale)) {
136
            app()->setLocale($locale);
137
        }
138
    }
139
140
    /**
141
     * Получить локаль
142
     *
143
     * @return string
144
     */
145
    public function getLocale(): string
146
    {
147
        return app()->getLocale();
148
    }
149
150
    /** Скрывать ли локаль по умолчанию в URL
151
     *
152
     *
153
     * @return boolean
154
     */
155
    public function isHideDefaultLocaleInURL(): bool
156
    {
157
        return $this->hideDefaultLocaleInURL;
158
    }
159
160
    /**
161
     * Имя фйала с переводами
162
     *
163
     * @return string
164
     */
165
    public function getTranslationFileName(): string
166
    {
167
        return $this->translationFileName;
168
    }
169
170
    /**
171
     * Используется в атрибуте 'as'
172
     *
173
     * @return string
174
     */
175
    public function getNamePrefix(): string
176
    {
177
        return $this->localizerNamePrefix;
178
    }
179
}