@@ -150,7 +150,7 @@ |
||
| 150 | 150 | $route->match(new ServerRequest('GET', '/api/blog')) |
| 151 | 151 | ); |
| 152 | 152 | |
| 153 | - $this->assertSame('/api/blog', (string) $route->uri()); |
|
| 153 | + $this->assertSame('/api/blog', (string)$route->uri()); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | 156 | public static function routePrefixDataProvider(): iterable |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | private readonly UriFactoryInterface $uriFactory, |
| 54 | 54 | SlugifyInterface $slugify = null, |
| 55 | 55 | ?RoutePatternRegistryInterface $patternRegistry = null, |
| 56 | - ) { |
|
| 56 | + ){ |
|
| 57 | 57 | $this->patternRegistry = $patternRegistry ?? new DefaultPatternRegistry(); |
| 58 | 58 | $this->slugify = $slugify ?? new Slugify(); |
| 59 | 59 | } |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function withBasePath(string $basePath): self |
| 105 | 105 | { |
| 106 | - if (!\str_ends_with($basePath, '/')) { |
|
| 106 | + if (!\str_ends_with($basePath, '/')){ |
|
| 107 | 107 | $basePath .= '/'; |
| 108 | 108 | } |
| 109 | 109 | |
@@ -153,12 +153,12 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function match(UriInterface $uri, array $defaults): ?array |
| 155 | 155 | { |
| 156 | - if (!$this->isCompiled()) { |
|
| 156 | + if (!$this->isCompiled()){ |
|
| 157 | 157 | $this->compile(); |
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | $matches = []; |
| 161 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 161 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
| 162 | 162 | return null; |
| 163 | 163 | } |
| 164 | 164 | |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | */ |
| 176 | 176 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 177 | 177 | { |
| 178 | - if (!$this->isCompiled()) { |
|
| 178 | + if (!$this->isCompiled()){ |
|
| 179 | 179 | $this->compile(); |
| 180 | 180 | } |
| 181 | 181 | |
@@ -185,8 +185,8 @@ discard block |
||
| 185 | 185 | $this->fetchOptions($parameters, $query) |
| 186 | 186 | ); |
| 187 | 187 | |
| 188 | - foreach ($this->constrains as $key => $_) { |
|
| 189 | - if (empty($parameters[$key])) { |
|
| 188 | + foreach ($this->constrains as $key => $_){ |
|
| 189 | + if (empty($parameters[$key])){ |
|
| 190 | 190 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 191 | 191 | } |
| 192 | 192 | } |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | $path = $this->interpolate($this->template, $parameters); |
| 196 | 196 | |
| 197 | 197 | //Uri with added base path and prefix |
| 198 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/')); |
|
| 198 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/')); |
|
| 199 | 199 | |
| 200 | 200 | return empty($query) ? $uri : $uri->withQuery(\http_build_query($query)); |
| 201 | 201 | } |
@@ -210,18 +210,18 @@ discard block |
||
| 210 | 210 | $allowed = \array_keys($this->options); |
| 211 | 211 | |
| 212 | 212 | $result = []; |
| 213 | - foreach ($parameters as $key => $parameter) { |
|
| 214 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 213 | + foreach ($parameters as $key => $parameter){ |
|
| 214 | + if (\is_int($key) && isset($allowed[$key])){ |
|
| 215 | 215 | // this segment fetched keys from given parameters either by name or by position |
| 216 | 216 | $key = $allowed[$key]; |
| 217 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 217 | + } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){ |
|
| 218 | 218 | // all additional parameters given in array form can be glued to query string |
| 219 | 219 | $query[$key] = $parameter; |
| 220 | 220 | continue; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | //String must be normalized here |
| 224 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 224 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
| 225 | 225 | $result[$key] = $this->slugify->slugify($parameter); |
| 226 | 226 | continue; |
| 227 | 227 | } |
@@ -239,15 +239,15 @@ discard block |
||
| 239 | 239 | { |
| 240 | 240 | $path = $uri->getPath(); |
| 241 | 241 | |
| 242 | - if (empty($path) || $path[0] !== '/') { |
|
| 243 | - $path = '/' . $path; |
|
| 242 | + if (empty($path) || $path[0] !== '/'){ |
|
| 243 | + $path = '/'.$path; |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - if ($this->matchHost) { |
|
| 247 | - $uriString = $uri->getHost() . $path; |
|
| 248 | - } else { |
|
| 246 | + if ($this->matchHost){ |
|
| 247 | + $uriString = $uri->getHost().$path; |
|
| 248 | + }else{ |
|
| 249 | 249 | $uriString = \substr($path, \strlen($this->basePath)); |
| 250 | - if ($uriString === false) { |
|
| 250 | + if ($uriString === false){ |
|
| 251 | 251 | $uriString = ''; |
| 252 | 252 | } |
| 253 | 253 | } |
@@ -263,7 +263,7 @@ discard block |
||
| 263 | 263 | */ |
| 264 | 264 | private function compile(): void |
| 265 | 265 | { |
| 266 | - if ($this->pattern === null) { |
|
| 266 | + if ($this->pattern === null){ |
|
| 267 | 267 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 268 | 268 | } |
| 269 | 269 | |
@@ -272,18 +272,18 @@ discard block |
||
| 272 | 272 | |
| 273 | 273 | $prefix = \rtrim($this->getPrefix(), '/ '); |
| 274 | 274 | $pattern = \ltrim($this->pattern, '/ '); |
| 275 | - $pattern = $prefix . '/' . $pattern; |
|
| 275 | + $pattern = $prefix.'/'.$pattern; |
|
| 276 | 276 | $pattern = \rtrim(\ltrim($pattern, ':/'), '/'); |
| 277 | 277 | |
| 278 | 278 | // correct [/ first occurrence] |
| 279 | - if (\str_starts_with($pattern, '[/')) { |
|
| 280 | - $pattern = '[' . \substr($pattern, 2); |
|
| 279 | + if (\str_starts_with($pattern, '[/')){ |
|
| 280 | + $pattern = '['.\substr($pattern, 2); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 283 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
| 284 | 284 | $variables = \array_combine($matches[1], $matches[2]); |
| 285 | 285 | |
| 286 | - foreach ($variables as $key => $segment) { |
|
| 286 | + foreach ($variables as $key => $segment){ |
|
| 287 | 287 | $segment = $this->prepareSegment($key, $segment); |
| 288 | 288 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 289 | 289 | $options[] = $key; |
@@ -293,13 +293,13 @@ discard block |
||
| 293 | 293 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 294 | 294 | $options = \array_fill_keys($options, null); |
| 295 | 295 | |
| 296 | - foreach ($this->constrains as $key => $value) { |
|
| 297 | - if ($value instanceof Autofill) { |
|
| 296 | + foreach ($this->constrains as $key => $value){ |
|
| 297 | + if ($value instanceof Autofill){ |
|
| 298 | 298 | // only forces value replacement, not required to be presented as parameter |
| 299 | 299 | continue; |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 302 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
| 303 | 303 | throw new ConstrainException( |
| 304 | 304 | \sprintf( |
| 305 | 305 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -310,7 +310,7 @@ discard block |
||
| 310 | 310 | } |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | - $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
| 313 | + $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
| 314 | 314 | $this->template = \stripslashes(\str_replace('?', '', $template)); |
| 315 | 315 | $this->options = $options; |
| 316 | 316 | } |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | private function interpolate(string $string, array $values): string |
| 322 | 322 | { |
| 323 | 323 | $replaces = []; |
| 324 | - foreach ($values as $key => $value) { |
|
| 324 | + foreach ($values as $key => $value){ |
|
| 325 | 325 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 326 | 326 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 327 | 327 | default => '', |
@@ -103,7 +103,8 @@ discard block |
||
| 103 | 103 | */ |
| 104 | 104 | public function withBasePath(string $basePath): self |
| 105 | 105 | { |
| 106 | - if (!\str_ends_with($basePath, '/')) { |
|
| 106 | + if (!\str_ends_with($basePath, '/')) |
|
| 107 | + { |
|
| 107 | 108 | $basePath .= '/'; |
| 108 | 109 | } |
| 109 | 110 | |
@@ -153,12 +154,14 @@ discard block |
||
| 153 | 154 | */ |
| 154 | 155 | public function match(UriInterface $uri, array $defaults): ?array |
| 155 | 156 | { |
| 156 | - if (!$this->isCompiled()) { |
|
| 157 | + if (!$this->isCompiled()) |
|
| 158 | + { |
|
| 157 | 159 | $this->compile(); |
| 158 | 160 | } |
| 159 | 161 | |
| 160 | 162 | $matches = []; |
| 161 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 163 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
| 164 | + { |
|
| 162 | 165 | return null; |
| 163 | 166 | } |
| 164 | 167 | |
@@ -175,7 +178,8 @@ discard block |
||
| 175 | 178 | */ |
| 176 | 179 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 177 | 180 | { |
| 178 | - if (!$this->isCompiled()) { |
|
| 181 | + if (!$this->isCompiled()) |
|
| 182 | + { |
|
| 179 | 183 | $this->compile(); |
| 180 | 184 | } |
| 181 | 185 | |
@@ -185,8 +189,10 @@ discard block |
||
| 185 | 189 | $this->fetchOptions($parameters, $query) |
| 186 | 190 | ); |
| 187 | 191 | |
| 188 | - foreach ($this->constrains as $key => $_) { |
|
| 189 | - if (empty($parameters[$key])) { |
|
| 192 | + foreach ($this->constrains as $key => $_) |
|
| 193 | + { |
|
| 194 | + if (empty($parameters[$key])) |
|
| 195 | + { |
|
| 190 | 196 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 191 | 197 | } |
| 192 | 198 | } |
@@ -210,18 +216,23 @@ discard block |
||
| 210 | 216 | $allowed = \array_keys($this->options); |
| 211 | 217 | |
| 212 | 218 | $result = []; |
| 213 | - foreach ($parameters as $key => $parameter) { |
|
| 214 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 219 | + foreach ($parameters as $key => $parameter) |
|
| 220 | + { |
|
| 221 | + if (\is_int($key) && isset($allowed[$key])) |
|
| 222 | + { |
|
| 215 | 223 | // this segment fetched keys from given parameters either by name or by position |
| 216 | 224 | $key = $allowed[$key]; |
| 217 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 225 | + } |
|
| 226 | + elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) |
|
| 227 | + { |
|
| 218 | 228 | // all additional parameters given in array form can be glued to query string |
| 219 | 229 | $query[$key] = $parameter; |
| 220 | 230 | continue; |
| 221 | 231 | } |
| 222 | 232 | |
| 223 | 233 | //String must be normalized here |
| 224 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 234 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
| 235 | + { |
|
| 225 | 236 | $result[$key] = $this->slugify->slugify($parameter); |
| 226 | 237 | continue; |
| 227 | 238 | } |
@@ -239,15 +250,20 @@ discard block |
||
| 239 | 250 | { |
| 240 | 251 | $path = $uri->getPath(); |
| 241 | 252 | |
| 242 | - if (empty($path) || $path[0] !== '/') { |
|
| 253 | + if (empty($path) || $path[0] !== '/') |
|
| 254 | + { |
|
| 243 | 255 | $path = '/' . $path; |
| 244 | 256 | } |
| 245 | 257 | |
| 246 | - if ($this->matchHost) { |
|
| 258 | + if ($this->matchHost) |
|
| 259 | + { |
|
| 247 | 260 | $uriString = $uri->getHost() . $path; |
| 248 | - } else { |
|
| 261 | + } |
|
| 262 | + else |
|
| 263 | + { |
|
| 249 | 264 | $uriString = \substr($path, \strlen($this->basePath)); |
| 250 | - if ($uriString === false) { |
|
| 265 | + if ($uriString === false) |
|
| 266 | + { |
|
| 251 | 267 | $uriString = ''; |
| 252 | 268 | } |
| 253 | 269 | } |
@@ -263,7 +279,8 @@ discard block |
||
| 263 | 279 | */ |
| 264 | 280 | private function compile(): void |
| 265 | 281 | { |
| 266 | - if ($this->pattern === null) { |
|
| 282 | + if ($this->pattern === null) |
|
| 283 | + { |
|
| 267 | 284 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 268 | 285 | } |
| 269 | 286 | |
@@ -276,14 +293,17 @@ discard block |
||
| 276 | 293 | $pattern = \rtrim(\ltrim($pattern, ':/'), '/'); |
| 277 | 294 | |
| 278 | 295 | // correct [/ first occurrence] |
| 279 | - if (\str_starts_with($pattern, '[/')) { |
|
| 296 | + if (\str_starts_with($pattern, '[/')) |
|
| 297 | + { |
|
| 280 | 298 | $pattern = '[' . \substr($pattern, 2); |
| 281 | 299 | } |
| 282 | 300 | |
| 283 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 301 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
| 302 | + { |
|
| 284 | 303 | $variables = \array_combine($matches[1], $matches[2]); |
| 285 | 304 | |
| 286 | - foreach ($variables as $key => $segment) { |
|
| 305 | + foreach ($variables as $key => $segment) |
|
| 306 | + { |
|
| 287 | 307 | $segment = $this->prepareSegment($key, $segment); |
| 288 | 308 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 289 | 309 | $options[] = $key; |
@@ -293,13 +313,16 @@ discard block |
||
| 293 | 313 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 294 | 314 | $options = \array_fill_keys($options, null); |
| 295 | 315 | |
| 296 | - foreach ($this->constrains as $key => $value) { |
|
| 297 | - if ($value instanceof Autofill) { |
|
| 316 | + foreach ($this->constrains as $key => $value) |
|
| 317 | + { |
|
| 318 | + if ($value instanceof Autofill) |
|
| 319 | + { |
|
| 298 | 320 | // only forces value replacement, not required to be presented as parameter |
| 299 | 321 | continue; |
| 300 | 322 | } |
| 301 | 323 | |
| 302 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 324 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
| 325 | + { |
|
| 303 | 326 | throw new ConstrainException( |
| 304 | 327 | \sprintf( |
| 305 | 328 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -321,7 +344,8 @@ discard block |
||
| 321 | 344 | private function interpolate(string $string, array $values): string |
| 322 | 345 | { |
| 323 | 346 | $replaces = []; |
| 324 | - foreach ($values as $key => $value) { |
|
| 347 | + foreach ($values as $key => $value) |
|
| 348 | + { |
|
| 325 | 349 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 326 | 350 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 327 | 351 | default => '', |