use newest zend modules
[GitHub/Stricted/Domain-Control-Panel.git] / vendor / Zend / Router / Http / Chain.php
1 <?php
2 /**
3 * @link http://github.com/zendframework/zend-router for the canonical source repository
4 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license http://framework.zend.com/license/new-bsd New BSD License
6 */
7
8 namespace Zend\Router\Http;
9
10 use ArrayObject;
11 use Traversable;
12 use Zend\Router\Exception;
13 use Zend\Router\PriorityList;
14 use Zend\Router\RoutePluginManager;
15 use Zend\Stdlib\ArrayUtils;
16 use Zend\Stdlib\RequestInterface as Request;
17
18 /**
19 * Chain route.
20 */
21 class Chain extends TreeRouteStack implements RouteInterface
22 {
23 /**
24 * Chain routes.
25 *
26 * @var array
27 */
28 protected $chainRoutes;
29
30 /**
31 * List of assembled parameters.
32 *
33 * @var array
34 */
35 protected $assembledParams = [];
36
37 /**
38 * Create a new part route.
39 *
40 * @param array $routes
41 * @param RoutePluginManager $routePlugins
42 * @param ArrayObject|null $prototypes
43 */
44 public function __construct(array $routes, RoutePluginManager $routePlugins, ArrayObject $prototypes = null)
45 {
46 $this->chainRoutes = array_reverse($routes);
47 $this->routePluginManager = $routePlugins;
48 $this->routes = new PriorityList();
49 $this->prototypes = $prototypes;
50 }
51
52 /**
53 * factory(): defined by RouteInterface interface.
54 *
55 * @see \Zend\Router\RouteInterface::factory()
56 * @param mixed $options
57 * @throws Exception\InvalidArgumentException
58 * @return Part
59 */
60 public static function factory($options = [])
61 {
62 if ($options instanceof Traversable) {
63 $options = ArrayUtils::iteratorToArray($options);
64 } elseif (!is_array($options)) {
65 throw new Exception\InvalidArgumentException(sprintf(
66 '%s expects an array or Traversable set of options',
67 __METHOD__
68 ));
69 }
70
71 if (!isset($options['routes'])) {
72 throw new Exception\InvalidArgumentException('Missing "routes" in options array');
73 }
74
75 if (!isset($options['prototypes'])) {
76 $options['prototypes'] = null;
77 }
78
79 if ($options['routes'] instanceof Traversable) {
80 $options['routes'] = ArrayUtils::iteratorToArray($options['child_routes']);
81 }
82
83 if (!isset($options['route_plugins'])) {
84 throw new Exception\InvalidArgumentException('Missing "route_plugins" in options array');
85 }
86
87 return new static(
88 $options['routes'],
89 $options['route_plugins'],
90 $options['prototypes']
91 );
92 }
93
94 /**
95 * match(): defined by RouteInterface interface.
96 *
97 * @see \Zend\Router\RouteInterface::match()
98 * @param Request $request
99 * @param int|null $pathOffset
100 * @param array $options
101 * @return RouteMatch|null
102 */
103 public function match(Request $request, $pathOffset = null, array $options = [])
104 {
105 if (!method_exists($request, 'getUri')) {
106 return;
107 }
108
109 if ($pathOffset === null) {
110 $mustTerminate = true;
111 $pathOffset = 0;
112 } else {
113 $mustTerminate = false;
114 }
115
116 if ($this->chainRoutes !== null) {
117 $this->addRoutes($this->chainRoutes);
118 $this->chainRoutes = null;
119 }
120
121 $match = new RouteMatch([]);
122 $uri = $request->getUri();
123 $pathLength = strlen($uri->getPath());
124
125 foreach ($this->routes as $route) {
126 $subMatch = $route->match($request, $pathOffset, $options);
127
128 if ($subMatch === null) {
129 return;
130 }
131
132 $match->merge($subMatch);
133 $pathOffset += $subMatch->getLength();
134 }
135
136 if ($mustTerminate && $pathOffset !== $pathLength) {
137 return;
138 }
139
140 return $match;
141 }
142
143 /**
144 * assemble(): Defined by RouteInterface interface.
145 *
146 * @see \Zend\Router\RouteInterface::assemble()
147 * @param array $params
148 * @param array $options
149 * @return mixed
150 */
151 public function assemble(array $params = [], array $options = [])
152 {
153 if ($this->chainRoutes !== null) {
154 $this->addRoutes($this->chainRoutes);
155 $this->chainRoutes = null;
156 }
157
158 $this->assembledParams = [];
159
160 $routes = ArrayUtils::iteratorToArray($this->routes);
161
162 end($routes);
163 $lastRouteKey = key($routes);
164 $path = '';
165
166 foreach ($routes as $key => $route) {
167 $chainOptions = $options;
168 $hasChild = isset($options['has_child']) ? $options['has_child'] : false;
169
170 $chainOptions['has_child'] = ($hasChild || $key !== $lastRouteKey);
171
172 $path .= $route->assemble($params, $chainOptions);
173 $params = array_diff_key($params, array_flip($route->getAssembledParams()));
174
175 $this->assembledParams += $route->getAssembledParams();
176 }
177
178 return $path;
179 }
180
181 /**
182 * getAssembledParams(): defined by RouteInterface interface.
183 *
184 * @see RouteInterface::getAssembledParams
185 * @return array
186 */
187 public function getAssembledParams()
188 {
189 return $this->assembledParams;
190 }
191 }