13 * to license@zend.com so we can send you a copy immediately. |
13 * to license@zend.com so we can send you a copy immediately. |
14 * |
14 * |
15 * @category Zend |
15 * @category Zend |
16 * @package Zend_Controller |
16 * @package Zend_Controller |
17 * @subpackage Router |
17 * @subpackage Router |
18 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
18 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
19 * @version $Id: Regex.php 24593 2012-01-05 20:35:02Z matthew $ |
19 * @version $Id$ |
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
20 * @license http://framework.zend.com/license/new-bsd New BSD License |
21 */ |
21 */ |
22 |
22 |
23 /** Zend_Controller_Router_Route_Abstract */ |
23 /** Zend_Controller_Router_Route_Abstract */ |
24 require_once 'Zend/Controller/Router/Route/Abstract.php'; |
24 require_once 'Zend/Controller/Router/Route/Abstract.php'; |
26 /** |
26 /** |
27 * Regex Route |
27 * Regex Route |
28 * |
28 * |
29 * @package Zend_Controller |
29 * @package Zend_Controller |
30 * @subpackage Router |
30 * @subpackage Router |
31 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) |
31 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
32 * @license http://framework.zend.com/license/new-bsd New BSD License |
33 */ |
33 */ |
34 class Zend_Controller_Router_Route_Regex extends Zend_Controller_Router_Route_Abstract |
34 class Zend_Controller_Router_Route_Regex extends Zend_Controller_Router_Route_Abstract |
35 { |
35 { |
|
36 |
|
37 /** |
|
38 * Regex string |
|
39 * |
|
40 * @var string|null |
|
41 */ |
36 protected $_regex = null; |
42 protected $_regex = null; |
|
43 |
|
44 /** |
|
45 * Default values for the route (ie. module, controller, action, params) |
|
46 * |
|
47 * @var array |
|
48 */ |
37 protected $_defaults = array(); |
49 protected $_defaults = array(); |
|
50 |
|
51 /** |
|
52 * Reverse |
|
53 * |
|
54 * @var string|null |
|
55 */ |
38 protected $_reverse = null; |
56 protected $_reverse = null; |
|
57 |
|
58 /** |
|
59 * Map |
|
60 * |
|
61 * @var array |
|
62 */ |
39 protected $_map = array(); |
63 protected $_map = array(); |
|
64 |
|
65 /** |
|
66 * Values |
|
67 * |
|
68 * @var array |
|
69 */ |
40 protected $_values = array(); |
70 protected $_values = array(); |
41 |
71 |
42 /** |
72 /** |
43 * Instantiates route based on passed Zend_Config structure |
73 * Instantiates route based on passed Zend_Config structure |
44 * |
74 * |
45 * @param Zend_Config $config Configuration object |
75 * @param Zend_Config $config Configuration object |
|
76 * @return Zend_Controller_Router_Route_Regex |
46 */ |
77 */ |
47 public static function getInstance(Zend_Config $config) |
78 public static function getInstance(Zend_Config $config) |
48 { |
79 { |
49 $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array(); |
80 $defs = ($config->defaults instanceof Zend_Config) ? $config->defaults->toArray() : array(); |
50 $map = ($config->map instanceof Zend_Config) ? $config->map->toArray() : array(); |
81 $map = ($config->map instanceof Zend_Config) ? $config->map->toArray() : array(); |
51 $reverse = (isset($config->reverse)) ? $config->reverse : null; |
82 $reverse = (isset($config->reverse)) ? $config->reverse : null; |
|
83 |
52 return new self($config->route, $defs, $map, $reverse); |
84 return new self($config->route, $defs, $map, $reverse); |
53 } |
85 } |
54 |
86 |
|
87 /** |
|
88 * Constructor |
|
89 * |
|
90 * @param $route |
|
91 * @param array $defaults |
|
92 * @param array $map |
|
93 * @param null $reverse |
|
94 */ |
55 public function __construct($route, $defaults = array(), $map = array(), $reverse = null) |
95 public function __construct($route, $defaults = array(), $map = array(), $reverse = null) |
56 { |
96 { |
57 $this->_regex = $route; |
97 $this->_regex = $route; |
58 $this->_defaults = (array) $defaults; |
98 $this->_defaults = (array) $defaults; |
59 $this->_map = (array) $map; |
99 $this->_map = (array) $map; |
60 $this->_reverse = $reverse; |
100 $this->_reverse = $reverse; |
61 } |
101 } |
62 |
102 |
63 public function getVersion() { |
103 /** |
|
104 * Get the version of the route |
|
105 * |
|
106 * @return int |
|
107 */ |
|
108 public function getVersion() |
|
109 { |
64 return 1; |
110 return 1; |
65 } |
111 } |
66 |
112 |
67 /** |
113 /** |
68 * Matches a user submitted path with a previously defined route. |
114 * Matches a user submitted path with a previously defined route. |
72 * @return array|false An array of assigned values or a false on a mismatch |
118 * @return array|false An array of assigned values or a false on a mismatch |
73 */ |
119 */ |
74 public function match($path, $partial = false) |
120 public function match($path, $partial = false) |
75 { |
121 { |
76 if (!$partial) { |
122 if (!$partial) { |
77 $path = trim(urldecode($path), self::URI_DELIMITER); |
123 $path = trim(urldecode($path), self::URI_DELIMITER); |
78 $regex = '#^' . $this->_regex . '$#i'; |
124 $regex = '#^' . $this->_regex . '$#i'; |
79 } else { |
125 } else { |
80 $regex = '#^' . $this->_regex . '#i'; |
126 $regex = '#^' . $this->_regex . '#i'; |
81 } |
127 } |
82 |
128 |
113 * |
159 * |
114 * Method strips destination type of keys form source array. Ie. if source array is |
160 * Method strips destination type of keys form source array. Ie. if source array is |
115 * indexed numerically then every associative key will be stripped. Vice versa if reversed |
161 * indexed numerically then every associative key will be stripped. Vice versa if reversed |
116 * is set to true. |
162 * is set to true. |
117 * |
163 * |
118 * @param array $values Indexed or associative array of values to map |
164 * @param array $values Indexed or associative array of values to map |
119 * @param boolean $reversed False means translation of index to association. True means reverse. |
165 * @param boolean $reversed False means translation of index to association. True means reverse. |
120 * @param boolean $preserve Should wrong type of keys be preserved or stripped. |
166 * @param boolean $preserve Should wrong type of keys be preserved or stripped. |
121 * @return array An array of mapped values |
167 * @return array An array of mapped values |
122 */ |
168 */ |
123 protected function _getMappedValues($values, $reversed = false, $preserve = false) |
169 protected function _getMappedValues($values, $reversed = false, $preserve = false) |
157 } |
203 } |
158 |
204 |
159 /** |
205 /** |
160 * Assembles a URL path defined by this route |
206 * Assembles a URL path defined by this route |
161 * |
207 * |
162 * @param array $data An array of name (or index) and value pairs used as parameters |
208 * @param array $data An array of name (or index) and value pairs used as parameters |
|
209 * @param boolean $reset |
|
210 * @param boolean $encode |
|
211 * @param boolean $partial |
|
212 * @throws Zend_Controller_Router_Exception |
163 * @return string Route path with user submitted parameters |
213 * @return string Route path with user submitted parameters |
164 */ |
214 */ |
165 public function assemble($data = array(), $reset = false, $encode = false, $partial = false) |
215 public function assemble($data = array(), $reset = false, $encode = false, $partial = false) |
166 { |
216 { |
167 if ($this->_reverse === null) { |
217 if ($this->_reverse === null) { |
168 require_once 'Zend/Controller/Router/Exception.php'; |
218 require_once 'Zend/Controller/Router/Exception.php'; |
169 throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.'); |
219 throw new Zend_Controller_Router_Exception('Cannot assemble. Reversed route is not specified.'); |
170 } |
220 } |
171 |
221 |
172 $defaultValuesMapped = $this->_getMappedValues($this->_defaults, true, false); |
222 $defaultValuesMapped = $this->_getMappedValues($this->_defaults, true, false); |
173 $matchedValuesMapped = $this->_getMappedValues($this->_values, true, false); |
223 $matchedValuesMapped = $this->_getMappedValues($this->_values, true, false); |
174 $dataValuesMapped = $this->_getMappedValues($data, true, false); |
224 $dataValuesMapped = $this->_getMappedValues($data, true, false); |
175 |
225 |
176 // handle resets, if so requested (By null value) to do so |
226 // handle resets, if so requested (By null value) to do so |
177 if (($resetKeys = array_search(null, $dataValuesMapped, true)) !== false) { |
227 if (($resetKeys = array_search(null, $dataValuesMapped, true)) !== false) { |
178 foreach ((array) $resetKeys as $resetKey) { |
228 foreach ((array)$resetKeys as $resetKey) { |
179 if (isset($matchedValuesMapped[$resetKey])) { |
229 if (isset($matchedValuesMapped[$resetKey])) { |
180 unset($matchedValuesMapped[$resetKey]); |
230 unset($matchedValuesMapped[$resetKey]); |
181 unset($dataValuesMapped[$resetKey]); |
231 unset($dataValuesMapped[$resetKey]); |
182 } |
232 } |
183 } |
233 } |
202 require_once 'Zend/Controller/Router/Exception.php'; |
252 require_once 'Zend/Controller/Router/Exception.php'; |
203 throw new Zend_Controller_Router_Exception('Cannot assemble. Too few arguments?'); |
253 throw new Zend_Controller_Router_Exception('Cannot assemble. Too few arguments?'); |
204 } |
254 } |
205 |
255 |
206 return $return; |
256 return $return; |
207 |
|
208 } |
257 } |
209 |
258 |
210 /** |
259 /** |
211 * Return a single parameter of route's defaults |
260 * Return a single parameter of route's defaults |
212 * |
261 * |
213 * @param string $name Array key of the parameter |
262 * @param string $name Array key of the parameter |
214 * @return string Previously set default |
263 * @return string Previously set default |
215 */ |
264 */ |
216 public function getDefault($name) { |
265 public function getDefault($name) |
|
266 { |
217 if (isset($this->_defaults[$name])) { |
267 if (isset($this->_defaults[$name])) { |
218 return $this->_defaults[$name]; |
268 return $this->_defaults[$name]; |
219 } |
269 } |
220 } |
270 } |
221 |
271 |
222 /** |
272 /** |
223 * Return an array of defaults |
273 * Return an array of defaults |
224 * |
274 * |
225 * @return array Route defaults |
275 * @return array Route defaults |
226 */ |
276 */ |
227 public function getDefaults() { |
277 public function getDefaults() |
|
278 { |
228 return $this->_defaults; |
279 return $this->_defaults; |
229 } |
280 } |
230 |
281 |
231 /** |
282 /** |
232 * Get all variables which are used by the route |
283 * Get all variables which are used by the route |