PageRenderTime 27ms CodeModel.GetById 60ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/Controller/Router/Route/Regex.php

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