1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale; |
4
|
|
|
|
5
|
|
|
use Thinktomorrow\Locale\Exceptions\InvalidScope; |
6
|
|
|
use Thinktomorrow\Locale\Exceptions\NotDetectedException; |
7
|
|
|
use Thinktomorrow\Locale\Values\Locale; |
8
|
|
|
use Thinktomorrow\Locale\Values\Root; |
9
|
|
|
|
10
|
|
|
class Scope |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $locales; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The active locale. |
19
|
|
|
* |
20
|
|
|
* @var Locale |
21
|
|
|
*/ |
22
|
|
|
private static $activeLocale; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The default locale. In the request path |
26
|
|
|
* it's the hidden segment, e.g. /. |
27
|
|
|
* |
28
|
|
|
* @var Locale |
29
|
|
|
*/ |
30
|
|
|
private $default; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* When the canonical scope has a root set to be |
34
|
|
|
* other than the current, that specific root is defined here |
35
|
|
|
* By default the current request root is of use (NULL). |
36
|
|
|
* |
37
|
|
|
* @var null|Root |
38
|
|
|
*/ |
39
|
|
|
private $customRoot = null; |
40
|
|
|
|
41
|
83 |
|
public function __construct(array $locales) |
42
|
|
|
{ |
43
|
83 |
|
if (!isset($locales['/'])) { |
44
|
1 |
|
throw new InvalidScope('Default locale is required for scope. Add this as \'/\' => locale.'); |
45
|
|
|
} |
46
|
82 |
|
$this->locales = $locales; |
47
|
82 |
|
$this->default = Locale::from($this->locales['/']); |
48
|
82 |
|
} |
49
|
|
|
|
50
|
12 |
|
public function setCustomRoot(Root $customRoot) |
51
|
|
|
{ |
52
|
12 |
|
$this->customRoot = $customRoot; |
53
|
|
|
|
54
|
12 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
10 |
|
public function customRoot(): ?Root |
58
|
|
|
{ |
59
|
10 |
|
return $this->customRoot; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the locale by segment identifier. |
64
|
|
|
* |
65
|
|
|
* @param $segment |
66
|
|
|
* |
67
|
|
|
* @return null|Locale |
68
|
|
|
*/ |
69
|
68 |
|
public function findLocale($segment): ?Locale |
70
|
|
|
{ |
71
|
68 |
|
return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null; |
72
|
|
|
} |
73
|
|
|
|
74
|
32 |
|
public function locales(): array |
75
|
|
|
{ |
76
|
32 |
|
return $this->locales; |
77
|
|
|
} |
78
|
|
|
|
79
|
61 |
|
public function defaultLocale(): Locale |
80
|
|
|
{ |
81
|
61 |
|
return $this->default; |
82
|
|
|
} |
83
|
|
|
|
84
|
19 |
|
public static function activeLocale(): ?Locale |
85
|
|
|
{ |
86
|
19 |
|
if(is_null(static::$activeLocale)){ |
|
|
|
|
87
|
1 |
|
throw new NotDetectedException('Make sure you detect the locale by using the routeprefix helper.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
18 |
|
return static::$activeLocale; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
68 |
|
public static function setActiveLocale(Locale $locale) |
94
|
|
|
{ |
95
|
68 |
|
static::$activeLocale = $locale; |
|
|
|
|
96
|
68 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get the url segment which corresponds with the passed locale. |
100
|
|
|
* |
101
|
|
|
* @param null $locale |
102
|
|
|
* |
103
|
|
|
* @return null|string |
104
|
|
|
*/ |
105
|
35 |
|
public function segment($locale = null): ?string |
106
|
|
|
{ |
107
|
35 |
|
if (is_null($locale)) { |
108
|
2 |
|
return $this->activeSegment(); |
109
|
|
|
} |
110
|
|
|
|
111
|
35 |
|
return ($key = array_search($locale, $this->locales)) ? $key : null; |
112
|
|
|
} |
113
|
|
|
|
114
|
18 |
|
public function activeSegment(): ?string |
115
|
|
|
{ |
116
|
18 |
|
return $this->segment($this->activeLocale()); |
117
|
|
|
} |
118
|
|
|
|
119
|
27 |
|
public function validateLocale(string $locale = null): bool |
120
|
|
|
{ |
121
|
27 |
|
if (!$locale) { |
|
|
|
|
122
|
5 |
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
25 |
|
return in_array($locale, $this->locales); |
126
|
|
|
} |
127
|
|
|
|
128
|
24 |
|
public function validateSegment(string $segment = null): bool |
129
|
|
|
{ |
130
|
24 |
|
if (!$segment) { |
|
|
|
|
131
|
5 |
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
22 |
|
return isset($this->locales[$segment]); |
135
|
|
|
} |
136
|
|
|
|
137
|
1 |
|
public function refresh() |
138
|
|
|
{ |
139
|
1 |
|
static::$activeLocale = null; |
|
|
|
|
140
|
1 |
|
} |
141
|
|
|
} |
142
|
|
|
|
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: