@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | |
107 | 107 | $uri = $router->uri('test:id', [ |
108 | 108 | 'id' => 100, |
109 | - 'title' => new class implements \Stringable { |
|
109 | + 'title' => new class implements \Stringable{ |
|
110 | 110 | public function __toString() |
111 | 111 | { |
112 | 112 | return 'hello-world'; |
@@ -201,7 +201,7 @@ discard block |
||
201 | 201 | $uriHandler->setStrict(true); |
202 | 202 | $route = $route->withUriHandler($uriHandler); |
203 | 203 | |
204 | - self::assertSame($expected, (string) $route->uri($params)); |
|
204 | + self::assertSame($expected, (string)$route->uri($params)); |
|
205 | 205 | } |
206 | 206 | |
207 | 207 | #[DataProvider('provideSegmentInDifferentLanguages')] |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | private readonly UriFactoryInterface $uriFactory, |
62 | 62 | ?SlugifyInterface $slugify = null, |
63 | 63 | ?RoutePatternRegistryInterface $patternRegistry = null, |
64 | - ) { |
|
64 | + ){ |
|
65 | 65 | $this->patternRegistry = $patternRegistry ?? new DefaultPatternRegistry(); |
66 | 66 | |
67 | 67 | $slugify ??= new Slugify(); |
@@ -131,7 +131,7 @@ 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 | $basePath .= '/'; |
136 | 136 | } |
137 | 137 | |
@@ -181,12 +181,12 @@ discard block |
||
181 | 181 | */ |
182 | 182 | public function match(UriInterface $uri, array $defaults): ?array |
183 | 183 | { |
184 | - if (!$this->isCompiled()) { |
|
184 | + if (!$this->isCompiled()){ |
|
185 | 185 | $this->compile(); |
186 | 186 | } |
187 | 187 | |
188 | 188 | $matches = []; |
189 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
189 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
190 | 190 | return null; |
191 | 191 | } |
192 | 192 | |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | */ |
204 | 204 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
205 | 205 | { |
206 | - if (!$this->isCompiled()) { |
|
206 | + if (!$this->isCompiled()){ |
|
207 | 207 | $this->compile(); |
208 | 208 | } |
209 | 209 | |
@@ -214,19 +214,19 @@ discard block |
||
214 | 214 | ); |
215 | 215 | |
216 | 216 | $required = \array_keys($this->constrains); |
217 | - if ($this->strict) { |
|
217 | + if ($this->strict){ |
|
218 | 218 | $required = \array_unique([...$this->requiredOptions, ...$required]); |
219 | 219 | } |
220 | 220 | |
221 | 221 | $missingParameters = []; |
222 | 222 | |
223 | - foreach ($required as $key) { |
|
224 | - if (empty($parameters[$key])) { |
|
223 | + foreach ($required as $key){ |
|
224 | + if (empty($parameters[$key])){ |
|
225 | 225 | $missingParameters[] = $key; |
226 | 226 | } |
227 | 227 | } |
228 | 228 | |
229 | - if ($missingParameters !== []) { |
|
229 | + if ($missingParameters !== []){ |
|
230 | 230 | throw new UriHandlerException( |
231 | 231 | \sprintf( |
232 | 232 | \count($missingParameters) === 1 |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | $path = $this->interpolate($this->template, $parameters); |
242 | 242 | |
243 | 243 | //Uri with added base path and prefix |
244 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/')); |
|
244 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/')); |
|
245 | 245 | |
246 | 246 | return empty($query) ? $uri : $uri->withQuery(\http_build_query($query)); |
247 | 247 | } |
@@ -256,23 +256,23 @@ discard block |
||
256 | 256 | $allowed = \array_keys($this->options); |
257 | 257 | |
258 | 258 | $result = []; |
259 | - foreach ($parameters as $key => $parameter) { |
|
260 | - if (\is_int($key) && isset($allowed[$key])) { |
|
259 | + foreach ($parameters as $key => $parameter){ |
|
260 | + if (\is_int($key) && isset($allowed[$key])){ |
|
261 | 261 | // this segment fetched keys from given parameters either by name or by position |
262 | 262 | $key = $allowed[$key]; |
263 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
263 | + } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){ |
|
264 | 264 | // all additional parameters given in array form can be glued to query string |
265 | 265 | $query[$key] = $parameter; |
266 | 266 | continue; |
267 | 267 | } |
268 | 268 | |
269 | 269 | // String must be normalized here |
270 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
270 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
271 | 271 | $result[$key] = ($this->pathSegmentEncoder)($parameter); |
272 | 272 | continue; |
273 | 273 | } |
274 | 274 | |
275 | - $result[$key] = (string) $parameter; |
|
275 | + $result[$key] = (string)$parameter; |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | return $result; |
@@ -285,12 +285,12 @@ discard block |
||
285 | 285 | { |
286 | 286 | $path = $uri->getPath(); |
287 | 287 | |
288 | - if (empty($path) || $path[0] !== '/') { |
|
289 | - $path = '/' . $path; |
|
288 | + if (empty($path) || $path[0] !== '/'){ |
|
289 | + $path = '/'.$path; |
|
290 | 290 | } |
291 | 291 | |
292 | 292 | $uriString = $this->matchHost |
293 | - ? $uri->getHost() . $path |
|
293 | + ? $uri->getHost().$path |
|
294 | 294 | : \substr($path, \strlen($this->basePath)); |
295 | 295 | |
296 | 296 | return \trim($uriString, '/'); |
@@ -304,7 +304,7 @@ discard block |
||
304 | 304 | */ |
305 | 305 | private function compile(): void |
306 | 306 | { |
307 | - if ($this->pattern === null) { |
|
307 | + if ($this->pattern === null){ |
|
308 | 308 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
309 | 309 | } |
310 | 310 | |
@@ -314,19 +314,19 @@ discard block |
||
314 | 314 | // 1) Build full pattern |
315 | 315 | $prefix = \rtrim($this->getPrefix(), '/ '); |
316 | 316 | $pattern = \ltrim($this->pattern, '/ '); |
317 | - $pattern = $prefix . '/' . $pattern; |
|
317 | + $pattern = $prefix.'/'.$pattern; |
|
318 | 318 | $pattern = \rtrim(\ltrim($pattern, ':/'), '/'); |
319 | 319 | |
320 | 320 | // correct [/ first occurrence] |
321 | - if (\str_starts_with($pattern, '[/')) { |
|
322 | - $pattern = '[' . \substr($pattern, 2); |
|
321 | + if (\str_starts_with($pattern, '[/')){ |
|
322 | + $pattern = '['.\substr($pattern, 2); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | // 2) Extract variables from the pattern |
326 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
326 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
327 | 327 | $variables = \array_combine($matches[1], $matches[2]); |
328 | 328 | |
329 | - foreach ($variables as $key => $segment) { |
|
329 | + foreach ($variables as $key => $segment){ |
|
330 | 330 | $segment = $this->prepareSegment($key, $segment); |
331 | 331 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
332 | 332 | $options[] = $key; |
@@ -338,14 +338,14 @@ discard block |
||
338 | 338 | $options = \array_fill_keys($options, null); |
339 | 339 | |
340 | 340 | // 3) Validate constraints |
341 | - foreach ($this->constrains as $key => $value) { |
|
342 | - if ($value instanceof Autofill) { |
|
341 | + foreach ($this->constrains as $key => $value){ |
|
342 | + if ($value instanceof Autofill){ |
|
343 | 343 | // only forces value replacement, not required to be presented as parameter |
344 | 344 | continue; |
345 | 345 | } |
346 | 346 | |
347 | 347 | // 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])) { |
|
348 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
349 | 349 | throw new ConstrainException( |
350 | 350 | \sprintf( |
351 | 351 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -357,12 +357,12 @@ discard block |
||
357 | 357 | } |
358 | 358 | |
359 | 359 | // 4) Compile your final regex pattern |
360 | - $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
360 | + $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
361 | 361 | $this->template = \stripslashes(\str_replace('?', '', $template)); |
362 | 362 | $this->options = $options; |
363 | 363 | |
364 | 364 | // 5) Mark which parameters are required vs. optional |
365 | - if ($this->strict) { |
|
365 | + if ($this->strict){ |
|
366 | 366 | $this->requiredOptions = $this->findRequiredOptions($pattern, \array_keys($options)); |
367 | 367 | } |
368 | 368 | } |
@@ -384,21 +384,21 @@ discard block |
||
384 | 384 | $pos = 0; |
385 | 385 | $length = \strlen($pattern); |
386 | 386 | |
387 | - while ($pos < $length) { |
|
387 | + while ($pos < $length){ |
|
388 | 388 | $char = $pattern[$pos]; |
389 | 389 | |
390 | - if ($char === '[') { |
|
390 | + if ($char === '['){ |
|
391 | 391 | // We enter an optional segment |
392 | 392 | ++$optLevel; |
393 | - } elseif ($char === ']') { |
|
393 | + } elseif ($char === ']'){ |
|
394 | 394 | // We exit an optional segment |
395 | 395 | $optLevel = \max(0, $optLevel - 1); |
396 | - } elseif ($char === '<') { |
|
396 | + } elseif ($char === '<'){ |
|
397 | 397 | // We see a parameter like <id> or <action:\d+> |
398 | 398 | |
399 | 399 | // Find the closing '>' |
400 | 400 | $endPos = \strpos($pattern, '>', $pos); |
401 | - if ($endPos === false) { |
|
401 | + if ($endPos === false){ |
|
402 | 402 | break; |
403 | 403 | } |
404 | 404 | |
@@ -432,9 +432,9 @@ discard block |
||
432 | 432 | private function interpolate(string $string, array $values): string |
433 | 433 | { |
434 | 434 | $replaces = []; |
435 | - foreach ($values as $key => $value) { |
|
435 | + foreach ($values as $key => $value){ |
|
436 | 436 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
437 | - $value instanceof \Stringable || \is_scalar($value) => (string) $value, |
|
437 | + $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
|
438 | 438 | default => '', |
439 | 439 | }; |
440 | 440 | } |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | '|', |
459 | 459 | \array_map(fn(string $segment): string => $this->filterSegment($segment), $this->constrains[$name]), |
460 | 460 | ), |
461 | - default => $this->filterSegment((string) $this->constrains[$name]) |
|
461 | + default => $this->filterSegment((string)$this->constrains[$name]) |
|
462 | 462 | }; |
463 | 463 | } |
464 | 464 |
@@ -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 => '', |