@@ -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 => '', |