fix calendar css, take 2. (#213)
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / Router / Http / Hostname.php
blob03b24baf0232f763007e61dbcba6edfa4caaf724
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
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 */
10 namespace Zend\Mvc\Router\Http;
12 use Traversable;
13 use Zend\Mvc\Router\Exception;
14 use Zend\Stdlib\ArrayUtils;
15 use Zend\Stdlib\RequestInterface as Request;
17 /**
18 * Hostname route.
20 class Hostname implements RouteInterface
22 /**
23 * Parts of the route.
25 * @var array
27 protected $parts;
29 /**
30 * Regex used for matching the route.
32 * @var string
34 protected $regex;
36 /**
37 * Map from regex groups to parameter names.
39 * @var array
41 protected $paramMap = array();
43 /**
44 * Default values.
46 * @var array
48 protected $defaults;
50 /**
51 * List of assembled parameters.
53 * @var array
55 protected $assembledParams = array();
57 /**
58 * Create a new hostname route.
60 * @param string $route
61 * @param array $constraints
62 * @param array $defaults
64 public function __construct($route, array $constraints = array(), array $defaults = array())
66 $this->defaults = $defaults;
67 $this->parts = $this->parseRouteDefinition($route);
68 $this->regex = $this->buildRegex($this->parts, $constraints);
71 /**
72 * factory(): defined by RouteInterface interface.
74 * @see \Zend\Mvc\Router\RouteInterface::factory()
75 * @param array|Traversable $options
76 * @return Hostname
77 * @throws Exception\InvalidArgumentException
79 public static function factory($options = array())
81 if ($options instanceof Traversable) {
82 $options = ArrayUtils::iteratorToArray($options);
83 } elseif (!is_array($options)) {
84 throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
87 if (!isset($options['route'])) {
88 throw new Exception\InvalidArgumentException('Missing "route" in options array');
91 if (!isset($options['constraints'])) {
92 $options['constraints'] = array();
95 if (!isset($options['defaults'])) {
96 $options['defaults'] = array();
99 return new static($options['route'], $options['constraints'], $options['defaults']);
103 * Parse a route definition.
105 * @param string $def
106 * @return array
107 * @throws Exception\RuntimeException
109 protected function parseRouteDefinition($def)
111 $currentPos = 0;
112 $length = strlen($def);
113 $parts = array();
114 $levelParts = array(&$parts);
115 $level = 0;
117 while ($currentPos < $length) {
118 if (!preg_match('(\G(?P<literal>[a-z0-9-.]*)(?P<token>[:{\[\]]|$))', $def, $matches, 0, $currentPos)) {
119 throw new Exception\RuntimeException('Matched hostname literal contains a disallowed character');
122 $currentPos += strlen($matches[0]);
124 if (!empty($matches['literal'])) {
125 $levelParts[$level][] = array('literal', $matches['literal']);
128 if ($matches['token'] === ':') {
129 if (!preg_match('(\G(?P<name>[^:.{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)', $def, $matches, 0, $currentPos)) {
130 throw new Exception\RuntimeException('Found empty parameter name');
133 $levelParts[$level][] = array('parameter', $matches['name'], isset($matches['delimiters']) ? $matches['delimiters'] : null);
135 $currentPos += strlen($matches[0]);
136 } elseif ($matches['token'] === '[') {
137 $levelParts[$level][] = array('optional', array());
138 $levelParts[$level + 1] = &$levelParts[$level][count($levelParts[$level]) - 1][1];
140 $level++;
141 } elseif ($matches['token'] === ']') {
142 unset($levelParts[$level]);
143 $level--;
145 if ($level < 0) {
146 throw new Exception\RuntimeException('Found closing bracket without matching opening bracket');
148 } else {
149 break;
153 if ($level > 0) {
154 throw new Exception\RuntimeException('Found unbalanced brackets');
157 return $parts;
161 * Build the matching regex from parsed parts.
163 * @param array $parts
164 * @param array $constraints
165 * @param int $groupIndex
166 * @return string
167 * @throws Exception\RuntimeException
169 protected function buildRegex(array $parts, array $constraints, &$groupIndex = 1)
171 $regex = '';
173 foreach ($parts as $part) {
174 switch ($part[0]) {
175 case 'literal':
176 $regex .= preg_quote($part[1]);
177 break;
179 case 'parameter':
180 $groupName = '?P<param' . $groupIndex . '>';
182 if (isset($constraints[$part[1]])) {
183 $regex .= '(' . $groupName . $constraints[$part[1]] . ')';
184 } elseif ($part[2] === null) {
185 $regex .= '(' . $groupName . '[^.]+)';
186 } else {
187 $regex .= '(' . $groupName . '[^' . $part[2] . ']+)';
190 $this->paramMap['param' . $groupIndex++] = $part[1];
191 break;
193 case 'optional':
194 $regex .= '(?:' . $this->buildRegex($part[1], $constraints, $groupIndex) . ')?';
195 break;
199 return $regex;
203 * Build host.
205 * @param array $parts
206 * @param array $mergedParams
207 * @param bool $isOptional
208 * @return string
209 * @throws Exception\RuntimeException
210 * @throws Exception\InvalidArgumentException
212 protected function buildHost(array $parts, array $mergedParams, $isOptional)
214 $host = '';
215 $skip = true;
216 $skippable = false;
218 foreach ($parts as $part) {
219 switch ($part[0]) {
220 case 'literal':
221 $host .= $part[1];
222 break;
224 case 'parameter':
225 $skippable = true;
227 if (!isset($mergedParams[$part[1]])) {
228 if (!$isOptional) {
229 throw new Exception\InvalidArgumentException(sprintf('Missing parameter "%s"', $part[1]));
232 return '';
233 } elseif (!$isOptional || !isset($this->defaults[$part[1]]) || $this->defaults[$part[1]] !== $mergedParams[$part[1]]) {
234 $skip = false;
237 $host .= $mergedParams[$part[1]];
239 $this->assembledParams[] = $part[1];
240 break;
242 case 'optional':
243 $skippable = true;
244 $optionalPart = $this->buildHost($part[1], $mergedParams, true);
246 if ($optionalPart !== '') {
247 $host .= $optionalPart;
248 $skip = false;
250 break;
254 if ($isOptional && $skippable && $skip) {
255 return '';
258 return $host;
262 * match(): defined by RouteInterface interface.
264 * @see \Zend\Mvc\Router\RouteInterface::match()
265 * @param Request $request
266 * @return RouteMatch|null
268 public function match(Request $request)
270 if (!method_exists($request, 'getUri')) {
271 return;
274 $uri = $request->getUri();
275 $host = $uri->getHost();
277 $result = preg_match('(^' . $this->regex . '$)', $host, $matches);
279 if (!$result) {
280 return;
283 $params = array();
285 foreach ($this->paramMap as $index => $name) {
286 if (isset($matches[$index]) && $matches[$index] !== '') {
287 $params[$name] = $matches[$index];
291 return new RouteMatch(array_merge($this->defaults, $params));
295 * assemble(): Defined by RouteInterface interface.
297 * @see \Zend\Mvc\Router\RouteInterface::assemble()
298 * @param array $params
299 * @param array $options
300 * @return mixed
302 public function assemble(array $params = array(), array $options = array())
304 $this->assembledParams = array();
306 if (isset($options['uri'])) {
307 $host = $this->buildHost(
308 $this->parts,
309 array_merge($this->defaults, $params),
310 false
313 $options['uri']->setHost($host);
316 // A hostname does not contribute to the path, thus nothing is returned.
317 return '';
321 * getAssembledParams(): defined by RouteInterface interface.
323 * @see RouteInterface::getAssembledParams
324 * @return array
326 public function getAssembledParams()
328 return $this->assembledParams;