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