PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Controller/Router/Route/Regex.php

http://firephp.googlecode.com/
PHP | 230 lines | 117 code | 31 blank | 82 comment | 15 complexity | bc276aabf095e0c1b6b82cf16458ceed MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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: Regex.php 12525 2008-11-10 20:18:32Z ralph $
  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 . '$#i';
  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)
  68. {
  69. $path = trim(urldecode($path), '/');
  70. $res = preg_match($this->_regex, $path, $values);
  71. if ($res === 0) return false;
  72. // array_filter_key()? Why isn't this in a standard PHP function set yet? :)
  73. foreach ($values as $i => $value) {
  74. if (!is_int($i) || $i === 0) {
  75. unset($values[$i]);
  76. }
  77. }
  78. $this->_values = $values;
  79. $values = $this->_getMappedValues($values);
  80. $defaults = $this->_getMappedValues($this->_defaults, false, true);
  81. $return = $values + $defaults;
  82. return $return;
  83. }
  84. /**
  85. * Maps numerically indexed array values to it's associative mapped counterpart.
  86. * Or vice versa. Uses user provided map array which consists of index => name
  87. * parameter mapping. If map is not found, it returns original array.
  88. *
  89. * Method strips destination type of keys form source array. Ie. if source array is
  90. * indexed numerically then every associative key will be stripped. Vice versa if reversed
  91. * is set to true.
  92. *
  93. * @param array $values Indexed or associative array of values to map
  94. * @param boolean $reversed False means translation of index to association. True means reverse.
  95. * @param boolean $preserve Should wrong type of keys be preserved or stripped.
  96. * @return array An array of mapped values
  97. */
  98. protected function _getMappedValues($values, $reversed = false, $preserve = false)
  99. {
  100. if (count($this->_map) == 0) {
  101. return $values;
  102. }
  103. $return = array();
  104. foreach ($values as $key => $value) {
  105. if (is_int($key) && !$reversed) {
  106. if (array_key_exists($key, $this->_map)) {
  107. $index = $this->_map[$key];
  108. } elseif (false === ($index = array_search($key, $this->_map))) {
  109. $index = $key;
  110. }
  111. $return[$index] = $values[$key];
  112. } elseif ($reversed) {
  113. $index = (!is_int($key)) ? array_search($key, $this->_map, true) : $key;
  114. if (false !== $index) {
  115. $return[$index] = $values[$key];
  116. }
  117. } elseif ($preserve) {
  118. $return[$key] = $value;
  119. }
  120. }
  121. return $return;
  122. }
  123. /**
  124. * Assembles a URL path defined by this route
  125. *
  126. * @param array $data An array of name (or index) and value pairs used as parameters
  127. * @return string Route path with user submitted parameters
  128. */
  129. public function assemble($data = array(), $reset = false, $encode = false)
  130. {
  131. if ($this->_reverse === null) {
  132. require_once 'Zend/Controller/Router/Exception.php';
  133. throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.');
  134. }
  135. $defaultValuesMapped = $this->_getMappedValues($this->_defaults, true, false);
  136. $matchedValuesMapped = $this->_getMappedValues($this->_values, true, false);
  137. $dataValuesMapped = $this->_getMappedValues($data, true, false);
  138. // handle resets, if so requested (By null value) to do so
  139. if (($resetKeys = array_search(null, $dataValuesMapped, true)) !== false) {
  140. foreach ((array) $resetKeys as $resetKey) {
  141. if (isset($matchedValuesMapped[$resetKey])) {
  142. unset($matchedValuesMapped[$resetKey]);
  143. unset($dataValuesMapped[$resetKey]);
  144. }
  145. }
  146. }
  147. // merge all the data together, first defaults, then values matched, then supplied
  148. $mergedData = $defaultValuesMapped;
  149. $mergedData = $this->_arrayMergeNumericKeys($mergedData, $matchedValuesMapped);
  150. $mergedData = $this->_arrayMergeNumericKeys($mergedData, $dataValuesMapped);
  151. if ($encode) {
  152. foreach ($mergedData as $key => &$value) {
  153. $value = urlencode($value);
  154. }
  155. }
  156. ksort($mergedData);
  157. $return = @vsprintf($this->_reverse, $mergedData);
  158. if ($return === false) {
  159. require_once 'Zend/Controller/Router/Exception.php';
  160. throw new Zend_Controller_Router_Exception('Cannot assemble. Too few arguments?');
  161. }
  162. return $return;
  163. }
  164. /**
  165. * Return a single parameter of route's defaults
  166. *
  167. * @param string $name Array key of the parameter
  168. * @return string Previously set default
  169. */
  170. public function getDefault($name) {
  171. if (isset($this->_defaults[$name])) {
  172. return $this->_defaults[$name];
  173. }
  174. }
  175. /**
  176. * Return an array of defaults
  177. *
  178. * @return array Route defaults
  179. */
  180. public function getDefaults() {
  181. return $this->_defaults;
  182. }
  183. /**
  184. * _arrayMergeNumericKeys() - allows for a strict key (numeric's included) array_merge.
  185. * php's array_merge() lacks the ability to merge with numeric keys.
  186. *
  187. * @param array $array1
  188. * @param array $array2
  189. * @return array
  190. */
  191. protected function _arrayMergeNumericKeys(Array $array1, Array $array2)
  192. {
  193. $returnArray = $array1;
  194. foreach ($array2 as $array2Index => $array2Value) {
  195. $returnArray[$array2Index] = $array2Value;
  196. }
  197. return $returnArray;
  198. }
  199. }