From aec1f5f10cc31f5ddc65998d915eee12351c999f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 14 Oct 2015 13:54:53 +0200 Subject: [PATCH] =?UTF-8?q?[FEATURE]=C2=A0Features=20and=20optimisations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added where method to RouterRoute to support custom regular expression matches on routes. - Moved parameters property to RouterRoute class. - Added IRouteEntry class. --- src/Pecee/SimpleRouter/IRouteEntry.php | 7 ++++ src/Pecee/SimpleRouter/RouterEntry.php | 18 ---------- src/Pecee/SimpleRouter/RouterRoute.php | 48 ++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 20 deletions(-) create mode 100644 src/Pecee/SimpleRouter/IRouteEntry.php diff --git a/src/Pecee/SimpleRouter/IRouteEntry.php b/src/Pecee/SimpleRouter/IRouteEntry.php new file mode 100644 index 0000000..d13e752 --- /dev/null +++ b/src/Pecee/SimpleRouter/IRouteEntry.php @@ -0,0 +1,7 @@ +settings = array(); - $this->parameters = array(); } protected function loadClass($name) { @@ -63,22 +61,6 @@ abstract class RouterEntry { return $this->callback; } - /** - * @return mixed - */ - public function getParameters(){ - return $this->parameters; - } - - /** - * @param mixed $parameters - * @return self - */ - public function setParameters($parameters) { - $this->parameters = $parameters; - return $this; - } - /** * @param string $prefix * @return self diff --git a/src/Pecee/SimpleRouter/RouterRoute.php b/src/Pecee/SimpleRouter/RouterRoute.php index 9498037..3944c25 100644 --- a/src/Pecee/SimpleRouter/RouterRoute.php +++ b/src/Pecee/SimpleRouter/RouterRoute.php @@ -9,6 +9,8 @@ class RouterRoute extends RouterEntry { protected $url; protected $requestTypes; + protected $parameters; + protected $parametersRegex; public function __construct($url, $callback) { parent::__construct(); @@ -17,6 +19,8 @@ class RouterRoute extends RouterEntry { $this->settings['aliases'] = array(); $this->requestTypes = array(); + $this->parameters = array(); + $this->parametersRegex = array(); } protected function parseParameters($url) { @@ -73,7 +77,21 @@ class RouterRoute extends RouterEntry { // Save parameter if we have one if($parameter) { - $parameters[$parameter] = $url[$i]; + $parameterValue = $url[$i]; + $regex = (isset($this->parametersRegex[$parameter]) ? $this->parametersRegex[$parameter] : null); + + if($regex !== null) { + // Use the regular expression rule provided to filter the value + $matches = array(); + preg_match('/'.$regex.'/is', $url[$i], $matches); + + if(count($matches)) { + $parameterValue = $matches[0]; + } + } + + // Add parameter value + $parameters[$parameter] = $parameterValue; } } @@ -82,7 +100,6 @@ class RouterRoute extends RouterEntry { $this->parameters = $parameters; return $this; } - } } @@ -161,4 +178,31 @@ class RouterRoute extends RouterEntry { public function getRequestTypes() { return $this->requestTypes; } + + /** + * @return mixed + */ + public function getParameters(){ + return $this->parameters; + } + + /** + * @param mixed $parameters + * @return self + */ + public function setParameters($parameters) { + $this->parameters = $parameters; + return $this; + } + + /** + * Add regular expression parameter match + * + * @param array $options + * @return self + */ + public function where(array $options) { + $this->parametersRegex = array_merge($this->parametersRegex, $options); + return $this; + } } \ No newline at end of file