diff --git a/src/SegmentRouterTrait.php b/src/SegmentRouterTrait.php index 40162a8..a851f22 100644 --- a/src/SegmentRouterTrait.php +++ b/src/SegmentRouterTrait.php @@ -63,8 +63,8 @@ trait SegmentRouterTrait // node is a reference to our current location in the node tree $node = $routes; - // 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 the URI is the root, and the method is defined, return the handler + if (self::isRootUri($uri, $node, $method)) { return ['code' => 200, 'handler' => $node[$method], 'params' => null]; } @@ -81,14 +81,21 @@ trait SegmentRouterTrait } } - // if we found a handler for the method, return it and any params. if not, return a 405 - if (array_key_exists($method, $node)) { - return ['code' => 200, 'handler' => $node[$method], 'params' => $params]; - } - - return ['code' => 405, 'handler' => null, 'params' => []]; + 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 + public static function getHandler($node, $method, $params): ?array + { + return array_key_exists($method, $node) + ? ['code' => 200, 'handler' => $node[$method], 'params' => $params] + : null; + } /** * Clear all routes from the router. */