@@ -131,7 +131,8 @@ discard block |
||
131 | 131 | */ |
132 | 132 | public function withBasePath(string $basePath): self |
133 | 133 | { |
134 | - if (!\str_ends_with($basePath, '/')) { |
|
134 | + if (!\str_ends_with($basePath, '/')) |
|
135 | + { |
|
135 | 136 | $basePath .= '/'; |
136 | 137 | } |
137 | 138 | |
@@ -181,12 +182,14 @@ discard block |
||
181 | 182 | */ |
182 | 183 | public function match(UriInterface $uri, array $defaults): ?array |
183 | 184 | { |
184 | - if (!$this->isCompiled()) { |
|
185 | + if (!$this->isCompiled()) |
|
186 | + { |
|
185 | 187 | $this->compile(); |
186 | 188 | } |
187 | 189 | |
188 | 190 | $matches = []; |
189 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
191 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
192 | + { |
|
190 | 193 | return null; |
191 | 194 | } |
192 | 195 | |
@@ -203,7 +206,8 @@ discard block |
||
203 | 206 | */ |
204 | 207 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
205 | 208 | { |
206 | - if (!$this->isCompiled()) { |
|
209 | + if (!$this->isCompiled()) |
|
210 | + { |
|
207 | 211 | $this->compile(); |
208 | 212 | } |
209 | 213 | |
@@ -214,19 +218,23 @@ discard block |
||
214 | 218 | ); |
215 | 219 | |
216 | 220 | $required = \array_keys($this->constrains); |
217 | - if ($this->strict) { |
|
221 | + if ($this->strict) |
|
222 | + { |
|
218 | 223 | $required = \array_unique([...$this->requiredOptions, ...$required]); |
219 | 224 | } |
220 | 225 | |
221 | 226 | $missingParameters = []; |
222 | 227 | |
223 | - foreach ($required as $key) { |
|
224 | - if (empty($parameters[$key])) { |
|
228 | + foreach ($required as $key) |
|
229 | + { |
|
230 | + if (empty($parameters[$key])) |
|
231 | + { |
|
225 | 232 | $missingParameters[] = $key; |
226 | 233 | } |
227 | 234 | } |
228 | 235 | |
229 | - if ($missingParameters !== []) { |
|
236 | + if ($missingParameters !== []) |
|
237 | + { |
|
230 | 238 | throw new UriHandlerException( |
231 | 239 | \sprintf( |
232 | 240 | \count($missingParameters) === 1 |
@@ -256,18 +264,23 @@ discard block |
||
256 | 264 | $allowed = \array_keys($this->options); |
257 | 265 | |
258 | 266 | $result = []; |
259 | - foreach ($parameters as $key => $parameter) { |
|
260 | - if (\is_int($key) && isset($allowed[$key])) { |
|
267 | + foreach ($parameters as $key => $parameter) |
|
268 | + { |
|
269 | + if (\is_int($key) && isset($allowed[$key])) |
|
270 | + { |
|
261 | 271 | // this segment fetched keys from given parameters either by name or by position |
262 | 272 | $key = $allowed[$key]; |
263 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
273 | + } |
|
274 | + elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) |
|
275 | + { |
|
264 | 276 | // all additional parameters given in array form can be glued to query string |
265 | 277 | $query[$key] = $parameter; |
266 | 278 | continue; |
267 | 279 | } |
268 | 280 | |
269 | 281 | // String must be normalized here |
270 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
282 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
283 | + { |
|
271 | 284 | $result[$key] = ($this->pathSegmentEncoder)($parameter); |
272 | 285 | continue; |
273 | 286 | } |
@@ -285,7 +298,8 @@ discard block |
||
285 | 298 | { |
286 | 299 | $path = $uri->getPath(); |
287 | 300 | |
288 | - if (empty($path) || $path[0] !== '/') { |
|
301 | + if (empty($path) || $path[0] !== '/') |
|
302 | + { |
|
289 | 303 | $path = '/' . $path; |
290 | 304 | } |
291 | 305 | |
@@ -304,7 +318,8 @@ discard block |
||
304 | 318 | */ |
305 | 319 | private function compile(): void |
306 | 320 | { |
307 | - if ($this->pattern === null) { |
|
321 | + if ($this->pattern === null) |
|
322 | + { |
|
308 | 323 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
309 | 324 | } |
310 | 325 | |
@@ -318,15 +333,18 @@ discard block |
||
318 | 333 | $pattern = \rtrim(\ltrim($pattern, ':/'), '/'); |
319 | 334 | |
320 | 335 | // correct [/ first occurrence] |
321 | - if (\str_starts_with($pattern, '[/')) { |
|
336 | + if (\str_starts_with($pattern, '[/')) |
|
337 | + { |
|
322 | 338 | $pattern = '[' . \substr($pattern, 2); |
323 | 339 | } |
324 | 340 | |
325 | 341 | // 2) Extract variables from the pattern |
326 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
342 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
343 | + { |
|
327 | 344 | $variables = \array_combine($matches[1], $matches[2]); |
328 | 345 | |
329 | - foreach ($variables as $key => $segment) { |
|
346 | + foreach ($variables as $key => $segment) |
|
347 | + { |
|
330 | 348 | $segment = $this->prepareSegment($key, $segment); |
331 | 349 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
332 | 350 | $options[] = $key; |
@@ -338,14 +356,17 @@ discard block |
||
338 | 356 | $options = \array_fill_keys($options, null); |
339 | 357 | |
340 | 358 | // 3) Validate constraints |
341 | - foreach ($this->constrains as $key => $value) { |
|
342 | - if ($value instanceof Autofill) { |
|
359 | + foreach ($this->constrains as $key => $value) |
|
360 | + { |
|
361 | + if ($value instanceof Autofill) |
|
362 | + { |
|
343 | 363 | // only forces value replacement, not required to be presented as parameter |
344 | 364 | continue; |
345 | 365 | } |
346 | 366 | |
347 | 367 | // If a constraint references a param that doesn't appear in the pattern or defaults |
348 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
368 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
369 | + { |
|
349 | 370 | throw new ConstrainException( |
350 | 371 | \sprintf( |
351 | 372 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -362,7 +383,8 @@ discard block |
||
362 | 383 | $this->options = $options; |
363 | 384 | |
364 | 385 | // 5) Mark which parameters are required vs. optional |
365 | - if ($this->strict) { |
|
386 | + if ($this->strict) |
|
387 | + { |
|
366 | 388 | $this->requiredOptions = $this->findRequiredOptions($pattern, \array_keys($options)); |
367 | 389 | } |
368 | 390 | } |
@@ -384,21 +406,28 @@ discard block |
||
384 | 406 | $pos = 0; |
385 | 407 | $length = \strlen($pattern); |
386 | 408 | |
387 | - while ($pos < $length) { |
|
409 | + while ($pos < $length) |
|
410 | + { |
|
388 | 411 | $char = $pattern[$pos]; |
389 | 412 | |
390 | - if ($char === '[') { |
|
413 | + if ($char === '[') |
|
414 | + { |
|
391 | 415 | // We enter an optional segment |
392 | 416 | ++$optLevel; |
393 | - } elseif ($char === ']') { |
|
417 | + } |
|
418 | + elseif ($char === ']') |
|
419 | + { |
|
394 | 420 | // We exit an optional segment |
395 | 421 | $optLevel = \max(0, $optLevel - 1); |
396 | - } elseif ($char === '<') { |
|
422 | + } |
|
423 | + elseif ($char === '<') |
|
424 | + { |
|
397 | 425 | // We see a parameter like <id> or <action:\d+> |
398 | 426 | |
399 | 427 | // Find the closing '>' |
400 | 428 | $endPos = \strpos($pattern, '>', $pos); |
401 | - if ($endPos === false) { |
|
429 | + if ($endPos === false) |
|
430 | + { |
|
402 | 431 | break; |
403 | 432 | } |
404 | 433 | |
@@ -432,7 +461,8 @@ discard block |
||
432 | 461 | private function interpolate(string $string, array $values): string |
433 | 462 | { |
434 | 463 | $replaces = []; |
435 | - foreach ($values as $key => $value) { |
|
464 | + foreach ($values as $key => $value) |
|
465 | + { |
|
436 | 466 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
437 | 467 | $value instanceof \Stringable || \is_scalar($value) => (string) $value, |
438 | 468 | default => '', |