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; |
|
|
|
|
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; |
|
|
|
|
107
|
|
|
|
108
|
27 |
|
return isset($this->locales[$segment]); |
109
|
|
|
} |
110
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
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: