Completed
Push — master ( a3bd8c...b5d8e1 )
by Philipp
06:13 queued 03:39
created

Tongue::getAliases()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pmochine\LaravelTongue;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Foundation\Application;
7
use Pmochine\LaravelTongue\Misc\Config;
8
use Pmochine\LaravelTongue\Localization\Localization;
9
use Pmochine\LaravelTongue\Exceptions\SupportedLocalesNotDefined;
10
11
class Tongue
12
{
13
    /**
14
     * Our instance of the Laravel app.
15
     *
16
     * @var Illuminate\Foundation\Application
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\I...\Foundation\Application was not found. Did you mean Illuminate\Foundation\Application? If so, make sure to prefix the type with \.
Loading history...
17
     */
18
    protected $app = '';
19
20
    public function __construct(Application $app)
21
    {
22
        $this->app = $app;
0 ignored issues
show
Documentation Bug introduced by
It seems like $app of type Illuminate\Foundation\Application is incompatible with the declared type Pmochine\LaravelTongue\I...\Foundation\Application of property $app.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
23
    }
24
25
    /**
26
     * Detects the tongue, the locale
27
     * of the User.
28
     *
29
     * @return Tongue :P
30
     */
31
    public function detect()
32
    {
33
        $locale = $this->findLocale();
34
35
        $this->speaks($locale);
36
37
        return $this;
38
    }
39
40
    /**
41
     * Gets the current speaking tongue...
42
     * (language code).
43
     *
44
     * @return  string
45
     */
46
    public function current($key = null)
47
    {
48
        $locale = $this->app->getLocale();
49
50
        if (!$key) {
51
            return $locale;
52
        }
53
54
        return $this->speaking($key, $locale);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->speaking($key, $locale) also could return the type array which is incompatible with the documented return type string.
Loading history...
55
    }
56
57
    /**
58
     * Gets the twist of the tongue.
59
     * Return the direction left or right.
60
     * e.g. for arabic language.
61
     *
62
     * @return string
63
     */
64
    public function leftOrRight()
65
    {
66
        switch (Config::supportedLocales()[$this->current()]['script']) {
67
            case 'Arab':
68
            case 'Hebr':
69
            case 'Mong':
70
            case 'Tfng':
71
            case 'Thaa':
72
                return 'rtl';
73
            default:
74
                return 'ltr';
75
        }
76
    }
77
78
    /**
79
     * A tongue-twister is a phrase that is
80
     * designed to be difficult to articulate properly,
81
     * So lets just asume the user just can't speak the
82
     * language...
83
     *
84
     * @return bool (yes if its not speakable)
85
     */
86
    public function twister()
87
    {
88
        $locale = Localization::fromUrl();
89
90
        if (tongue()->speaking('subdomains', $locale)) {
91
            //whitelisted subdomains! like admin.domain.com
92
            return false;
93
        }
94
95
        //custom subdomains with locale. gewinnen.domain.com -> de as locale
96
        if ($customLocale = tongue()->speaking('aliases', $locale)) {
97
            //but we need to check again if it is spoken or not
98
            return $this->current() != $customLocale;
99
        }
100
101
        //fallback language is the same as the current language
102
        if (Config::beautify() && $this->current() === Config::fallbackLocale()) {
103
            //didn't found locale means browser is set to exmaple.com
104
            if (!$locale) {
105
                return false;
106
            }
107
            //browser is set to en.example.com but should be forced back to example.com
108
            if ($locale === Config::fallbackLocale()) {
109
                return true;
110
            }
111
        }
112
113
        //decipher from
114
        return $this->current() != $locale;
115
    }
116
117
    /**
118
     * The user speaks locale language.
119
     * Set the locale.
120
     *
121
     * @param  string $locale
122
     * @return Tongue :P
123
     */
124
    public function speaks(string $locale)
125
    {
126
        if (!$this->isSpeaking($locale)) {
127
            return abort(404); //oder error?
0 ignored issues
show
Bug Best Practice introduced by
The expression return abort(404) returns the type void which is incompatible with the documented return type Pmochine\LaravelTongue\Tongue.
Loading history...
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
128
        }
129
130
        $this->app->setLocale($this->locale = $locale);
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
131
132
        if ($locale != Localization::cookie() && Config::cookieLocalization()) {
133
            Localization::cookie($locale);
134
        }
135
136
        // Regional locale such as de_DE, so formatLocalized works in Carbon
137
        $regional = $this->speaking('regional', $locale);
138
139
        if ($regional) {
140
            setlocale(LC_TIME, $regional . '.UTF-8');
0 ignored issues
show
Bug introduced by
Are you sure $regional of type Pmochine\LaravelTongue\collection|array|string can be used in concatenation? ( Ignorable by Annotation )

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

140
            setlocale(LC_TIME, /** @scrutinizer ignore-type */ $regional . '.UTF-8');
Loading history...
141
            setlocale(LC_MONETARY, $regional . '.UTF-8');
142
        }
143
144
        return $this;
145
    }
146
147
    /**
148
     * Used to return back to previous url.
149
     * e.g. if you change the language. its usefull.
150
     *
151
     * @return Illuminate\Routing\Redirector
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\I...nate\Routing\Redirector was not found. Did you mean Illuminate\Routing\Redirector? If so, make sure to prefix the type with \.
Loading history...
152
     */
153
    public function back()
154
    {
155
        return dialect()->redirect(dialect()->redirectUrl(url()->previous()));
0 ignored issues
show
Bug Best Practice introduced by
The expression return dialect()->redire...Url(url()->previous())) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Pmochine\LaravelTongue\I...nate\Routing\Redirector.
Loading history...
156
    }
157
158
    /**
159
     * Gets the collection list of all languages,
160
     * the website speaks. Or give us the specific keys.
161
     *
162
     * @return collection|string|array|null
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
163
     */
164
    public function speaking($key = null, $locale = null)
165
    {
166
        $locales = Config::supportedLocales();
167
168
        if (empty($locales) || !is_array($locales)) {
169
            throw new SupportedLocalesNotDefined();
170
        }
171
172
        if (!$key) {
173
            return collect($locales);
174
        }
175
176
        if ($key === 'BCP47') {
177
            return $this->BCP47($locale, $locales);
178
        }
179
180
        if ($key === 'subdomains') {
181
            return $this->getSubdomains($locale);
182
        }
183
184
        if ($key === 'aliases') {
185
            return $this->getAliases($locale);
186
        }
187
188
        if (!Arr::has($locales, "{$locale}.{$key}")) {
189
            throw new SupportedLocalesNotDefined();
190
        }
191
192
        return data_get($locales, "{$locale}.{$key}");
193
    }
194
195
    /**
196
     * Finds the Subdomain in the URL.
197
     * Like en, de...
198
     *
199
     * @return string
200
     */
201
    protected function findLocale()
202
    {
203
        return Localization::decipherTongue();
204
    }
205
206
    /**
207
     * Checks if your page is speaking the language.
208
     *
209
     * @param  string  $locale
210
     * @return bool
211
     */
212
    public function isSpeaking($locale)
213
    {
214
        return array_key_exists($locale, Config::supportedLocales());
215
    }
216
217
    /**
218
     * Gets the BCP 47 Value of the regional
219
     * See for more: http://schneegans.de/lv/?tags=en&format=text.
220
     *
221
     * @param  string $locale
222
     * @param  array $loacles [the list in the config file]
223
     */
224
    protected function BCP47($locale, $locales)
225
    {
226
        $bcp47 = data_get($locales, "{$locale}.regional");
227
228
        if (!$bcp47) {
229
            return $locale;
230
        } //locale is the "minimum" of BCP 47
231
232
        //regional value needs to replace underscore
233
        return str_replace('_', '-', $bcp47);
234
    }
235
236
    /**
237
     * @param   string  $subdomain  [like "admin"]
238
     *
239
     * @return  array|bool
240
     */
241
    protected function getSubdomains(string $subdomain = null)
242
    {
243
        if (is_null($subdomain)) {
244
            return Config::subdomains();
245
        }
246
247
        return in_array($subdomain, Config::subdomains());
248
    }
249
250
    /**
251
     * Gets the array of the config, or gets the locale value of a subdomain.
252
     * Like: "gewinnen" -> "de".
253
     *
254
     * @param   string  $subdomain
255
     *
256
     * @return  array|string
257
     */
258
    protected function getAliases(string $subdomain = null)
259
    {
260
        $domains = Config::aliases();
261
262
        if (is_null($subdomain)) {
263
            return $domains;
264
        }
265
266
        if (array_key_exists($subdomain, $domains)) {
267
            return $domains[$subdomain];
268
        }
269
270
        return '';
271
    }
272
}
273