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; |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
72 |
|
public static function setActiveLocale(Locale $locale) |
89
|
|
|
{ |
90
|
72 |
|
static::$activeLocale = $locale; |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
126
|
5 |
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
25 |
|
return isset($this->locales[$segment]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Let’s assume you have a class which uses late-static binding:
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:In the case above, it makes sense to update
SomeClass
to useself
instead: