|
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
|
75 |
|
public function __construct(array $locales) |
|
34
|
|
|
{ |
|
35
|
75 |
|
if (!isset($locales['/'])) { |
|
36
|
1 |
|
throw new InvalidScope('Default locale is required for scope. Add this as \'/\' => locale.'); |
|
37
|
|
|
} |
|
38
|
75 |
|
$this->locales = $locales; |
|
39
|
75 |
|
$this->default = Locale::from($this->locales['/']); |
|
40
|
75 |
|
} |
|
41
|
|
|
|
|
42
|
11 |
|
public function setCustomRoot(Root $customRoot) |
|
43
|
|
|
{ |
|
44
|
11 |
|
$this->customRoot = $customRoot; |
|
45
|
|
|
|
|
46
|
11 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
9 |
|
public function customRoot(): ?Root |
|
50
|
|
|
{ |
|
51
|
9 |
|
return $this->customRoot; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the locale by segment identifier. |
|
56
|
|
|
* |
|
57
|
|
|
* @param $segment |
|
58
|
|
|
* |
|
59
|
|
|
* @return null|Locale |
|
60
|
|
|
*/ |
|
61
|
64 |
|
public function findLocale($segment): ?Locale |
|
62
|
|
|
{ |
|
63
|
64 |
|
return isset($this->locales[$segment]) ? Locale::from($this->locales[$segment]) : null; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
30 |
|
public function locales(): array |
|
67
|
|
|
{ |
|
68
|
30 |
|
return $this->locales; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
59 |
|
public function defaultLocale(): Locale |
|
72
|
|
|
{ |
|
73
|
59 |
|
return $this->default; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
16 |
|
public function activeLocale(): string |
|
77
|
|
|
{ |
|
78
|
16 |
|
return app()->getLocale(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the url segment which corresponds with the passed locale. |
|
83
|
|
|
* |
|
84
|
|
|
* @param null $locale |
|
85
|
|
|
* |
|
86
|
|
|
* @return null|string |
|
87
|
|
|
*/ |
|
88
|
33 |
|
public function segment($locale = null): ?string |
|
89
|
|
|
{ |
|
90
|
33 |
|
if (is_null($locale)) { |
|
91
|
2 |
|
return $this->activeSegment(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
33 |
|
return ($key = array_search($locale, $this->locales)) ? $key : null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
16 |
|
public function activeSegment(): ?string |
|
98
|
|
|
{ |
|
99
|
16 |
|
return $this->segment($this->activeLocale()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
26 |
|
public function validateLocale(string $locale = null): bool |
|
103
|
|
|
{ |
|
104
|
26 |
|
if (!$locale) { |
|
|
|
|
|
|
105
|
5 |
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
24 |
|
return in_array($locale, $this->locales); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
23 |
|
public function validateSegment(string $segment = null): bool |
|
112
|
|
|
{ |
|
113
|
23 |
|
if (!$segment) { |
|
|
|
|
|
|
114
|
5 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
21 |
|
return isset($this->locales[$segment]); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: