Abstracting lookup method

This commit is contained in:
Valithor Obsidion 2025-02-05 12:24:26 -05:00
parent 9fdef6b250
commit b82a662c02

View File

@ -64,7 +64,7 @@ trait SegmentRouterTrait
$node = $routes; $node = $routes;
// if the URI is the root, and the method is defined, return the handler // if the URI is the root, and the method is defined, return the handler
if ($uri === '/' && isset($node[$method]) && array_key_exists($method, $node)) { if (self::isRootUri($uri, $node, $method)) {
return ['code' => 200, 'handler' => $node[$method], 'params' => null]; return ['code' => 200, 'handler' => $node[$method], 'params' => null];
} }
@ -81,14 +81,21 @@ trait SegmentRouterTrait
} }
} }
return self::getHandler($node, $method, $params) ?? ['code' => 405, 'handler' => null, 'params' => []];
}
public static function isRootUri(string $uri, array &$node, string $method): bool
{
return ($uri === '/' && isset($node[$method]) && array_key_exists($method, $node));
}
// if we found a handler for the method, return it and any params. if not, return a 405 // if we found a handler for the method, return it and any params. if not, return a 405
if (array_key_exists($method, $node)) { public static function getHandler($node, $method, $params): ?array
return ['code' => 200, 'handler' => $node[$method], 'params' => $params]; {
return array_key_exists($method, $node)
? ['code' => 200, 'handler' => $node[$method], 'params' => $params]
: null;
} }
return ['code' => 405, 'handler' => null, 'params' => []];
}
/** /**
* Clear all routes from the router. * Clear all routes from the router.
*/ */