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