1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Locale\Parsers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\UrlGenerator; |
6
|
|
|
use Thinktomorrow\Locale\Locale; |
7
|
|
|
|
8
|
|
|
class UrlParser implements Parser |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var Locale |
12
|
|
|
*/ |
13
|
|
|
private $locale; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* If locale is explicitly passed, we will set it |
17
|
|
|
* If null is passed it means the default locale must be used. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $localeslug = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $parsed; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
private $secure = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $parameters = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Internal flag to keep track of schemeless url. |
40
|
|
|
* |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
private $schemeless = false; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var UrlGenerator |
47
|
|
|
*/ |
48
|
|
|
private $generator; |
49
|
|
|
|
50
|
50 |
|
public function __construct(Locale $locale, UrlGenerator $generator) |
51
|
|
|
{ |
52
|
50 |
|
$this->locale = $locale; |
53
|
50 |
|
$this->generator = $generator; |
54
|
50 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set the original url/uri. |
58
|
|
|
* |
59
|
|
|
* @param $url |
60
|
|
|
* |
61
|
|
|
* @return $this |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
47 |
|
public function set($url) |
65
|
|
|
{ |
66
|
47 |
|
$this->parsed = parse_url($url); |
|
|
|
|
67
|
|
|
|
68
|
47 |
|
if (false === $this->parsed) { |
69
|
1 |
|
throw new \InvalidArgumentException('Failed to parse url. Invalid url ['.$url.'] passed as parameter.'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// If a schemeless url is passed, parse_url will ignore this and strip the first tags |
73
|
|
|
// so we keep a reminder to explicitly reassemble the 'anonymous scheme' manually |
74
|
46 |
|
$this->schemeless = (0 === strpos($url, '//') && isset($this->parsed['host'])); |
75
|
|
|
|
76
|
46 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the localized url. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
47 |
|
public function get() |
85
|
|
|
{ |
86
|
47 |
|
$this->assertUrlExists(); |
87
|
|
|
|
88
|
|
|
// Only localize the url if a locale is passed. |
89
|
46 |
|
if (false !== $this->localeslug) { |
90
|
43 |
|
$this->localizePath($this->localeslug); |
91
|
|
|
} |
92
|
|
|
|
93
|
46 |
|
$url = $this->generator->to($this->reassemble($this->parsed), $this->parameters, $this->secure); |
94
|
|
|
|
95
|
|
|
// Secure url is not enforced by the urlgenerator when a valid url is passed |
96
|
|
|
// So we enforce it after url generation |
97
|
46 |
|
if ($this->secure && 0 === strpos($url, 'http://')) { |
98
|
2 |
|
$url = str_replace('http://', 'https://', $url); |
99
|
|
|
} |
100
|
|
|
|
101
|
46 |
|
return $url; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Resolve the route via the Illuminate UrlGenerator. |
106
|
|
|
* |
107
|
|
|
* @param $routekey |
108
|
|
|
* @param array $parameters |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
27 |
|
public function resolveRoute($routekey, $parameters = []) |
113
|
|
|
{ |
114
|
27 |
|
return $this->generator->route($routekey, $parameters, true); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Place locale segment in front of url path |
119
|
|
|
* e.g. /foo/bar is transformed into /en/foo/bar. |
120
|
|
|
* |
121
|
|
|
* @param null $localeslug |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
45 |
|
public function localize($localeslug = null) |
126
|
|
|
{ |
127
|
45 |
|
$this->localeslug = $localeslug; |
128
|
|
|
|
129
|
45 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param array $parameters |
134
|
|
|
* |
135
|
|
|
* @return $this |
136
|
|
|
*/ |
137
|
11 |
|
public function parameters(array $parameters = []) |
138
|
|
|
{ |
139
|
11 |
|
$this->parameters = $parameters; |
140
|
|
|
|
141
|
11 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param bool $secure |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
13 |
|
public function secure($secure = true) |
150
|
|
|
{ |
151
|
13 |
|
$this->secure = (bool) $secure; |
152
|
|
|
|
153
|
13 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Inject the locale slug in the uri. |
158
|
|
|
* |
159
|
|
|
* @param null $locale |
160
|
|
|
*/ |
161
|
43 |
|
private function localizePath($locale = null) |
162
|
|
|
{ |
163
|
43 |
|
$this->parsed['path'] = str_replace('//', '/', |
164
|
43 |
|
'/'.$this->locale->getSlug($locale).$this->delocalizePath() |
165
|
|
|
); |
166
|
43 |
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
43 |
|
private function delocalizePath() |
172
|
|
|
{ |
173
|
43 |
|
if (!isset($this->parsed['path'])) { |
174
|
7 |
|
return; |
175
|
|
|
} |
176
|
|
|
|
177
|
43 |
|
$path_segments = explode('/', trim($this->parsed['path'], '/')); |
178
|
|
|
|
179
|
43 |
|
if (count($path_segments) < 1) { |
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
|
183
|
43 |
|
if ($this->locale->isHidden($path_segments[0]) || $path_segments[0] === $this->locale->getSlug($path_segments[0])) { |
184
|
12 |
|
unset($path_segments[0]); |
185
|
|
|
} |
186
|
|
|
|
187
|
43 |
|
return '/'.implode('/', $path_segments); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Construct a full url with the parsed url elements |
192
|
|
|
* resulted from a parse_url() function call. |
193
|
|
|
* |
194
|
|
|
* @param array $parsed |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
46 |
|
private function reassemble(array $parsed) |
199
|
|
|
{ |
200
|
46 |
|
$scheme = (isset($parsed['scheme'])) ? $parsed['scheme'].'://' : ($this->schemeless ? '//' : ''); |
201
|
|
|
|
202
|
|
|
return |
203
|
|
|
$scheme |
204
|
46 |
|
.((isset($parsed['user'])) ? $parsed['user'].((isset($parsed['pass'])) ? ':'.$parsed['pass'] : '').'@' : '') |
205
|
46 |
|
.((isset($parsed['host'])) ? $parsed['host'] : '') |
206
|
46 |
|
.((isset($parsed['port'])) ? ':'.$parsed['port'] : '') |
207
|
46 |
|
.((isset($parsed['path'])) ? $parsed['path'] : '') |
208
|
46 |
|
.((isset($parsed['query'])) ? '?'.$parsed['query'] : '') |
209
|
46 |
|
.((isset($parsed['fragment'])) ? '#'.$parsed['fragment'] : ''); |
210
|
|
|
} |
211
|
|
|
|
212
|
47 |
|
private function assertUrlExists() |
213
|
|
|
{ |
214
|
47 |
|
if (!$this->parsed) { |
|
|
|
|
215
|
1 |
|
throw new \LogicException('Url is required. Please run UrlParser::set($url) first.'); |
216
|
|
|
} |
217
|
46 |
|
} |
218
|
|
|
} |
219
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.