diff --git a/AltoRouter.php b/AltoRouter.php index 7e3acac..ee0759b 100644 --- a/AltoRouter.php +++ b/AltoRouter.php @@ -5,15 +5,26 @@ class AltoRouter { protected $routes = array(); protected $namedRoutes = array(); protected $basePath = ''; + protected $matchTypes = array( + 'i' => '[0-9]++', + 'a' => '[0-9A-Za-z]++', + 'h' => '[0-9A-Fa-f]++', + '*' => '.+?', + '**' => '.++', + '' => '[^/\.]++' + ); /** * Create router in one call from config. * * @param array $routes * @param string $basePath + * @param array $matchTypes */ - public function __construct( $routes = array(), $basePath = '' ) { + public function __construct( $routes = array(), $basePath = '', $matchTypes = array() ) { $this->basePath = $basePath; + $this->matchTypes = array_merge($this->matchTypes, $matchTypes); + foreach( $routes as $route ) { call_user_func_array(array($this,'map'),$route); } @@ -27,6 +38,15 @@ class AltoRouter { $this->basePath = $basePath; } + /** + * Add a new named match type. It uses array_merge so keys can be overwritten. + * + * @param array $matchType The key is the name and the value is the regex. + */ + public function addMatchType($matchTypes) { + $this->matchTypes = array_merge($this->matchTypes, $matchTypes); + } + /** * Map a route to a target * @@ -205,20 +225,12 @@ class AltoRouter { private function compileRoute($route) { if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { - $match_types = array( - 'i' => '[0-9]++', - 'a' => '[0-9A-Za-z]++', - 'h' => '[0-9A-Fa-f]++', - '*' => '.+?', - '**' => '.++', - '' => '[^/\.]++' - ); - + $matchTypes = $this->matchTypes; foreach ($matches as $match) { list($block, $pre, $type, $param, $optional) = $match; - if (isset($match_types[$type])) { - $type = $match_types[$type]; + if (isset($matchTypes[$type])) { + $type = $matchTypes[$type]; } if ($pre === '.') { $pre = '\.'; diff --git a/AltoRouterTest.php b/AltoRouterTest.php index f40176b..16199a0 100644 --- a/AltoRouterTest.php +++ b/AltoRouterTest.php @@ -3,7 +3,7 @@ require 'AltoRouter.php'; class AltoRouterDebug extends AltoRouter{ - + public function getNamedRoutes(){ return $this->namedRoutes; } @@ -15,6 +15,7 @@ class AltoRouterDebug extends AltoRouter{ public function getBasePath(){ return $this->basePath; } + } /** @@ -264,4 +265,29 @@ class AltoRouterTest extends PHPUnit_Framework_TestCase $this->assertFalse($this->router->match('/some-other-thing', 'GET')); } + + public function testMatchWithCustomNamedRegex() + { + $this->router->addMatchType(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); + $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); + + $this->assertEquals(array( + 'target' => 'bar_action', + 'params' => array( + 'customId' => 'AB1', + ), + 'name' => 'bar_route' + ), $this->router->match('/bar/AB1', 'GET')); + + $this->assertEquals(array( + 'target' => 'bar_action', + 'params' => array( + 'customId' => 'AB1_0123456789', + ), + 'name' => 'bar_route' + ), $this->router->match('/bar/AB1_0123456789', 'GET')); + + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); + + } }