Regex.php 8.5 KB

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