1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale\Parsers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\UrlGenerator; |
6
|
|
|
use Thinktomorrow\Locale\Values\Root; |
7
|
|
|
use Thinktomorrow\Locale\Values\Url; |
8
|
|
|
|
9
|
|
|
class UrlParser |
10
|
|
|
{ |
11
|
|
|
/** @var Url */ |
12
|
|
|
private $url; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
private $localeSegment = null; |
16
|
|
|
|
17
|
|
|
/** @var array */ |
18
|
|
|
private $available_locales = []; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
private $secure; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
private $parameters = []; |
25
|
|
|
|
26
|
|
|
/** @var UrlGenerator */ |
27
|
|
|
private $generator; |
28
|
|
|
|
29
|
|
|
public function __construct(UrlGenerator $generator) |
30
|
|
|
{ |
31
|
|
|
$this->generator = $generator; |
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
* Default url is the root as given by the application |
35
|
|
|
*/ |
36
|
|
|
$this->url = $this->rootFromApplication(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function get(): string |
40
|
|
|
{ |
41
|
|
|
if (is_bool($this->secure)) { |
42
|
|
|
$this->url->secure($this->secure); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->generator->to( |
46
|
|
|
$this->url->localize($this->localeSegment, $this->available_locales)->get(), |
47
|
|
|
$this->parameters, |
48
|
|
|
$this->secure |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function set(string $url): self |
53
|
|
|
{ |
54
|
|
|
$this->url = Url::fromString($url); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setCustomRoot(Root $root) |
60
|
|
|
{ |
61
|
|
|
$this->url->setCustomRoot($root); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function localize(string $localeSegment = null, array $available_locales): self |
67
|
|
|
{ |
68
|
|
|
$this->localeSegment = $localeSegment; |
69
|
|
|
$this->available_locales = $available_locales; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function parameters(array $parameters = []): self |
75
|
|
|
{ |
76
|
|
|
$this->parameters = $parameters; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function secure($secure = true): self |
82
|
|
|
{ |
83
|
|
|
$this->secure = (bool) $secure; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function rootFromApplication(): Url |
89
|
|
|
{ |
90
|
|
|
return Url::fromString($this->generator->formatRoot($this->generator->formatScheme($this->secure))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Resolve the route via the Illuminate UrlGenerator. |
95
|
|
|
* |
96
|
|
|
* @param $routekey |
97
|
|
|
* @param array $parameters |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function resolveRoute($routekey, $parameters = []) |
102
|
|
|
{ |
103
|
|
|
return $this->generator->route($routekey, $parameters, true); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|