Tongue::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Pmochine\LaravelTongue;
4
5
use Illuminate\Foundation\Application;
6
use Pmochine\LaravelTongue\Localization\Locale;
7
use Pmochine\LaravelTongue\Localization\Localization;
8
use Pmochine\LaravelTongue\Misc\Config;
9
use Pmochine\LaravelTongue\Misc\ConfigList;
10
11
class Tongue
12
{
13
    /**
14
     * The class that handles the locale methods.
15
     *
16
     * @var \Pmochine\LaravelTongue\Localization\Locale
17
     */
18
    protected $locale;
19
20
    public function __construct(Application $app)
21
    {
22
        $this->locale = new Locale($app);
23
    }
24
25
    /**
26
     * Detects the tongue, the locale
27
     * of the User.
28
     *
29
     * @return Tongue :P
30
     */
31
    public function detect(): self
32
    {
33
        $this->speaks($this->locale->find());
34
35
        return $this;
36
    }
37
38
    /**
39
     * Gets the current speaking tongue...
40
     * (language code).
41
     *
42
     * @return string|array|null
43
     */
44
    public function current($key = null)
45
    {
46
        if (! $key) {
47
            return $this->locale->get();
48
        }
49
50
        return $this->speaking($key, $this->locale->get());
51
    }
52
53
    /**
54
     * Gets the twist of the tongue.
55
     * Return the direction left or right.
56
     * e.g. for arabic language.
57
     *
58
     * @return string
59
     */
60
    public function leftOrRight(): string
61
    {
62
        return $this->locale->scriptDirection($this->current('script'));
0 ignored issues
show
Bug introduced by
It seems like $this->current('script') can also be of type array and null; however, parameter $script of Pmochine\LaravelTongue\L...cale::scriptDirection() 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

62
        return $this->locale->scriptDirection(/** @scrutinizer ignore-type */ $this->current('script'));
Loading history...
63
    }
64
65
    /**
66
     * A tongue-twister is a phrase that is
67
     * designed to be difficult to articulate properly,
68
     * So lets just asume the user just can't speak the
69
     * language...
70
     *
71
     * @return bool (yes if its not speakable)
72
     */
73
    public function twister(): bool
74
    {
75
        $locale = Localization::fromUrl();
76
77
        if ($locale && tongue()->speaking('subdomains', $locale)) {
78
            //whitelisted subdomains! like admin.domain.com
79
            return false;
80
        }
81
82
        //custom subdomains with locale. gewinnen.domain.com -> de as locale
83
        if ($locale && $customLocale = tongue()->speaking('aliases', $locale)) {
84
            //but we need to check again if it is spoken or not
85
            return $this->current() !== $customLocale;
86
        }
87
88
        //fallback language is the same as the current language
89
        if (Config::beautify() && $this->current() === Config::fallbackLocale()) {
90
            //didn't found locale means browser is set to exmaple.com
91
            if (! $locale) {
92
                return false;
93
            }
94
            //browser is set to en.example.com but should be forced back to example.com
95
            if ($locale === Config::fallbackLocale()) {
96
                return true;
97
            }
98
        }
99
100
        //decipher from
101
        return $this->current() !== $locale;
102
    }
103
104
    /**
105
     * The user speaks locale language.
106
     * Set the locale.
107
     *
108
     * @param  string  $locale
109
     * @return Tongue|\Illuminate\Http\RedirectResponse;
110
     */
111
    public function speaks(string $locale)
112
    {
113
        if (! $this->isSpeaking($locale)) {
114
            //locale does not exist.
115
            return dialect()->redirectBackToLatest();
116
        }
117
118
        $this->locale->save($locale);
119
120
        return $this;
121
    }
122
123
    /**
124
     * Used to return back to previous url.
125
     * e.g. if you change the language. its usefull.
126
     *
127
     * @return \Illuminate\Http\RedirectResponse;
128
     */
129
    public function back()
130
    {
131
        return dialect()->redirect(dialect()->redirectUrl(url()->previous()));
132
    }
133
134
    /**
135
     * Gets the collection list of all languages,
136
     * the website speaks. Or give us the specific keys.
137
     *
138
     * @return \Illuminate\Support\Collection|string|array|null
139
     */
140
    public function speaking(string $key = null, string $locale = null)
141
    {
142
        return (new ConfigList)->lookup($key, $locale);
143
    }
144
145
    /**
146
     * Checks if your page is speaking the language.
147
     *
148
     * @param  string  $locale
149
     * @return bool
150
     */
151
    public function isSpeaking(string $locale): bool
152
    {
153
        return $this->speaking()->has($locale);
154
    }
155
}
156