@@ -103,7 +103,7 @@ |
||
| 103 | 103 | |
| 104 | 104 | $uri = $router->uri('test:id', [ |
| 105 | 105 | 'id' => 100, |
| 106 | - 'title' => new class implements \Stringable { |
|
| 106 | + 'title' => new class implements \Stringable{ |
|
| 107 | 107 | public function __toString() |
| 108 | 108 | { |
| 109 | 109 | return 'hello-world'; |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | private readonly UriFactoryInterface $uriFactory, |
| 58 | 58 | SlugifyInterface $slugify = null, |
| 59 | 59 | ?RoutePatternRegistryInterface $patternRegistry = null, |
| 60 | - ) { |
|
| 60 | + ){ |
|
| 61 | 61 | $this->patternRegistry = $patternRegistry ?? new DefaultPatternRegistry(); |
| 62 | 62 | |
| 63 | 63 | $slugify ??= new Slugify(); |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function withBasePath(string $basePath): self |
| 124 | 124 | { |
| 125 | - if (!\str_ends_with($basePath, '/')) { |
|
| 125 | + if (!\str_ends_with($basePath, '/')){ |
|
| 126 | 126 | $basePath .= '/'; |
| 127 | 127 | } |
| 128 | 128 | |
@@ -172,12 +172,12 @@ discard block |
||
| 172 | 172 | */ |
| 173 | 173 | public function match(UriInterface $uri, array $defaults): ?array |
| 174 | 174 | { |
| 175 | - if (!$this->isCompiled()) { |
|
| 175 | + if (!$this->isCompiled()){ |
|
| 176 | 176 | $this->compile(); |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $matches = []; |
| 180 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 180 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
| 181 | 181 | return null; |
| 182 | 182 | } |
| 183 | 183 | |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | */ |
| 195 | 195 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 196 | 196 | { |
| 197 | - if (!$this->isCompiled()) { |
|
| 197 | + if (!$this->isCompiled()){ |
|
| 198 | 198 | $this->compile(); |
| 199 | 199 | } |
| 200 | 200 | |
@@ -204,8 +204,8 @@ discard block |
||
| 204 | 204 | $this->fetchOptions($parameters, $query) |
| 205 | 205 | ); |
| 206 | 206 | |
| 207 | - foreach ($this->constrains as $key => $_) { |
|
| 208 | - if (empty($parameters[$key])) { |
|
| 207 | + foreach ($this->constrains as $key => $_){ |
|
| 208 | + if (empty($parameters[$key])){ |
|
| 209 | 209 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 210 | 210 | } |
| 211 | 211 | } |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | $path = $this->interpolate($this->template, $parameters); |
| 215 | 215 | |
| 216 | 216 | //Uri with added base path and prefix |
| 217 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/')); |
|
| 217 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/')); |
|
| 218 | 218 | |
| 219 | 219 | return empty($query) ? $uri : $uri->withQuery(\http_build_query($query)); |
| 220 | 220 | } |
@@ -229,18 +229,18 @@ discard block |
||
| 229 | 229 | $allowed = \array_keys($this->options); |
| 230 | 230 | |
| 231 | 231 | $result = []; |
| 232 | - foreach ($parameters as $key => $parameter) { |
|
| 233 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 232 | + foreach ($parameters as $key => $parameter){ |
|
| 233 | + if (\is_int($key) && isset($allowed[$key])){ |
|
| 234 | 234 | // this segment fetched keys from given parameters either by name or by position |
| 235 | 235 | $key = $allowed[$key]; |
| 236 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 236 | + } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){ |
|
| 237 | 237 | // all additional parameters given in array form can be glued to query string |
| 238 | 238 | $query[$key] = $parameter; |
| 239 | 239 | continue; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // String must be normalized here |
| 243 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 243 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
| 244 | 244 | $result[$key] = ($this->pathSegmentEncoder)($parameter); |
| 245 | 245 | continue; |
| 246 | 246 | } |
@@ -258,15 +258,15 @@ discard block |
||
| 258 | 258 | { |
| 259 | 259 | $path = $uri->getPath(); |
| 260 | 260 | |
| 261 | - if (empty($path) || $path[0] !== '/') { |
|
| 262 | - $path = '/' . $path; |
|
| 261 | + if (empty($path) || $path[0] !== '/'){ |
|
| 262 | + $path = '/'.$path; |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | - if ($this->matchHost) { |
|
| 266 | - $uriString = $uri->getHost() . $path; |
|
| 267 | - } else { |
|
| 265 | + if ($this->matchHost){ |
|
| 266 | + $uriString = $uri->getHost().$path; |
|
| 267 | + }else{ |
|
| 268 | 268 | $uriString = \substr($path, \strlen($this->basePath)); |
| 269 | - if ($uriString === false) { |
|
| 269 | + if ($uriString === false){ |
|
| 270 | 270 | $uriString = ''; |
| 271 | 271 | } |
| 272 | 272 | } |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | */ |
| 283 | 283 | private function compile(): void |
| 284 | 284 | { |
| 285 | - if ($this->pattern === null) { |
|
| 285 | + if ($this->pattern === null){ |
|
| 286 | 286 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 287 | 287 | } |
| 288 | 288 | |
@@ -291,18 +291,18 @@ discard block |
||
| 291 | 291 | |
| 292 | 292 | $prefix = \rtrim($this->getPrefix(), '/ '); |
| 293 | 293 | $pattern = \ltrim($this->pattern, '/ '); |
| 294 | - $pattern = $prefix . '/' . $pattern; |
|
| 294 | + $pattern = $prefix.'/'.$pattern; |
|
| 295 | 295 | $pattern = \rtrim(\ltrim($pattern, ':/'), '/'); |
| 296 | 296 | |
| 297 | 297 | // correct [/ first occurrence] |
| 298 | - if (\str_starts_with($pattern, '[/')) { |
|
| 299 | - $pattern = '[' . \substr($pattern, 2); |
|
| 298 | + if (\str_starts_with($pattern, '[/')){ |
|
| 299 | + $pattern = '['.\substr($pattern, 2); |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 302 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
| 303 | 303 | $variables = \array_combine($matches[1], $matches[2]); |
| 304 | 304 | |
| 305 | - foreach ($variables as $key => $segment) { |
|
| 305 | + foreach ($variables as $key => $segment){ |
|
| 306 | 306 | $segment = $this->prepareSegment($key, $segment); |
| 307 | 307 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 308 | 308 | $options[] = $key; |
@@ -312,13 +312,13 @@ discard block |
||
| 312 | 312 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 313 | 313 | $options = \array_fill_keys($options, null); |
| 314 | 314 | |
| 315 | - foreach ($this->constrains as $key => $value) { |
|
| 316 | - if ($value instanceof Autofill) { |
|
| 315 | + foreach ($this->constrains as $key => $value){ |
|
| 316 | + if ($value instanceof Autofill){ |
|
| 317 | 317 | // only forces value replacement, not required to be presented as parameter |
| 318 | 318 | continue; |
| 319 | 319 | } |
| 320 | 320 | |
| 321 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 321 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
| 322 | 322 | throw new ConstrainException( |
| 323 | 323 | \sprintf( |
| 324 | 324 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | } |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
| 332 | + $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
| 333 | 333 | $this->template = \stripslashes(\str_replace('?', '', $template)); |
| 334 | 334 | $this->options = $options; |
| 335 | 335 | } |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | private function interpolate(string $string, array $values): string |
| 341 | 341 | { |
| 342 | 342 | $replaces = []; |
| 343 | - foreach ($values as $key => $value) { |
|
| 343 | + foreach ($values as $key => $value){ |
|
| 344 | 344 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 345 | 345 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 346 | 346 | default => '', |
@@ -122,7 +122,8 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function withBasePath(string $basePath): self |
| 124 | 124 | { |
| 125 | - if (!\str_ends_with($basePath, '/')) { |
|
| 125 | + if (!\str_ends_with($basePath, '/')) |
|
| 126 | + { |
|
| 126 | 127 | $basePath .= '/'; |
| 127 | 128 | } |
| 128 | 129 | |
@@ -172,12 +173,14 @@ discard block |
||
| 172 | 173 | */ |
| 173 | 174 | public function match(UriInterface $uri, array $defaults): ?array |
| 174 | 175 | { |
| 175 | - if (!$this->isCompiled()) { |
|
| 176 | + if (!$this->isCompiled()) |
|
| 177 | + { |
|
| 176 | 178 | $this->compile(); |
| 177 | 179 | } |
| 178 | 180 | |
| 179 | 181 | $matches = []; |
| 180 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 182 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
| 183 | + { |
|
| 181 | 184 | return null; |
| 182 | 185 | } |
| 183 | 186 | |
@@ -194,7 +197,8 @@ discard block |
||
| 194 | 197 | */ |
| 195 | 198 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 196 | 199 | { |
| 197 | - if (!$this->isCompiled()) { |
|
| 200 | + if (!$this->isCompiled()) |
|
| 201 | + { |
|
| 198 | 202 | $this->compile(); |
| 199 | 203 | } |
| 200 | 204 | |
@@ -204,8 +208,10 @@ discard block |
||
| 204 | 208 | $this->fetchOptions($parameters, $query) |
| 205 | 209 | ); |
| 206 | 210 | |
| 207 | - foreach ($this->constrains as $key => $_) { |
|
| 208 | - if (empty($parameters[$key])) { |
|
| 211 | + foreach ($this->constrains as $key => $_) |
|
| 212 | + { |
|
| 213 | + if (empty($parameters[$key])) |
|
| 214 | + { |
|
| 209 | 215 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 210 | 216 | } |
| 211 | 217 | } |
@@ -229,18 +235,23 @@ discard block |
||
| 229 | 235 | $allowed = \array_keys($this->options); |
| 230 | 236 | |
| 231 | 237 | $result = []; |
| 232 | - foreach ($parameters as $key => $parameter) { |
|
| 233 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 238 | + foreach ($parameters as $key => $parameter) |
|
| 239 | + { |
|
| 240 | + if (\is_int($key) && isset($allowed[$key])) |
|
| 241 | + { |
|
| 234 | 242 | // this segment fetched keys from given parameters either by name or by position |
| 235 | 243 | $key = $allowed[$key]; |
| 236 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 244 | + } |
|
| 245 | + elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) |
|
| 246 | + { |
|
| 237 | 247 | // all additional parameters given in array form can be glued to query string |
| 238 | 248 | $query[$key] = $parameter; |
| 239 | 249 | continue; |
| 240 | 250 | } |
| 241 | 251 | |
| 242 | 252 | // String must be normalized here |
| 243 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 253 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
| 254 | + { |
|
| 244 | 255 | $result[$key] = ($this->pathSegmentEncoder)($parameter); |
| 245 | 256 | continue; |
| 246 | 257 | } |
@@ -258,15 +269,20 @@ discard block |
||
| 258 | 269 | { |
| 259 | 270 | $path = $uri->getPath(); |
| 260 | 271 | |
| 261 | - if (empty($path) || $path[0] !== '/') { |
|
| 272 | + if (empty($path) || $path[0] !== '/') |
|
| 273 | + { |
|
| 262 | 274 | $path = '/' . $path; |
| 263 | 275 | } |
| 264 | 276 | |
| 265 | - if ($this->matchHost) { |
|
| 277 | + if ($this->matchHost) |
|
| 278 | + { |
|
| 266 | 279 | $uriString = $uri->getHost() . $path; |
| 267 | - } else { |
|
| 280 | + } |
|
| 281 | + else |
|
| 282 | + { |
|
| 268 | 283 | $uriString = \substr($path, \strlen($this->basePath)); |
| 269 | - if ($uriString === false) { |
|
| 284 | + if ($uriString === false) |
|
| 285 | + { |
|
| 270 | 286 | $uriString = ''; |
| 271 | 287 | } |
| 272 | 288 | } |
@@ -282,7 +298,8 @@ discard block |
||
| 282 | 298 | */ |
| 283 | 299 | private function compile(): void |
| 284 | 300 | { |
| 285 | - if ($this->pattern === null) { |
|
| 301 | + if ($this->pattern === null) |
|
| 302 | + { |
|
| 286 | 303 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 287 | 304 | } |
| 288 | 305 | |
@@ -295,14 +312,17 @@ discard block |
||
| 295 | 312 | $pattern = \rtrim(\ltrim($pattern, ':/'), '/'); |
| 296 | 313 | |
| 297 | 314 | // correct [/ first occurrence] |
| 298 | - if (\str_starts_with($pattern, '[/')) { |
|
| 315 | + if (\str_starts_with($pattern, '[/')) |
|
| 316 | + { |
|
| 299 | 317 | $pattern = '[' . \substr($pattern, 2); |
| 300 | 318 | } |
| 301 | 319 | |
| 302 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 320 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
| 321 | + { |
|
| 303 | 322 | $variables = \array_combine($matches[1], $matches[2]); |
| 304 | 323 | |
| 305 | - foreach ($variables as $key => $segment) { |
|
| 324 | + foreach ($variables as $key => $segment) |
|
| 325 | + { |
|
| 306 | 326 | $segment = $this->prepareSegment($key, $segment); |
| 307 | 327 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 308 | 328 | $options[] = $key; |
@@ -312,13 +332,16 @@ discard block |
||
| 312 | 332 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 313 | 333 | $options = \array_fill_keys($options, null); |
| 314 | 334 | |
| 315 | - foreach ($this->constrains as $key => $value) { |
|
| 316 | - if ($value instanceof Autofill) { |
|
| 335 | + foreach ($this->constrains as $key => $value) |
|
| 336 | + { |
|
| 337 | + if ($value instanceof Autofill) |
|
| 338 | + { |
|
| 317 | 339 | // only forces value replacement, not required to be presented as parameter |
| 318 | 340 | continue; |
| 319 | 341 | } |
| 320 | 342 | |
| 321 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 343 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
| 344 | + { |
|
| 322 | 345 | throw new ConstrainException( |
| 323 | 346 | \sprintf( |
| 324 | 347 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -340,7 +363,8 @@ discard block |
||
| 340 | 363 | private function interpolate(string $string, array $values): string |
| 341 | 364 | { |
| 342 | 365 | $replaces = []; |
| 343 | - foreach ($values as $key => $value) { |
|
| 366 | + foreach ($values as $key => $value) |
|
| 367 | + { |
|
| 344 | 368 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 345 | 369 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 346 | 370 | default => '', |