Completed
Push — master ( 4e8d1d...8d6f18 )
by Philipp
03:00
created

Locale::saveInCookie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pmochine\LaravelTongue\Localization;
4
5
use Illuminate\Foundation\Application;
6
use Pmochine\LaravelTongue\Misc\Config;
7
8
class Locale
9
{
10
    /**
11
     * Our instance of the Laravel app.
12
     *
13
     * @var Illuminate\Foundation\Application
0 ignored issues
show
Bug introduced by
The type Pmochine\LaravelTongue\L...\Foundation\Application was not found. Did you mean Illuminate\Foundation\Application? If so, make sure to prefix the type with \.
Loading history...
14
     */
15
    protected $app = '';
16
17
    public function __construct(Application $app)
18
    {
19
        $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\L...\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...
20
    }
21
22
    /**
23
     * Gets the current app locale that is set
24
     *
25
     * @return  string 
26
     */
27
    public function get(): string
28
    {
29
        return $this->app->getLocale();
30
    }
31
32
    /**
33
     * Sets the locale in the app
34
     *
35
     * @param   string  $locale  
36
     *
37
     * @return  void            
38
     */
39
    public function set(string $locale): void
40
    {
41
        $this->app->setLocale($locale);
42
    }
43
44
    /**
45
     * Set and saves locale in app & cookies and sets regions
46
     *
47
     * @param   string  $locale  
48
     *
49
     * @return  void             
50
     */
51
    public function save(string $locale): void
52
    {
53
        $this->set($locale);
54
        $this->saveInCookie($locale);
55
        $this->setRegionalTimeAndMoney($locale);
56
    }
57
58
    /**
59
     * Save locale in cookie
60
     *
61
     * @param   string  $locale  
62
     *
63
     * @return  void             
64
     */
65
    public function saveInCookie(string $locale): void
66
    {
67
        if (Config::cookieLocalization()) {
68
            Localization::cookie($locale);
69
        }
70
    }
71
72
    public function setRegionalTimeAndMoney(string $locale): void
73
    {
74
        // Regional locale such as de_DE, so formatLocalized works in Carbon
75
        $regional = tongue()->speaking('regional', $locale);
76
77
        if ($regional) {
78
            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

78
            setlocale(LC_TIME, /** @scrutinizer ignore-type */ $regional . '.UTF-8');
Loading history...
79
            setlocale(LC_MONETARY, $regional . '.UTF-8');
80
        }
81
    }
82
    /**
83
     * Gets the twist of the tongue.
84
     * Return the direction left or right.
85
     * e.g. for arabic language.
86
     * 
87
     * @param   string  $script  
88
     *
89
     * @return  string           
90
     */
91
    public function scriptDirection(string $script): string
92
    {
93
        switch ($script) {
94
            case 'Arab':
95
            case 'Hebr':
96
            case 'Mong':
97
            case 'Tfng':
98
            case 'Thaa':
99
                return 'rtl';
100
            default:
101
                return 'ltr';
102
        }
103
    }
104
105
    /**
106
     * Finds the locale in the URL.
107
     * Like en, de...
108
     *
109
     * @return string
110
     */
111
    public function find(): string
112
    {
113
        return Localization::decipherTongue();
114
    }
115
}
116