1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Controller
* @subpackage Router
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id: Regex.php 20096 2010-01-06 02:05:09Z bkarwin $
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/** Zend_Controller_Router_Route_Abstract */
require_once 'Zend/Controller/Router/Route/Abstract.php';
/**
* Regex Route
*
* @package Zend_Controller
* @subpackage Router
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Controller_Router_Route_Regex extends Zend_Controller_Router_Route_Abstract
{
protected $_regex = null;
protected $_defaults = array();
protected $_reverse = null;
protected $_map = array();
protected $_values = array();
/**
* Instantiates route based on passed Zend_Config structure
*
* @param Zend_Config $config Configuration object
*/
public static function getInstance(Zend_Config $config)
{
$defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array();
$map = ($config->map instanceof Zend_Config) ? $config->map->toArray() : array();
$reverse = (isset($config->reverse)) ? $config->reverse : null;
return new self($config->route, $defs, $map, $reverse);
}
public function __construct($route, $defaults = array(), $map = array(), $reverse = null)
{
$this->_regex = $route;
$this->_defaults = (array) $defaults;
$this->_map = (array) $map;
$this->_reverse = $reverse;
}
public function getVersion() {
return 1;
}
/**
* Matches a user submitted path with a previously defined route.
* Assigns and returns an array of defaults on a successful match.
*
* @param string $path Path used to match against this routing map
* @return array|false An array of assigned values or a false on a mismatch
*/
public function match($path, $partial = false)
{
if (!$partial) {
$path = trim(urldecode($path), '/');
$regex = '#^' . $this->_regex . '$#i';
} else {
$regex = '#^' . $this->_regex . '#i';
}
$res = preg_match($regex, $path, $values);
if ($res === 0) {
return false;
}
if ($partial) {
$this->setMatchedPath($values[0]);
}
// array_filter_key()? Why isn't this in a standard PHP function set yet? :)
foreach ($values as $i => $value) {
if (!is_int($i) || $i === 0) {
unset($values[$i]);
}
}
$this->_values = $values;
$values = $this->_getMappedValues($values);
$defaults = $this->_getMappedValues($this->_defaults, false, true);
$return = $values + $defaults;
return $return;
}
/**
* Maps numerically indexed array values to it's associative mapped counterpart.
* Or vice versa. Uses user provided map array which consists of index => name
* parameter mapping. If map is not found, it returns original array.
*
* Method strips destination type of keys form source array. Ie. if source array is
* indexed numerically then every associative key will be stripped. Vice versa if reversed
* is set to true.
*
* @param array $values Indexed or associative array of values to map
* @param boolean $reversed False means translation of index to association. True means reverse.
* @param boolean $preserve Should wrong type of keys be preserved or stripped.
* @return array An array of mapped values
*/
protected function _getMappedValues($values, $reversed = false, $preserve = false)
{
if (count($this->_map) == 0) {
return $values;
}
$return = array();
foreach ($values as $key => $value) {
if (is_int($key) && !$reversed) {
if (array_key_exists($key, $this->_map)) {
$index = $this->_map[$key];
} elseif (false === ($index = array_search($key, $this->_map))) {
$index = $key;
}
$return[$index] = $values[$key];
} elseif ($reversed) {
$index = $key;
if (!is_int($key)) {
if (array_key_exists($key, $this->_map)) {
$index = $this->_map[$key];
} else {
$index = array_search($key, $this->_map, true);
}
}
if (false !== $index) {
$return[$index] = $values[$key];
}
} elseif ($preserve) {
$return[$key] = $value;
}
}
return $return;
}
/**
* Assembles a URL path defined by this route
*
* @param array $data An array of name (or index) and value pairs used as parameters
* @return string Route path with user submitted parameters
*/
public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
{
if ($this->_reverse === null) {
require_once 'Zend/Controller/Router/Exception.php';
throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.');
}
$defaultValuesMapped = $this->_getMappedValues($this->_defaults, true, false);
$matchedValuesMapped = $this->_getMappedValues($this->_values, true, false);
$dataValuesMapped = $this->_getMappedValues($data, true, false);
// handle resets, if so requested (By null value) to do so
if (($resetKeys = array_search(null, $dataValuesMapped, true)) !== false) {
foreach ((array) $resetKeys as $resetKey) {
if (isset($matchedValuesMapped[$resetKey])) {
unset($matchedValuesMapped[$resetKey]);
unset($dataValuesMapped[$resetKey]);
}
}
}
// merge all the data together, first defaults, then values matched, then supplied
$mergedData = $defaultValuesMapped;
$mergedData = $this->_arrayMergeNumericKeys($mergedData, $matchedValuesMapped);
$mergedData = $this->_arrayMergeNumericKeys($mergedData, $dataValuesMapped);
if ($encode) {
foreach ($mergedData as $key => &$value) {
$value = urlencode($value);
}
}
ksort($mergedData);
$return = @vsprintf($this->_reverse, $mergedData);
if ($return === false) {
require_once 'Zend/Controller/Router/Exception.php';
throw new Zend_Controller_Router_Exception('Cannot assemble. Too few arguments?');
}
return $return;
}
/**
* Return a single parameter of route's defaults
*
* @param string $name Array key of the parameter
* @return string Previously set default
*/
public function getDefault($name) {
if (isset($this->_defaults[$name])) {
return $this->_defaults[$name];
}
}
/**
* Return an array of defaults
*
* @return array Route defaults
*/
public function getDefaults() {
return $this->_defaults;
}
/**
* Get all variables which are used by the route
*
* @return array
*/
public function getVariables()
{
$variables = array();
foreach ($this->_map as $key => $value) {
if (is_numeric($key)) {
$variables[] = $value;
} else {
$variables[] = $key;
}
}
return $variables;
}
/**
* _arrayMergeNumericKeys() - allows for a strict key (numeric's included) array_merge.
* php's array_merge() lacks the ability to merge with numeric keys.
*
* @param array $array1
* @param array $array2
* @return array
*/
protected function _arrayMergeNumericKeys(Array $array1, Array $array2)
{
$returnArray = $array1;
foreach ($array2 as $array2Index => $array2Value) {
$returnArray[$array2Index] = $array2Value;
}
return $returnArray;
}
}
|