Commit 7fc25bec by pupi1985

Use strtr function to map placeholders to regular expressions

parent 17f1cfcf
...@@ -24,10 +24,7 @@ class Router ...@@ -24,10 +24,7 @@ class Router
protected $routes; protected $routes;
/** @var array */ /** @var array */
private $paramsConverterKeys; private $paramsConverter;
/** @var array */
private $paramsConverterValues;
/** @var string */ /** @var string */
private $httpMethod; private $httpMethod;
...@@ -36,12 +33,10 @@ class Router ...@@ -36,12 +33,10 @@ class Router
{ {
$this->routes = $routeData; $this->routes = $routeData;
$paramsConverter = array( $this->paramsConverter = array(
'{str}' => '([^/]+)', '{str}' => '([^/]+)',
'{int}' => '([0-9]+)', '{int}' => '([0-9]+)',
); );
$this->paramsConverterKeys = array_keys($paramsConverter);
$this->paramsConverterValues = array_values($paramsConverter);
$this->httpMethod = strtoupper($_SERVER['REQUEST_METHOD']); $this->httpMethod = strtoupper($_SERVER['REQUEST_METHOD']);
} }
...@@ -51,6 +46,14 @@ class Router ...@@ -51,6 +46,14 @@ class Router
$this->routes[] = new Route($id, $httpMethod, $routePath, $class, $func); $this->routes[] = new Route($id, $httpMethod, $routePath, $class, $func);
} }
/**
* Return the route definition that matches the given request. If none is found then null is
* returned.
*
* @param string $request Request that will be looked for a match
*
* @return Route|null
*/
public function match($request) public function match($request)
{ {
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
...@@ -70,13 +73,16 @@ class Router ...@@ -70,13 +73,16 @@ class Router
} }
/** /**
* @param $routePath * Build the final regular expression to match the request. This method replaces all the
* parameters' placeholders with the given regular expression.
*
* @param string $routePath Route that might have placeholders
* *
* @return string * @return string
*/ */
private function buildPathRegex($routePath) private function buildPathRegex($routePath)
{ {
return '#^' . str_replace($this->paramsConverterKeys, $this->paramsConverterValues, $routePath) . '$#'; return '#^' . strtr($routePath, $this->paramsConverter) . '$#';
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment