/** * testGetVariables * * @return void * * @covers Windwalker\Router\RouteHelper::getVariables */ public function testGetVariables() { $array = array(0 => 5, 'id' => 5, 1 => 'foo', 'bar' => 'foo'); $this->assertEquals(array('id' => 5, 'bar' => 'foo'), RouteHelper::getVariables($array)); $vars = array('flower' => 'sakura'); $this->assertEquals(array('flower' => 'sakura', 'id' => 5, 'bar' => 'foo'), RouteHelper::getVariables($array, $vars)); }
/** * Method to test compile(). * * @param string $pattern * @param string $expected * @param int $line * * @return void * * @covers Windwalker\Router\Compiler\BasicCompiler::compile * * @dataProvider regexList */ public function testCompile($pattern, $expected, $route, $expectedMatches, $line) { $regex = TrieCompiler::compile($pattern, array('id' => '\\d+')); $this->assertEquals(chr(1) . '^' . $expected . '$' . chr(1), $regex, 'Fail at: ' . $line); preg_match($regex, $route, $matches); $vars = RouteHelper::getVariables($matches); $this->assertNotEmpty($matches); $this->assertEquals($expectedMatches, $vars); }
/** * Match routes. * * @param string $route * @param Route $routeItem * * @return Route|false */ public function matchRoute($route, Route $routeItem) { $regex = $routeItem->getRegex(); if (!$regex || $this->debug) { $regex = BasicCompiler::compile($routeItem->getPattern(), $routeItem->getRequirements()); $routeItem->setRegex($regex); } $route = RouteHelper::normalise($route); if (preg_match($regex, $route, $matches)) { $variables = RouteHelper::getVariables($matches); $variables['_rawRoute'] = $route; } else { return false; } $routeItem->setVariables(array_merge($routeItem->getVariables(), $variables)); return $routeItem; }
/** * matchSegment * * @param array $segments * @param array $node * @param int $level * * @return bool|Route */ protected function matchSegment($segments, $node, $level = 1) { $segment = isset($segments[$level - 1]) ? $segments[$level - 1] : false; if ($segment === false) { return false; } $segment = $segment ?: '/'; foreach ($node as $regex => $child) { $this->count++; // Start with a '(' is a regex if ($regex[0] == '(') { preg_match(chr(1) . $regex . chr(1), $segment, $match); if (!$match) { continue; } RouteHelper::getVariables($match, $this->vars); } else { if ($regex != $segment) { continue; } } $result = false; // Has child, iterate it. if ($child && is_array($child)) { $child = $this->matchSegment($segments, $child, $level + 1); } // If is string, means we get a route index, using this index to find Route from maps. if (is_string($child)) { $child = $this->routes[$this->routeMaps[$child]]; } // Match this Route if ($child instanceof Route) { $result = $this->matchOptions($child, $this->method, $this->options); } // If match fail, continue find next element. if (!$result) { continue; } return $child; } return false; }