|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ReRoute\Template; |
|
4
|
|
|
|
|
5
|
|
|
use ReRoute\ParameterStore; |
|
6
|
|
|
use ReRoute\RequestContext; |
|
7
|
|
|
use ReRoute\Url; |
|
8
|
|
|
use ReRoute\UrlBuilder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @package ReRoute\Template |
|
12
|
|
|
*/ |
|
13
|
|
|
class UrlTemplate { |
|
14
|
|
|
|
|
15
|
|
|
use ParameterStore; |
|
16
|
|
|
|
|
17
|
|
|
CONST PARAMETER_SCHEME = 'scheme'; |
|
18
|
|
|
|
|
19
|
|
|
CONST PARAMETER_HOST = 'host'; |
|
20
|
|
|
|
|
21
|
|
|
CONST PARAMETER_PATH = 'path'; |
|
22
|
|
|
|
|
23
|
|
|
CONST PARAMETER_METHOD = 'method'; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Template |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $pathTemplate = null; |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Template |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $hostTemplate = null; |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $scheme = null; |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $method = null; |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* UrlTemplate constructor. |
|
52
|
|
|
* @param array $parameters |
|
53
|
|
|
* @throws \InvalidArgumentException |
|
54
|
|
|
*/ |
|
55
|
120 |
|
public function __construct(array $parameters = []) { |
|
56
|
120 |
|
foreach ($parameters as $key => $value) { |
|
57
|
102 |
|
if ($key == self::PARAMETER_METHOD) { |
|
58
|
39 |
|
$this->setMethod($value); |
|
59
|
39 |
|
continue; |
|
60
|
|
|
} |
|
61
|
102 |
|
if ($key == self::PARAMETER_SCHEME) { |
|
62
|
69 |
|
$this->setScheme($value); |
|
63
|
69 |
|
continue; |
|
64
|
|
|
} |
|
65
|
102 |
|
if ($key == self::PARAMETER_HOST) { |
|
66
|
90 |
|
$this->setHostTemplate($value); |
|
67
|
90 |
|
continue; |
|
68
|
|
|
} |
|
69
|
93 |
|
if ($key == self::PARAMETER_PATH) { |
|
70
|
90 |
|
$this->setPathTemplate($value); |
|
71
|
90 |
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
throw new \InvalidArgumentException('Unsupported parameter: ' . $key); |
|
75
|
117 |
|
} |
|
76
|
117 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param RequestContext $requestContext |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
90 |
|
public function isMatched(RequestContext $requestContext) { |
|
84
|
90 |
|
if (!empty($this->pathTemplate)) { |
|
85
|
78 |
|
if ($this->pathTemplate->match($requestContext->getPath(), $matchedParams)) { |
|
86
|
66 |
|
$this->storeDefaultParameters($matchedParams); |
|
|
|
|
|
|
87
|
66 |
|
} else { |
|
88
|
24 |
|
return false; |
|
89
|
|
|
} |
|
90
|
66 |
|
} |
|
91
|
|
|
|
|
92
|
84 |
|
if (!empty($this->hostTemplate)) { |
|
93
|
75 |
|
if ($this->hostTemplate->match($requestContext->getHost(), $matchedParams)) { |
|
94
|
54 |
|
$this->storeParameters($matchedParams); |
|
|
|
|
|
|
95
|
54 |
|
} else { |
|
96
|
33 |
|
return false; |
|
97
|
|
|
} |
|
98
|
54 |
|
} |
|
99
|
|
|
|
|
100
|
63 |
|
if (!empty($this->scheme)) { |
|
101
|
36 |
|
if (!in_array($requestContext->getScheme(), explode('|', $this->scheme))) { |
|
102
|
6 |
|
return false; |
|
103
|
|
|
} |
|
104
|
33 |
|
} |
|
105
|
|
|
|
|
106
|
60 |
|
if (!empty($this->method)) { |
|
107
|
24 |
|
if (!in_array(strtolower($requestContext->getMethod()), explode('|', $this->method))) { |
|
108
|
9 |
|
return false; |
|
109
|
|
|
} |
|
110
|
18 |
|
} |
|
111
|
|
|
|
|
112
|
54 |
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Url $url |
|
118
|
|
|
* @param UrlBuilder $urlBuilder |
|
119
|
|
|
*/ |
|
120
|
27 |
|
public function build(Url $url, UrlBuilder $urlBuilder) { |
|
121
|
27 |
|
if (!empty($this->hostTemplate)) { |
|
122
|
24 |
|
$url->setHost($this->templateBuild($this->hostTemplate, $urlBuilder)); |
|
123
|
24 |
|
} |
|
124
|
|
|
|
|
125
|
27 |
|
if (!empty($this->pathTemplate)) { |
|
126
|
27 |
|
$url->setPath($this->templateBuild($this->pathTemplate, $urlBuilder)); |
|
127
|
24 |
|
} |
|
128
|
|
|
|
|
129
|
27 |
|
if (!empty($this->scheme)) { |
|
130
|
18 |
|
$url->setScheme($this->scheme); |
|
131
|
18 |
|
} |
|
132
|
27 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param Template $template |
|
137
|
|
|
* @param UrlBuilder $urlBuilder |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
27 |
|
protected function templateBuild(Template $template, UrlBuilder $urlBuilder) { |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
27 |
|
$path = $template->build($urlBuilder->getAllParameters(), $usedParameters); |
|
144
|
27 |
|
foreach ($usedParameters as $name) { |
|
|
|
|
|
|
145
|
9 |
|
$urlBuilder->useParameter($name); |
|
146
|
27 |
|
} |
|
147
|
|
|
|
|
148
|
27 |
|
return $path; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return Template |
|
154
|
|
|
*/ |
|
155
|
6 |
|
public function getPathTemplate() { |
|
156
|
6 |
|
return $this->pathTemplate; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $pathTemplate |
|
162
|
|
|
* |
|
163
|
|
|
* @return $this |
|
164
|
|
|
*/ |
|
165
|
111 |
|
public function setPathTemplate($pathTemplate) { |
|
166
|
111 |
|
if (is_string($pathTemplate)) { |
|
167
|
111 |
|
$this->pathTemplate = new Template($pathTemplate); |
|
168
|
111 |
|
} elseif ($pathTemplate instanceof Template) { |
|
169
|
|
|
$this->pathTemplate = $pathTemplate; |
|
170
|
|
|
} else { |
|
171
|
|
|
throw new \InvalidArgumentException("Invalid pathTemplate parameter. Expect string or instance of Template. Given:" . gettype($pathTemplate)); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
111 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return Template |
|
180
|
|
|
*/ |
|
181
|
6 |
|
public function getHostTemplate() { |
|
182
|
6 |
|
return $this->hostTemplate; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param string $hostTemplate |
|
188
|
|
|
* |
|
189
|
|
|
* @return $this |
|
190
|
|
|
*/ |
|
191
|
108 |
|
public function setHostTemplate($hostTemplate) { |
|
192
|
108 |
|
if (is_string($hostTemplate)) { |
|
193
|
108 |
|
$this->hostTemplate = new Template($hostTemplate); |
|
194
|
108 |
|
} elseif ($hostTemplate instanceof Template) { |
|
195
|
|
|
$this->hostTemplate = $hostTemplate; |
|
196
|
|
|
} else { |
|
197
|
|
|
throw new \InvalidArgumentException("Invalid hostTemplate parameter. Expect string or instance of Template. Given:" . gettype($hostTemplate)); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
108 |
|
return $this; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
6 |
|
public function getScheme() { |
|
208
|
6 |
|
return $this->scheme; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param string|\string[] $scheme |
|
214
|
|
|
* |
|
215
|
|
|
* @return $this |
|
216
|
|
|
*/ |
|
217
|
90 |
|
public function setScheme($scheme) { |
|
218
|
90 |
|
$this->scheme = strtolower($scheme); |
|
219
|
90 |
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
6 |
|
public function getMethod() { |
|
227
|
6 |
|
return $this->method; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string|\string[] $method |
|
233
|
|
|
* |
|
234
|
|
|
* @return $this |
|
235
|
|
|
*/ |
|
236
|
45 |
|
public function setMethod($method) { |
|
237
|
45 |
|
$this->method = strtolower($method); |
|
238
|
45 |
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: