|
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
|
63 |
|
public function __construct(UrlGenerator $generator) |
|
30
|
|
|
{ |
|
31
|
63 |
|
$this->generator = $generator; |
|
32
|
|
|
|
|
33
|
|
|
/* |
|
34
|
|
|
* Default url is the root as given by the application |
|
35
|
|
|
*/ |
|
36
|
63 |
|
$this->url = $this->rootFromApplication(); |
|
37
|
63 |
|
} |
|
38
|
|
|
|
|
39
|
37 |
|
public function get(): string |
|
40
|
|
|
{ |
|
41
|
37 |
|
if (is_bool($this->secure)) { |
|
42
|
28 |
|
$this->url->secure($this->secure); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
37 |
|
return $this->generator->to( |
|
46
|
37 |
|
$this->url->localize($this->localeSegment, $this->available_locales)->get(), $this->parameters, $this->secure |
|
47
|
|
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
38 |
|
public function set(string $url): self |
|
51
|
|
|
{ |
|
52
|
38 |
|
$this->url = Url::fromString($url); |
|
53
|
|
|
|
|
54
|
37 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
8 |
|
public function setCustomRoot(Root $root) |
|
58
|
|
|
{ |
|
59
|
8 |
|
$this->url->setCustomRoot($root); |
|
60
|
|
|
|
|
61
|
8 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
36 |
|
public function localize(string $localeSegment = null, array $available_locales): self |
|
65
|
|
|
{ |
|
66
|
36 |
|
$this->localeSegment = $localeSegment; |
|
67
|
36 |
|
$this->available_locales = $available_locales; |
|
68
|
|
|
|
|
69
|
36 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
7 |
|
public function parameters(array $parameters = []): self |
|
73
|
|
|
{ |
|
74
|
7 |
|
$this->parameters = $parameters; |
|
75
|
|
|
|
|
76
|
7 |
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
28 |
|
public function secure($secure = true): self |
|
80
|
|
|
{ |
|
81
|
28 |
|
$this->secure = (bool) $secure; |
|
82
|
|
|
|
|
83
|
28 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
63 |
|
private function rootFromApplication(): Url |
|
87
|
|
|
{ |
|
88
|
63 |
|
return Url::fromString($this->generator->formatRoot($this->generator->formatScheme($this->secure))); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Resolve the route via the Illuminate UrlGenerator. |
|
93
|
|
|
* |
|
94
|
|
|
* @param $routekey |
|
95
|
|
|
* @param array $parameters |
|
96
|
|
|
* |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
23 |
|
public function resolveRoute($routekey, $parameters = []) |
|
100
|
|
|
{ |
|
101
|
23 |
|
return $this->generator->route($routekey, $parameters, true); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|