/** * @dataProvider getSetProvider * @covers Route::getController * @covers Route::setController * @covers Route::getAction * @covers Route::setAction * @covers Route::getParams * @covers Route::setParams * * @param string $controller * @param string $action * @param array $params */ public function testGetSet($controller, $action, array $params) { $route = new Route('a', 'b', array('c')); $route->setController($controller); $this->assertEquals($controller, $route->getController()); $route->setAction($action); $this->assertEquals($action, $route->getAction()); $route->setParams($params); $this->assertEquals($params, $route->getParams()); }
public function match($method, $route) { $method = strtolower($method); if (!is_array($route)) { $route = trim($route, "/"); $route = explode("/", $route); //Root is defined as / instead of empty space if (count($route) === 1 && $route[0] == "") { $route[0] = "/"; } } else { if (empty($route)) { $route = ["/"]; } } $node = $this->nodes; $params = []; foreach ($route as $fragment) { //Search for fragment as pre-defined piece of the URI $child = $node->getChild(strtolower($fragment)); if ($child === false) { //Search for fragment as a parameter of the URI $child = $node->getChild(":"); if ($child === false) { return false; } $params[] = $fragment; } $node = $child; } $routes = $node->getRoutes(); if (isset($routes[$method])) { $route = new Route(); $route->setProcedure($routes[$method]["target"]); $route->setParams($params); $route->setProperties($routes[$method]["properties"]); return $route; } else { if (count($routes) > 0) { return true; } else { return false; } } }
public static function fetchPlaceholder(Route $route, $path) { $origin = $route->getOrigin(); $params = self::extractPlaceholder($origin); $segments = explode('/', $path); $numeric = $params ? array_slice($segments, (int) min($params)) : []; foreach ($params as $name => $index) { if (isset($segments[$index])) { $params[$name] = $segments[$index]; } else { $params[$name] = null; } } $wildcard = $route->getWildcard(); if (null !== $wildcard) { $slice = substr($path, strlen($route->getPath())); $slice = array_values(array_filter(explode('/', $slice))); $params[$wildcard] = $slice; } $route->setParams($params + $numeric); }