Scope::validateLocale()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Locale;
4
5
use Thinktomorrow\Locale\Exceptions\InvalidScope;
6
use Thinktomorrow\Locale\Values\Locale;
7
use Thinktomorrow\Locale\Values\Root;
8
9
class Scope
10
{
11
    /**
12
     * @var array
13
     */
14
    private $locales;
15
16
    /**
17
     * The active locale.
18
     *
19
     * @var Locale
20
     */
21
    private static $activeLocale;
22
23
    /**
24
     * The default locale. In the request path
25
     * it's the hidden segment, e.g. /.
26
     *
27
     * @var Locale
28
     */
29
    private $default;
30
31
    /**
32
     * When the canonical scope has a root set to be
33
     * other than the current, that specific root is defined here
34
     * By default the current request root is of use (NULL).
35
     *
36
     * @var null|Root
37
     */
38
    private $customRoot = null;
39
40 86
    public function __construct(array $locales)
41
    {
42 86
        if (!isset($locales['/'])) {
43 1
            throw new InvalidScope('Default locale is required for scope. Add this as \'/\' => locale.');
44
        }
45 86
        $this->locales = $locales;
46 86
        $this->default = Locale::from($this->locales['/']);
47 86
    }
48
49 12
    public function setCustomRoot(Root $customRoot)
50
    {
51 12
        $this->customRoot = $customRoot;
52
53 12
        return $this;
54
    }
55
56 10
    public function customRoot(): ?Root
57
    {
58 10
        return $this->customRoot;
59
    }
60
61
    /**
62
     * Get the locale by segment identifier.
63
     *
64
     * @param $segment
65
     *
66
     * @return null|Locale
67
     */
68 72
    public function findLocale($segment): ?Locale
69
    {
70 72
        return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null;
71
    }
72
73 35
    public function locales(): array
74
    {
75 35
        return $this->locales;
76
    }
77
78 65
    public function defaultLocale(): Locale
79
    {
80 65
        return $this->default;
81
    }
82
83 18
    public static function activeLocale(): ?Locale
84
    {
85 18
        return static::$activeLocale;
0 ignored issues
show
Bug introduced by
Since $activeLocale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $activeLocale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
86
    }
87
88 72
    public static function setActiveLocale(Locale $locale)
89
    {
90 72
        static::$activeLocale = $locale;
0 ignored issues
show
Bug introduced by
Since $activeLocale is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $activeLocale to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
91 72
    }
92
93
    /**
94
     * Get the url segment which corresponds with the passed locale.
95
     *
96
     * @param null $locale
97
     *
98
     * @return null|string
99
     */
100 39
    public function segment($locale = null): ?string
101
    {
102 39
        if (is_null($locale)) {
103 2
            return $this->activeSegment();
104
        }
105
106 39
        return ($key = array_search($locale, $this->locales)) ? $key : null;
107
    }
108
109 18
    public function activeSegment(): ?string
110
    {
111 18
        return $this->segment($this->activeLocale());
112
    }
113
114 30
    public function validateLocale(string $locale = null): bool
115
    {
116 30
        if (!$locale) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $locale of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
117 5
            return false;
118
        }
119
120 28
        return in_array($locale, $this->locales);
121
    }
122
123 27
    public function validateSegment(string $segment = null): bool
124
    {
125 27
        if (!$segment) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $segment of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
126 5
            return false;
127
        }
128
129 25
        return isset($this->locales[$segment]);
130
    }
131
}
132