forked from PHP/Router
26 lines
711 B
PHP
26 lines
711 B
PHP
<?php
|
|
|
|
class StaticRouter
|
|
{
|
|
public static function lookup(array $node, string $method, string $uri): int|array
|
|
{
|
|
$uriSegments = explode('/', trim($uri, '/'));
|
|
$params = [];
|
|
|
|
if (isset($node[$method])) return [$node[$method], $params];
|
|
|
|
if (! $node2 = array_reduce($uriSegments, function ($carry, $segment) use (&$params) {
|
|
if (isset($carry[$segment])) return $carry[$segment];
|
|
if (isset($carry[':x'])) {
|
|
$params[] = $segment;
|
|
return $carry[':x'];
|
|
}
|
|
return null;
|
|
}, $node)) return 404;
|
|
|
|
if (isset($node2[$method])) return [$node2[$method], $params];
|
|
|
|
return 405;
|
|
}
|
|
}
|