Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Mvc / Router / Http / Regex.php
blobbbbdec3518fbe932e79a64ff8cfce31b3ca3106e
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-2013 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 * Regex route.
20 class Regex implements RouteInterface
22 /**
23 * Regex to match.
25 * @var string
27 protected $regex;
29 /**
30 * Default values.
32 * @var array
34 protected $defaults;
36 /**
37 * Specification for URL assembly.
39 * Parameters accepting substitutions should be denoted as "%key%"
41 * @var string
43 protected $spec;
45 /**
46 * List of assembled parameters.
48 * @var array
50 protected $assembledParams = array();
52 /**
53 * Create a new regex route.
55 * @param string $regex
56 * @param string $spec
57 * @param array $defaults
59 public function __construct($regex, $spec, array $defaults = array())
61 $this->regex = $regex;
62 $this->spec = $spec;
63 $this->defaults = $defaults;
66 /**
67 * factory(): defined by RouteInterface interface.
69 * @see \Zend\Mvc\Router\RouteInterface::factory()
70 * @param array|Traversable $options
71 * @return Regex
72 * @throws \Zend\Mvc\Router\Exception\InvalidArgumentException
74 public static function factory($options = array())
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');
82 if (!isset($options['regex'])) {
83 throw new Exception\InvalidArgumentException('Missing "regex" in options array');
86 if (!isset($options['spec'])) {
87 throw new Exception\InvalidArgumentException('Missing "spec" in options array');
90 if (!isset($options['defaults'])) {
91 $options['defaults'] = array();
94 return new static($options['regex'], $options['spec'], $options['defaults']);
97 /**
98 * match(): defined by RouteInterface interface.
100 * @param Request $request
101 * @param int $pathOffset
102 * @return RouteMatch|null
104 public function match(Request $request, $pathOffset = null)
106 if (!method_exists($request, 'getUri')) {
107 return null;
110 $uri = $request->getUri();
111 $path = $uri->getPath();
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);
119 if (!$result) {
120 return null;
123 $matchedLength = strlen($matches[0]);
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);
133 return new RouteMatch(array_merge($this->defaults, $matches), $matchedLength);
137 * assemble(): Defined by RouteInterface interface.
139 * @see \Zend\Mvc\Router\RouteInterface::assemble()
140 * @param array $params
141 * @param array $options
142 * @return mixed
144 public function assemble(array $params = array(), array $options = array())
146 $url = $this->spec;
147 $mergedParams = array_merge($this->defaults, $params);
148 $this->assembledParams = array();
150 foreach ($mergedParams as $key => $value) {
151 $spec = '%' . $key . '%';
153 if (strpos($url, $spec) !== false) {
154 $url = str_replace($spec, rawurlencode($value), $url);
156 $this->assembledParams[] = $key;
160 return $url;
164 * getAssembledParams(): defined by RouteInterface interface.
166 * @see RouteInterface::getAssembledParams
167 * @return array
169 public function getAssembledParams()
171 return $this->assembledParams;