Passed
Push — master ( c05588...b38d31 )
by Philipp
04:34
created

Tongue::BCP47()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pmochine\LaravelTongue;
4
5
use Illuminate\Foundation\Application;
6
use Pmochine\LaravelTongue\Misc\Config;
7
use Pmochine\LaravelTongue\Localization\Localization;
8
use Pmochine\LaravelTongue\Exceptions\SupportedLocalesNotDefined;
9
10
class Tongue
11
{
12
    /**
13
     * Our instance of the Laravel app.
14
     *
15
     * @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...
16
     */
17
    protected $app = '';
18
19
    public function __construct(Application $app)
20
    {
21
        $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...
22
    }
23
24
    /**
25
     * Detects the tongue, the locale
26
     * of the User.
27
     *
28
     * @return Tongue :P
29
     */
30
    public function detect()
31
    {
32
        $locale = $this->findLocale();
33
34
        $this->speaks($locale);
35
36
        return $this;
37
    }
38
39
    /**
40
     * Gets the current speaking tongue...
41
     * (language code).
42
     *
43
     * @return  string
44
     */
45
    public function current($key = null)
46
    {
47
        $locale = $this->app->getLocale();
48
49
        if (! $key) {
50
            return $locale;
51
        }
52
53
        return $this->speaking($key, $locale);
54
    }
55
56
    /**
57
     * Gets the twist of the tongue.
58
     * Return the direction left or right.
59
     * e.g. for arabic language.
60
     *
61
     * @return string
62
     */
63
    public function leftOrRight()
64
    {
65
        switch (Config::supportedLocales()[$this->current()]['script']) {
66
            case 'Arab':
67
            case 'Hebr':
68
            case 'Mong':
69
            case 'Tfng':
70
            case 'Thaa':
71
            return 'rtl';
72
            default:
73
            return 'ltr';
74
        }
75
    }
76
77
    /**
78
     * A tongue-twister is a phrase that is
79
     * designed to be difficult to articulate properly,
80
     * So lets just asume the user just can't speak the
81
     * language...
82
     *
83
     * @return bool (yes if its not speakable)
84
     */
85
    public function twister()
86
    {
87
        $locale = Localization::fromUrl();
88
89
        //fallback language is the same as the current language
90
        if (Config::beautify() && $this->current() === Config::fallbackLocale()) {
91
            //didn't found locale means browser is set to exmaple.com
92
            if (! $locale) {
93
                return false;
94
            }
95
            //browser is set to en.example.com but should be forced back to example.com
96
            if ($locale === Config::fallbackLocale()) {
97
                return true;
98
            }
99
        }
100
101
        //decipher from
102
        return $this->current() != $locale;
103
    }
104
105
    /**
106
     * The user speaks locale language.
107
     * Set the locale.
108
     *
109
     * @param  string $locale
110
     * @return Tongue :P
111
     */
112
    public function speaks(string $locale)
113
    {
114
        if (! $this->isSpeaking($locale)) {
115
            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...
116
        }
117
118
        $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...
119
120
        if ($locale != Localization::cookie() && Config::cookieLocalization()) {
121
            Localization::cookie($locale);
122
        }
123
124
        // Regional locale such as de_DE, so formatLocalized works in Carbon
125
        $regional = $this->speaking('regional', $locale);
126
127
        if ($regional) {
128
            setlocale(LC_TIME, $regional.'.UTF-8');
129
            setlocale(LC_MONETARY, $regional.'.UTF-8');
130
        }
131
132
        return $this;
133
    }
134
135
    /**
136
     * Used to return back to previous url.
137
     * e.g. if you change the language. its usefull.
138
     *
139
     * @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...
140
     */
141
    public function back()
142
    {
143
        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...
144
    }
145
146
    /**
147
     * Gets the collection list of all languages,
148
     * the website speaks. Or give us the specific keys.
149
     *
150
     * @return collection | string
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...
151
     */
152
    public function speaking($key = null, $locale = null)
153
    {
154
        $locales = Config::supportedLocales();
155
156
        if (empty($locales) || ! is_array($locales)) {
157
            throw new SupportedLocalesNotDefined();
158
        }
159
160
        if (! $key) {
161
            return collect($locales);
162
        }
163
164
        if($key === 'BCP47'){
165
            return $this->BCP47($locale, $locales);
166
        }
167
168
        if (! array_has($locales, "{$locale}.{$key}")) {
169
            throw new SupportedLocalesNotDefined();
170
        }
171
172
        return data_get($locales, "{$locale}.{$key}");
173
    }
174
175
    /**
176
     * Finds the Subdomain in the URL.
177
     * Like en, de...
178
     *
179
     * @return string
180
     */
181
    protected function findLocale()
182
    {
183
        if (! Config::subdomain()) {
184
            return false; //use Mcamara Localization
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
185
        }
186
187
        return Localization::decipherTongue();
188
    }
189
190
    /**
191
     * Checks if your page is speaking the language.
192
     *
193
     * @param  string  $locale
194
     * @return bool
195
     */
196
    protected function isSpeaking($locale)
197
    {
198
        return array_key_exists($locale, Config::supportedLocales());
199
    }
200
201
    /**
202
     * Gets the BCP 47 Value of the regional
203
     * See for more: http://schneegans.de/lv/?tags=en&format=text
204
     *
205
     * @param  string $locale
206
     * @param  array $loacles [the list in the config file]
207
     */
208
    protected function BCP47($locale, $locales) 
209
    {
210
       $bcp47 = data_get($locales, "{$locale}.regional");
211
212
       if(! $bcp47) return $locale; //locale is the "minimum" of BCP 47
213
214
       //regional value needs to replace underscore
215
       return str_replace('_', '-', $bcp47);
216
    }
217
}
218