Regex.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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-2015 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-2015 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. {
  99. return 1;
  100. }
  101. /**
  102. * Matches a user submitted path with a previously defined route.
  103. * Assigns and returns an array of defaults on a successful match.
  104. *
  105. * @param string $path Path used to match against this routing map
  106. * @return array|false An array of assigned values or a false on a mismatch
  107. */
  108. public function match($path, $partial = false)
  109. {
  110. if (!$partial) {
  111. $path = trim(urldecode($path), self::URI_DELIMITER);
  112. $regex = '#^' . $this->_regex . '$#i';
  113. } else {
  114. $regex = '#^' . $this->_regex . '#i';
  115. }
  116. $res = preg_match($regex, $path, $values);
  117. if ($res === 0) {
  118. return false;
  119. }
  120. if ($partial) {
  121. $this->setMatchedPath($values[0]);
  122. }
  123. // array_filter_key()? Why isn't this in a standard PHP function set yet? :)
  124. foreach ($values as $i => $value) {
  125. if (!is_int($i) || $i === 0) {
  126. unset($values[$i]);
  127. }
  128. }
  129. $this->_values = $values;
  130. $values = $this->_getMappedValues($values);
  131. $defaults = $this->_getMappedValues($this->_defaults, false, true);
  132. $return = $values + $defaults;
  133. return $return;
  134. }
  135. /**
  136. * Maps numerically indexed array values to it's associative mapped counterpart.
  137. * Or vice versa. Uses user provided map array which consists of index => name
  138. * parameter mapping. If map is not found, it returns original array.
  139. *
  140. * Method strips destination type of keys form source array. Ie. if source array is
  141. * indexed numerically then every associative key will be stripped. Vice versa if reversed
  142. * is set to true.
  143. *
  144. * @param array $values Indexed or associative array of values to map
  145. * @param boolean $reversed False means translation of index to association. True means reverse.
  146. * @param boolean $preserve Should wrong type of keys be preserved or stripped.
  147. * @return array An array of mapped values
  148. */
  149. protected function _getMappedValues($values, $reversed = false, $preserve = false)
  150. {
  151. if (count($this->_map) == 0) {
  152. return $values;
  153. }
  154. $return = array();
  155. foreach ($values as $key => $value) {
  156. if (is_int($key) && !$reversed) {
  157. if (array_key_exists($key, $this->_map)) {
  158. $index = $this->_map[$key];
  159. } elseif (false === ($index = array_search($key, $this->_map))) {
  160. $index = $key;
  161. }
  162. $return[$index] = $values[$key];
  163. } elseif ($reversed) {
  164. $index = $key;
  165. if (!is_int($key)) {
  166. if (array_key_exists($key, $this->_map)) {
  167. $index = $this->_map[$key];
  168. } else {
  169. $index = array_search($key, $this->_map, true);
  170. }
  171. }
  172. if (false !== $index) {
  173. $return[$index] = $values[$key];
  174. }
  175. } elseif ($preserve) {
  176. $return[$key] = $value;
  177. }
  178. }
  179. return $return;
  180. }
  181. /**
  182. * Assembles a URL path defined by this route
  183. *
  184. * @param array $data An array of name (or index) and value pairs used as parameters
  185. * @param boolean $reset
  186. * @param boolean $encode
  187. * @param boolean $partial
  188. * @throws Zend_Controller_Router_Exception
  189. * @return string Route path with user submitted parameters
  190. */
  191. public function assemble($data = array(), $reset = false, $encode = false, $partial = false)
  192. {
  193. if ($this->_reverse === null) {
  194. require_once 'Zend/Controller/Router/Exception.php';
  195. throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.');
  196. }
  197. $defaultValuesMapped = $this->_getMappedValues($this->_defaults, true, false);
  198. $matchedValuesMapped = $this->_getMappedValues($this->_values, true, false);
  199. $dataValuesMapped = $this->_getMappedValues($data, true, false);
  200. // handle resets, if so requested (By null value) to do so
  201. if (($resetKeys = array_search(null, $dataValuesMapped, true)) !== false) {
  202. foreach ((array)$resetKeys as $resetKey) {
  203. if (isset($matchedValuesMapped[$resetKey])) {
  204. unset($matchedValuesMapped[$resetKey]);
  205. unset($dataValuesMapped[$resetKey]);
  206. }
  207. }
  208. }
  209. // merge all the data together, first defaults, then values matched, then supplied
  210. $mergedData = $defaultValuesMapped;
  211. $mergedData = $this->_arrayMergeNumericKeys($mergedData, $matchedValuesMapped);
  212. $mergedData = $this->_arrayMergeNumericKeys($mergedData, $dataValuesMapped);
  213. if ($encode) {
  214. foreach ($mergedData as $key => &$value) {
  215. $value = urlencode($value);
  216. }
  217. }
  218. ksort($mergedData);
  219. $return = @vsprintf($this->_reverse, $mergedData);
  220. if ($return === false) {
  221. require_once 'Zend/Controller/Router/Exception.php';
  222. throw new Zend_Controller_Router_Exception('Cannot assemble. Too few arguments?');
  223. }
  224. return $return;
  225. }
  226. /**
  227. * Return a single parameter of route's defaults
  228. *
  229. * @param string $name Array key of the parameter
  230. * @return string Previously set default
  231. */
  232. public function getDefault($name)
  233. {
  234. if (isset($this->_defaults[$name])) {
  235. return $this->_defaults[$name];
  236. }
  237. }
  238. /**
  239. * Return an array of defaults
  240. *
  241. * @return array Route defaults
  242. */
  243. public function getDefaults()
  244. {
  245. return $this->_defaults;
  246. }
  247. /**
  248. * Get all variables which are used by the route
  249. *
  250. * @return array
  251. */
  252. public function getVariables()
  253. {
  254. $variables = array();
  255. foreach ($this->_map as $key => $value) {
  256. if (is_numeric($key)) {
  257. $variables[] = $value;
  258. } else {
  259. $variables[] = $key;
  260. }
  261. }
  262. return $variables;
  263. }
  264. /**
  265. * _arrayMergeNumericKeys() - allows for a strict key (numeric's included) array_merge.
  266. * php's array_merge() lacks the ability to merge with numeric keys.
  267. *
  268. * @param array $array1
  269. * @param array $array2
  270. * @return array
  271. */
  272. protected function _arrayMergeNumericKeys(Array $array1, Array $array2)
  273. {
  274. $returnArray = $array1;
  275. foreach ($array2 as $array2Index => $array2Value) {
  276. $returnArray[$array2Index] = $array2Value;
  277. }
  278. return $returnArray;
  279. }
  280. }