@@ -95,7 +95,8 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | public function withBasePath(string $basePath): self |
| 97 | 97 | { |
| 98 | - if (!\str_ends_with($basePath, '/')) { |
|
| 98 | + if (!\str_ends_with($basePath, '/')) |
|
| 99 | + { |
|
| 99 | 100 | $basePath .= '/'; |
| 100 | 101 | } |
| 101 | 102 | |
@@ -132,12 +133,14 @@ discard block |
||
| 132 | 133 | */ |
| 133 | 134 | public function match(UriInterface $uri, array $defaults): ?array |
| 134 | 135 | { |
| 135 | - if (!$this->isCompiled()) { |
|
| 136 | + if (!$this->isCompiled()) |
|
| 137 | + { |
|
| 136 | 138 | $this->compile(); |
| 137 | 139 | } |
| 138 | 140 | |
| 139 | 141 | $matches = []; |
| 140 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 142 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) |
|
| 143 | + { |
|
| 141 | 144 | return null; |
| 142 | 145 | } |
| 143 | 146 | |
@@ -154,7 +157,8 @@ discard block |
||
| 154 | 157 | */ |
| 155 | 158 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 156 | 159 | { |
| 157 | - if (!$this->isCompiled()) { |
|
| 160 | + if (!$this->isCompiled()) |
|
| 161 | + { |
|
| 158 | 162 | $this->compile(); |
| 159 | 163 | } |
| 160 | 164 | |
@@ -164,8 +168,10 @@ discard block |
||
| 164 | 168 | $this->fetchOptions($parameters, $query) |
| 165 | 169 | ); |
| 166 | 170 | |
| 167 | - foreach ($this->constrains as $key => $_) { |
|
| 168 | - if (empty($parameters[$key])) { |
|
| 171 | + foreach ($this->constrains as $key => $_) |
|
| 172 | + { |
|
| 173 | + if (empty($parameters[$key])) |
|
| 174 | + { |
|
| 169 | 175 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 170 | 176 | } |
| 171 | 177 | } |
@@ -189,18 +195,23 @@ discard block |
||
| 189 | 195 | $allowed = \array_keys($this->options); |
| 190 | 196 | |
| 191 | 197 | $result = []; |
| 192 | - foreach ($parameters as $key => $parameter) { |
|
| 193 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 198 | + foreach ($parameters as $key => $parameter) |
|
| 199 | + { |
|
| 200 | + if (\is_int($key) && isset($allowed[$key])) |
|
| 201 | + { |
|
| 194 | 202 | // this segment fetched keys from given parameters either by name or by position |
| 195 | 203 | $key = $allowed[$key]; |
| 196 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 204 | + } |
|
| 205 | + elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) |
|
| 206 | + { |
|
| 197 | 207 | // all additional parameters given in array form can be glued to query string |
| 198 | 208 | $query[$key] = $parameter; |
| 199 | 209 | continue; |
| 200 | 210 | } |
| 201 | 211 | |
| 202 | 212 | //String must be normalized here |
| 203 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 213 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) |
|
| 214 | + { |
|
| 204 | 215 | $result[$key] = $this->slugify->slugify($parameter); |
| 205 | 216 | continue; |
| 206 | 217 | } |
@@ -218,15 +229,20 @@ discard block |
||
| 218 | 229 | { |
| 219 | 230 | $path = $uri->getPath(); |
| 220 | 231 | |
| 221 | - if (empty($path) || $path[0] !== '/') { |
|
| 232 | + if (empty($path) || $path[0] !== '/') |
|
| 233 | + { |
|
| 222 | 234 | $path = '/' . $path; |
| 223 | 235 | } |
| 224 | 236 | |
| 225 | - if ($this->matchHost) { |
|
| 237 | + if ($this->matchHost) |
|
| 238 | + { |
|
| 226 | 239 | $uriString = $uri->getHost() . $path; |
| 227 | - } else { |
|
| 240 | + } |
|
| 241 | + else |
|
| 242 | + { |
|
| 228 | 243 | $uriString = \substr($path, \strlen($this->basePath)); |
| 229 | - if ($uriString === false) { |
|
| 244 | + if ($uriString === false) |
|
| 245 | + { |
|
| 230 | 246 | $uriString = ''; |
| 231 | 247 | } |
| 232 | 248 | } |
@@ -239,7 +255,8 @@ discard block |
||
| 239 | 255 | */ |
| 240 | 256 | private function compile(): void |
| 241 | 257 | { |
| 242 | - if ($this->pattern === null) { |
|
| 258 | + if ($this->pattern === null) |
|
| 259 | + { |
|
| 243 | 260 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 244 | 261 | } |
| 245 | 262 | |
@@ -248,14 +265,17 @@ discard block |
||
| 248 | 265 | $pattern = \rtrim(\ltrim($this->getPrefix() . '/' . $this->pattern, ':/'), '/'); |
| 249 | 266 | |
| 250 | 267 | // correct [/ first occurrence] |
| 251 | - if (\str_starts_with($pattern, '[/')) { |
|
| 268 | + if (\str_starts_with($pattern, '[/')) |
|
| 269 | + { |
|
| 252 | 270 | $pattern = '[' . \substr($pattern, 2); |
| 253 | 271 | } |
| 254 | 272 | |
| 255 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 273 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) |
|
| 274 | + { |
|
| 256 | 275 | $variables = \array_combine($matches[1], $matches[2]); |
| 257 | 276 | |
| 258 | - foreach ($variables as $key => $segment) { |
|
| 277 | + foreach ($variables as $key => $segment) |
|
| 278 | + { |
|
| 259 | 279 | $segment = $this->prepareSegment($key, $segment); |
| 260 | 280 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 261 | 281 | $options[] = $key; |
@@ -265,13 +285,16 @@ discard block |
||
| 265 | 285 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 266 | 286 | $options = \array_fill_keys($options, null); |
| 267 | 287 | |
| 268 | - foreach ($this->constrains as $key => $value) { |
|
| 269 | - if ($value instanceof Autofill) { |
|
| 288 | + foreach ($this->constrains as $key => $value) |
|
| 289 | + { |
|
| 290 | + if ($value instanceof Autofill) |
|
| 291 | + { |
|
| 270 | 292 | // only forces value replacement, not required to be presented as parameter |
| 271 | 293 | continue; |
| 272 | 294 | } |
| 273 | 295 | |
| 274 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 296 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) |
|
| 297 | + { |
|
| 275 | 298 | throw new ConstrainException( |
| 276 | 299 | \sprintf( |
| 277 | 300 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -293,7 +316,8 @@ discard block |
||
| 293 | 316 | private function interpolate(string $string, array $values): string |
| 294 | 317 | { |
| 295 | 318 | $replaces = []; |
| 296 | - foreach ($values as $key => $value) { |
|
| 319 | + foreach ($values as $key => $value) |
|
| 320 | + { |
|
| 297 | 321 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 298 | 322 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 299 | 323 | default => '', |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | private readonly UriFactoryInterface $uriFactory, |
| 49 | 49 | SlugifyInterface $slugify = null, |
| 50 | 50 | private readonly ?RoutePatternRegistryInterface $patternRegistry = new DefaultPatternRegistry(), |
| 51 | - ) { |
|
| 51 | + ){ |
|
| 52 | 52 | $this->slugify = $slugify ?? new Slugify(); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | public function withBasePath(string $basePath): self |
| 90 | 90 | { |
| 91 | - if (!\str_ends_with($basePath, '/')) { |
|
| 91 | + if (!\str_ends_with($basePath, '/')){ |
|
| 92 | 92 | $basePath .= '/'; |
| 93 | 93 | } |
| 94 | 94 | |
@@ -125,12 +125,12 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | public function match(UriInterface $uri, array $defaults): ?array |
| 127 | 127 | { |
| 128 | - if (!$this->isCompiled()) { |
|
| 128 | + if (!$this->isCompiled()){ |
|
| 129 | 129 | $this->compile(); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | $matches = []; |
| 133 | - if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)) { |
|
| 133 | + if (!\preg_match($this->compiled, $this->fetchTarget($uri), $matches)){ |
|
| 134 | 134 | return null; |
| 135 | 135 | } |
| 136 | 136 | |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | */ |
| 148 | 148 | public function uri(iterable $parameters = [], array $defaults = []): UriInterface |
| 149 | 149 | { |
| 150 | - if (!$this->isCompiled()) { |
|
| 150 | + if (!$this->isCompiled()){ |
|
| 151 | 151 | $this->compile(); |
| 152 | 152 | } |
| 153 | 153 | |
@@ -157,8 +157,8 @@ discard block |
||
| 157 | 157 | $this->fetchOptions($parameters, $query) |
| 158 | 158 | ); |
| 159 | 159 | |
| 160 | - foreach ($this->constrains as $key => $_) { |
|
| 161 | - if (empty($parameters[$key])) { |
|
| 160 | + foreach ($this->constrains as $key => $_){ |
|
| 161 | + if (empty($parameters[$key])){ |
|
| 162 | 162 | throw new UriHandlerException(\sprintf('Unable to generate Uri, parameter `%s` is missing', $key)); |
| 163 | 163 | } |
| 164 | 164 | } |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | $path = $this->interpolate($this->template, $parameters); |
| 168 | 168 | |
| 169 | 169 | //Uri with added base path and prefix |
| 170 | - $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath) . \trim($path, '/')); |
|
| 170 | + $uri = $this->uriFactory->createUri(($this->matchHost ? '' : $this->basePath).\trim($path, '/')); |
|
| 171 | 171 | |
| 172 | 172 | return empty($query) ? $uri : $uri->withQuery(\http_build_query($query)); |
| 173 | 173 | } |
@@ -182,18 +182,18 @@ discard block |
||
| 182 | 182 | $allowed = \array_keys($this->options); |
| 183 | 183 | |
| 184 | 184 | $result = []; |
| 185 | - foreach ($parameters as $key => $parameter) { |
|
| 186 | - if (\is_int($key) && isset($allowed[$key])) { |
|
| 185 | + foreach ($parameters as $key => $parameter){ |
|
| 186 | + if (\is_int($key) && isset($allowed[$key])){ |
|
| 187 | 187 | // this segment fetched keys from given parameters either by name or by position |
| 188 | 188 | $key = $allowed[$key]; |
| 189 | - } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)) { |
|
| 189 | + } elseif (!\array_key_exists($key, $this->options) && \is_array($parameters)){ |
|
| 190 | 190 | // all additional parameters given in array form can be glued to query string |
| 191 | 191 | $query[$key] = $parameter; |
| 192 | 192 | continue; |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | //String must be normalized here |
| 196 | - if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)) { |
|
| 196 | + if (\is_string($parameter) && !\preg_match('/^[a-z\-_0-9]+$/i', $parameter)){ |
|
| 197 | 197 | $result[$key] = $this->slugify->slugify($parameter); |
| 198 | 198 | continue; |
| 199 | 199 | } |
@@ -211,15 +211,15 @@ discard block |
||
| 211 | 211 | { |
| 212 | 212 | $path = $uri->getPath(); |
| 213 | 213 | |
| 214 | - if (empty($path) || $path[0] !== '/') { |
|
| 215 | - $path = '/' . $path; |
|
| 214 | + if (empty($path) || $path[0] !== '/'){ |
|
| 215 | + $path = '/'.$path; |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - if ($this->matchHost) { |
|
| 219 | - $uriString = $uri->getHost() . $path; |
|
| 220 | - } else { |
|
| 218 | + if ($this->matchHost){ |
|
| 219 | + $uriString = $uri->getHost().$path; |
|
| 220 | + }else{ |
|
| 221 | 221 | $uriString = \substr($path, \strlen($this->basePath)); |
| 222 | - if ($uriString === false) { |
|
| 222 | + if ($uriString === false){ |
|
| 223 | 223 | $uriString = ''; |
| 224 | 224 | } |
| 225 | 225 | } |
@@ -232,23 +232,23 @@ discard block |
||
| 232 | 232 | */ |
| 233 | 233 | private function compile(): void |
| 234 | 234 | { |
| 235 | - if ($this->pattern === null) { |
|
| 235 | + if ($this->pattern === null){ |
|
| 236 | 236 | throw new UriHandlerException('Unable to compile UriHandler, pattern is not set'); |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | $options = []; |
| 240 | 240 | $replaces = []; |
| 241 | - $pattern = \rtrim(\ltrim($this->getPrefix() . '/' . $this->pattern, ':/'), '/'); |
|
| 241 | + $pattern = \rtrim(\ltrim($this->getPrefix().'/'.$this->pattern, ':/'), '/'); |
|
| 242 | 242 | |
| 243 | 243 | // correct [/ first occurrence] |
| 244 | - if (\str_starts_with($pattern, '[/')) { |
|
| 245 | - $pattern = '[' . \substr($pattern, 2); |
|
| 244 | + if (\str_starts_with($pattern, '[/')){ |
|
| 245 | + $pattern = '['.\substr($pattern, 2); |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | - if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)) { |
|
| 248 | + if (\preg_match_all('/<(\w+):?(.*?)?>/', $pattern, $matches)){ |
|
| 249 | 249 | $variables = \array_combine($matches[1], $matches[2]); |
| 250 | 250 | |
| 251 | - foreach ($variables as $key => $segment) { |
|
| 251 | + foreach ($variables as $key => $segment){ |
|
| 252 | 252 | $segment = $this->prepareSegment($key, $segment); |
| 253 | 253 | $replaces[\sprintf('<%s>', $key)] = \sprintf('(?P<%s>%s)', $key, $segment); |
| 254 | 254 | $options[] = $key; |
@@ -258,13 +258,13 @@ discard block |
||
| 258 | 258 | $template = \preg_replace('/<(\w+):?.*?>/', '<\1>', $pattern); |
| 259 | 259 | $options = \array_fill_keys($options, null); |
| 260 | 260 | |
| 261 | - foreach ($this->constrains as $key => $value) { |
|
| 262 | - if ($value instanceof Autofill) { |
|
| 261 | + foreach ($this->constrains as $key => $value){ |
|
| 262 | + if ($value instanceof Autofill){ |
|
| 263 | 263 | // only forces value replacement, not required to be presented as parameter |
| 264 | 264 | continue; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | - if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])) { |
|
| 267 | + if (!\array_key_exists($key, $options) && !isset($this->defaults[$key])){ |
|
| 268 | 268 | throw new ConstrainException( |
| 269 | 269 | \sprintf( |
| 270 | 270 | 'Route `%s` does not define routing parameter `<%s>`.', |
@@ -275,7 +275,7 @@ discard block |
||
| 275 | 275 | } |
| 276 | 276 | } |
| 277 | 277 | |
| 278 | - $this->compiled = '/^' . \strtr($template, $replaces + self::PATTERN_REPLACES) . '$/iu'; |
|
| 278 | + $this->compiled = '/^'.\strtr($template, $replaces + self::PATTERN_REPLACES).'$/iu'; |
|
| 279 | 279 | $this->template = \stripslashes(\str_replace('?', '', $template)); |
| 280 | 280 | $this->options = $options; |
| 281 | 281 | } |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | private function interpolate(string $string, array $values): string |
| 287 | 287 | { |
| 288 | 288 | $replaces = []; |
| 289 | - foreach ($values as $key => $value) { |
|
| 289 | + foreach ($values as $key => $value){ |
|
| 290 | 290 | $replaces[\sprintf('<%s>', $key)] = match (true) { |
| 291 | 291 | $value instanceof \Stringable || \is_scalar($value) => (string)$value, |
| 292 | 292 | default => '', |
@@ -6,7 +6,7 @@ |
||
| 6 | 6 | { |
| 7 | 7 | public function __construct( |
| 8 | 8 | private readonly array $values |
| 9 | - ) { |
|
| 9 | + ){ |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | public function __toString() |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | * @param non-empty-string $name |
| 9 | 9 | * @param non-empty-string|\Stringable $pattern |
| 10 | 10 | */ |
| 11 | - public function register(string $name, string|\Stringable $pattern): void; |
|
| 11 | + public function register(string $name, string | \Stringable $pattern): void; |
|
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * @return array<non-empty-string, non-empty-string> |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * @param non-empty-string $name |
| 22 | 22 | * @param non-empty-string|\Stringable $pattern |
| 23 | 23 | */ |
| 24 | - public function register(string $name, string|\Stringable $pattern): void |
|
| 24 | + public function register(string $name, string | \Stringable $pattern): void |
|
| 25 | 25 | { |
| 26 | 26 | $this->patterns[$name] = (string)$pattern; |
| 27 | 27 | } |