ce1c8cf7bb15aab9a2bc706f6941a3faa6c2c6b3
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Mvc / Router / Http / Regex.php
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10 namespace Zend\Mvc\Router\Http;
11
12 use Traversable;
13 use Zend\Mvc\Router\Exception;
14 use Zend\Stdlib\ArrayUtils;
15 use Zend\Stdlib\RequestInterface as Request;
16
17 /**
18 * Regex route.
19 */
20 class Regex implements RouteInterface
21 {
22 /**
23 * Regex to match.
24 *
25 * @var string
26 */
27 protected $regex;
28
29 /**
30 * Default values.
31 *
32 * @var array
33 */
34 protected $defaults;
35
36 /**
37 * Specification for URL assembly.
38 *
39 * Parameters accepting substitutions should be denoted as "%key%"
40 *
41 * @var string
42 */
43 protected $spec;
44
45 /**
46 * List of assembled parameters.
47 *
48 * @var array
49 */
50 protected $assembledParams = [];
51
52 /**
53 * Create a new regex route.
54 *
55 * @param string $regex
56 * @param string $spec
57 * @param array $defaults
58 */
59 public function __construct($regex, $spec, array $defaults = [])
60 {
61 $this->regex = $regex;
62 $this->spec = $spec;
63 $this->defaults = $defaults;
64 }
65
66 /**
67 * factory(): defined by RouteInterface interface.
68 *
69 * @see \Zend\Mvc\Router\RouteInterface::factory()
70 * @param array|Traversable $options
71 * @return Regex
72 * @throws \Zend\Mvc\Router\Exception\InvalidArgumentException
73 */
74 public static function factory($options = [])
75 {
76 if ($options instanceof Traversable) {
77 $options = ArrayUtils::iteratorToArray($options);
78 } elseif (!is_array($options)) {
79 throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
80 }
81
82 if (!isset($options['regex'])) {
83 throw new Exception\InvalidArgumentException('Missing "regex" in options array');
84 }
85
86 if (!isset($options['spec'])) {
87 throw new Exception\InvalidArgumentException('Missing "spec" in options array');
88 }
89
90 if (!isset($options['defaults'])) {
91 $options['defaults'] = [];
92 }
93
94 return new static($options['regex'], $options['spec'], $options['defaults']);
95 }
96
97 /**
98 * match(): defined by RouteInterface interface.
99 *
100 * @param Request $request
101 * @param int $pathOffset
102 * @return RouteMatch|null
103 */
104 public function match(Request $request, $pathOffset = null)
105 {
106 if (!method_exists($request, 'getUri')) {
107 return;
108 }
109
110 $uri = $request->getUri();
111 $path = $uri->getPath();
112
113 if ($pathOffset !== null) {
114 $result = preg_match('(\G' . $this->regex . ')', $path, $matches, null, $pathOffset);
115 } else {
116 $result = preg_match('(^' . $this->regex . '$)', $path, $matches);
117 }
118
119 if (!$result) {
120 return;
121 }
122
123 $matchedLength = strlen($matches[0]);
124
125 foreach ($matches as $key => $value) {
126 if (is_numeric($key) || is_int($key) || $value === '') {
127 unset($matches[$key]);
128 } else {
129 $matches[$key] = rawurldecode($value);
130 }
131 }
132
133 return new RouteMatch(array_merge($this->defaults, $matches), $matchedLength);
134 }
135
136 /**
137 * assemble(): Defined by RouteInterface interface.
138 *
139 * @see \Zend\Mvc\Router\RouteInterface::assemble()
140 * @param array $params
141 * @param array $options
142 * @return mixed
143 */
144 public function assemble(array $params = [], array $options = [])
145 {
146 $url = $this->spec;
147 $mergedParams = array_merge($this->defaults, $params);
148 $this->assembledParams = [];
149
150 foreach ($mergedParams as $key => $value) {
151 $spec = '%' . $key . '%';
152
153 if (strpos($url, $spec) !== false) {
154 $url = str_replace($spec, rawurlencode($value), $url);
155
156 $this->assembledParams[] = $key;
157 }
158 }
159
160 return $url;
161 }
162
163 /**
164 * getAssembledParams(): defined by RouteInterface interface.
165 *
166 * @see RouteInterface::getAssembledParams
167 * @return array
168 */
169 public function getAssembledParams()
170 {
171 return $this->assembledParams;
172 }
173 }