/** * */ public function testGetRegex() { $route = new Route(['GET'], '/', 'FooController::fooAction'); $this->assertSame('#^/?$#s', $route->getRegex()); // $route = new Route(['GET'], '/foo', 'FooController::fooAction'); $this->assertSame('#^/foo$#s', $route->getRegex()); // $route = new Route(['GET'], '/foo/', 'FooController::fooAction'); $this->assertSame('#^/foo/?$#s', $route->getRegex()); // $route = new Route(['GET'], '/foo/bar', 'FooController::fooAction'); $this->assertSame('#^/foo/bar$#s', $route->getRegex()); // $route = new Route(['GET'], '/{id}', 'FooController::fooAction'); $this->assertSame('#^/(?P<id>[^/]++)$#s', $route->getRegex()); // $route = new Route(['GET'], '/foo/{id}', 'FooController::fooAction'); $this->assertSame('#^/foo/(?P<id>[^/]++)$#s', $route->getRegex()); // $route = new Route(['GET'], '/foo/{id}/bar', 'FooController::fooAction'); $this->assertSame('#^/foo/(?P<id>[^/]++)/bar$#s', $route->getRegex()); // $route = new Route(['GET'], '/foo/{id}/', 'FooController::fooAction'); $this->assertSame('#^/foo/(?P<id>[^/]++)/?$#s', $route->getRegex()); // $route = (new Route(['GET'], '/foo/{id}', 'FooController::fooAction'))->when(['id' => '[0-9]+']); $this->assertSame('#^/foo/(?P<id>[0-9]+)$#s', $route->getRegex()); }
/** * Returns TRUE if the route matches the request path and FALSE if not. * * @access protected * @param \mako\http\routing\Route $route Route * @param string $path Request path * @param array $parameters Parameters * @return boolean */ protected function matches(Route $route, $path, array &$parameters = []) { if (preg_match($route->getRegex(), $path, $parameters) > 0) { foreach ($parameters as $key => $value) { if (is_int($key)) { unset($parameters[$key]); } } return true; } return false; }