Passed
Branch 2.0 (72e182)
by Philippe
02:18
created

Scope   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A setCustomRoot() 0 6 1
A customRoot() 0 4 1
A findLocale() 0 4 2
A locales() 0 4 1
A defaultLocale() 0 4 1
A activeLocale() 0 4 1
A segment() 0 6 3
A activeSegment() 0 4 1
A validateLocale() 0 6 2
A validateSegment() 0 6 2
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 default locale. In the request path
18
     * it's the hidden segment, e.g. /
19
     *
20
     * @var Locale
21
     */
22
    private $default;
23
24
    /**
25
     * When the canonical scope has a root set to be
26
     * other than the current, that specific root is defined here
27
     * By default the current request root is of use (NULL)
28
     *
29
     * @var null|Root
30
     */
31
    private $customRoot = null;
32
33 95
    public function __construct(array $locales)
34
    {
35 95
        if(!isset($locales['/'])) throw new InvalidScope('Default locale is required for scope.');
36
37 95
        $this->locales = $locales;
38 95
        $this->default = Locale::from($this->locales['/']);
39 95
    }
40
41 11
    public function setCustomRoot(Root $customRoot)
42
    {
43 11
        $this->customRoot = $customRoot;
44
45 11
        return $this;
46
    }
47
48 11
    public function customRoot(): ?Root
49
    {
50 11
        return $this->customRoot;
51
    }
52
53
    /**
54
     * Get the locale by segment identifier
55
     *
56
     * @param $segment
57
     * @return null|Locale
58
     */
59 43
    public function findLocale($segment): ?Locale
60
    {
61 43
        return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null;
62
    }
63
64 45
    public function locales(): array
65
    {
66 45
        return $this->locales;
67
    }
68
69 15
    public function defaultLocale(): Locale
70
    {
71 15
        return $this->default;
72
    }
73
74 19
    public function activeLocale(): string
75
    {
76 19
        return app()->getLocale();
77
    }
78
79
    /**
80
     * Get the url segment which corresponds with the passed locale
81
     *
82
     * @param null $locale
83
     * @return null|string
84
     */
85 45
    public function segment($locale = null): ?string
86
    {
87 45
        if(is_null($locale)) return $this->activeSegment();
88
89 45
        return ($key = array_search($locale, $this->locales)) ? $key : null;
90
    }
91
92 19
    public function activeSegment(): ?string
93
    {
94 19
        return $this->segment($this->activeLocale());
95
    }
96
97 34
    public function validateLocale(string $locale = null): bool
98
    {
99 34
        if(!$locale) return false;
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...
100
101 30
        return (in_array($locale, $this->locales));
102
    }
103
104 31
    public function validateSegment(string $segment = null): bool
105
    {
106 31
        if(!$segment) return false;
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...
107
108 27
        return isset($this->locales[$segment]);
109
    }
110
}