/** * @param Request $request * @param Route[] $routes * @return $this */ public function route($request, $routes) { # TODO Fix namespace $patternMatcher = new PatternMatcher(); foreach ($routes as $route) { if ($patternMatcher->matchAgainst($route->getPattern(), $request->getURI())) { return $route; } } # TODO Implement not found case }
/** * Processes a URI and extracts the information * * @param string $uri * @return array */ public function handle($uri) { $variableList = PatternMatcher::extract($uri, $this->pattern); // Now check for some given params in the routes scheme file // Overwrites any values obtained from URL if ($this->parameters) { foreach ($this->parameters as $parameterName => $parameterValue) { $variableList[$parameterName] = $parameterValue; } } // Now check if there are requirements if ($this->requirements) { foreach ($this->requirements as $variableField => $requirement) { foreach ($requirement as $req_type => $req_value) { switch ($req_type) { case 'type': if (gettype($variableList[$variableField]) != $req_value) { return false; } break; default: trigger_error('Requirement type ' . $requirement . ' does not exist'); break; } } } } $variableList['target'] = $this->target; $variableList['route'] = $this; return $variableList; }
public function pattern_match_test() { $matcher = new PatternMatcher('/fo+o.*/'); $this->assertTrue($matcher->matches('foooo')); $this->assertTrue($matcher->matches('fooooooooo')); $this->assertTrue($matcher->matches('adsfafdsfooooooooo')); $this->assertTrue($matcher->matches('asdfaf fooo dsfasfd')); $this->assertFalse($matcher->matches('fobo')); $this->assertFalse($matcher->matches('fo')); }
/** * @param string $pattern */ public function __construct($pattern) { $this->globPattern = $pattern; $parts = array_map('preg_quote', preg_split('/\\*/', $pattern)); parent::__construct('#^' . implode('[^\\/]+', $parts) . '$#i'); }
/** * @dataProvider dataRoutes */ public function testRoutePatternExtraction($string, $pattern, $expOutput) { self::assertEquals($expOutput, PatternMatcher::extract($string, $pattern)); }