mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-07-11 22:22:16 +00:00
[FEATURE] Features and optimisations
- Added where method to RouterRoute to support custom regular expression matches on routes. - Moved parameters property to RouterRoute class. - Added IRouteEntry class.
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Pecee\SimpleRouter;
|
||||||
|
|
||||||
|
interface IRouteEntry {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -18,11 +18,9 @@ abstract class RouterEntry {
|
|||||||
|
|
||||||
protected $settings;
|
protected $settings;
|
||||||
protected $callback;
|
protected $callback;
|
||||||
protected $parameters;
|
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->settings = array();
|
$this->settings = array();
|
||||||
$this->parameters = array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function loadClass($name) {
|
protected function loadClass($name) {
|
||||||
@@ -63,22 +61,6 @@ abstract class RouterEntry {
|
|||||||
return $this->callback;
|
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
|
* @param string $prefix
|
||||||
* @return self
|
* @return self
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ class RouterRoute extends RouterEntry {
|
|||||||
|
|
||||||
protected $url;
|
protected $url;
|
||||||
protected $requestTypes;
|
protected $requestTypes;
|
||||||
|
protected $parameters;
|
||||||
|
protected $parametersRegex;
|
||||||
|
|
||||||
public function __construct($url, $callback) {
|
public function __construct($url, $callback) {
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
@@ -17,6 +19,8 @@ class RouterRoute extends RouterEntry {
|
|||||||
|
|
||||||
$this->settings['aliases'] = array();
|
$this->settings['aliases'] = array();
|
||||||
$this->requestTypes = array();
|
$this->requestTypes = array();
|
||||||
|
$this->parameters = array();
|
||||||
|
$this->parametersRegex = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseParameters($url) {
|
protected function parseParameters($url) {
|
||||||
@@ -73,7 +77,21 @@ class RouterRoute extends RouterEntry {
|
|||||||
|
|
||||||
// Save parameter if we have one
|
// Save parameter if we have one
|
||||||
if($parameter) {
|
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;
|
$this->parameters = $parameters;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,4 +178,31 @@ class RouterRoute extends RouterEntry {
|
|||||||
public function getRequestTypes() {
|
public function getRequestTypes() {
|
||||||
return $this->requestTypes;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user