|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale\Parsers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Translation\Translator; |
|
6
|
|
|
use Thinktomorrow\Locale\Values\ApplicationLocale; |
|
7
|
|
|
use Thinktomorrow\Locale\Values\Root; |
|
8
|
|
|
|
|
9
|
|
|
class RouteParser |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var string */ |
|
12
|
|
|
private $routename; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $locale; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $localeSegment = null; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
private $available_locales = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var bool */ |
|
24
|
|
|
private $secure = null; |
|
25
|
|
|
|
|
26
|
|
|
/** @var array */ |
|
27
|
|
|
private $parameters = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var Root */ |
|
30
|
|
|
private $customRoot; |
|
31
|
|
|
|
|
32
|
|
|
/** @var Translator */ |
|
33
|
|
|
private $translator; |
|
34
|
|
|
|
|
35
|
|
|
/** @var UrlParser */ |
|
36
|
|
|
private $urlParser; |
|
37
|
|
|
|
|
38
|
74 |
|
public function __construct(UrlParser $urlParser, Translator $translator) |
|
39
|
|
|
{ |
|
40
|
74 |
|
$this->urlParser = $urlParser; |
|
41
|
74 |
|
$this->translator = $translator; |
|
42
|
74 |
|
} |
|
43
|
|
|
|
|
44
|
31 |
|
public function get(): string |
|
45
|
|
|
{ |
|
46
|
31 |
|
$translationKey = config('thinktomorrow.locale.routes_filename').'.'.$this->routename; |
|
47
|
|
|
|
|
48
|
31 |
|
$url = $this->translator->get($translationKey, [], $this->locale); |
|
49
|
|
|
|
|
50
|
|
|
// If route translation does not exist, laravel returns us back the langkey. |
|
51
|
31 |
|
$url = ($url == $translationKey) |
|
52
|
27 |
|
? $this->resolveRoute($this->routename, $this->parameters) |
|
53
|
29 |
|
: UriParameters::replace($url, $this->parameters); |
|
54
|
|
|
|
|
55
|
29 |
|
$parser = $this->urlParser->set($url)->secure($this->secure)->localize($this->localeSegment, $this->available_locales); |
|
56
|
|
|
|
|
57
|
29 |
|
if ($this->customRoot) { |
|
58
|
9 |
|
$parser->setCustomRoot($this->customRoot); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
29 |
|
return $parser->get(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
31 |
|
public function set(string $routename, array $parameters = [], $secure = null) |
|
65
|
|
|
{ |
|
66
|
31 |
|
$this->reset(); |
|
67
|
|
|
|
|
68
|
31 |
|
$this->routename = $routename; |
|
69
|
31 |
|
$this->parameters = SanitizeParameters::rawurlencode($parameters); |
|
70
|
31 |
|
$this->secure = $secure; |
|
71
|
|
|
|
|
72
|
31 |
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
31 |
|
private function reset() |
|
76
|
|
|
{ |
|
77
|
31 |
|
$this->routename = null; |
|
78
|
31 |
|
$this->customRoot = null; |
|
79
|
31 |
|
$this->secure = null; |
|
80
|
31 |
|
$this->parameters = []; |
|
81
|
31 |
|
$this->locale = null; |
|
82
|
31 |
|
$this->localeSegment = null; |
|
83
|
31 |
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
public function setCustomRoot(Root $customRoot) |
|
86
|
|
|
{ |
|
87
|
9 |
|
$this->customRoot = $customRoot; |
|
88
|
|
|
|
|
89
|
9 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
30 |
|
public function localize(string $localeSegment = null, array $available_locales): self |
|
93
|
|
|
{ |
|
94
|
30 |
|
$this->localeSegment = $localeSegment; |
|
95
|
30 |
|
$this->available_locales = $available_locales; |
|
96
|
|
|
|
|
97
|
|
|
// Our route translator requires the corresponding application locale |
|
98
|
30 |
|
$locale = (!$localeSegment || $localeSegment == '/') |
|
|
|
|
|
|
99
|
14 |
|
? $available_locales['/'] |
|
100
|
30 |
|
: $available_locales[$localeSegment]; |
|
101
|
|
|
|
|
102
|
30 |
|
$this->locale = ApplicationLocale::from($locale)->__toString(); |
|
103
|
|
|
|
|
104
|
30 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
27 |
|
public function resolveRoute($routekey, $parameters = []) |
|
108
|
|
|
{ |
|
109
|
27 |
|
return $this->urlParser->resolveRoute($routekey, $parameters); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
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: