Regex.php 9.4 KB

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