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
10 namespace Zend\Mvc\Router\Http
;
13 use Zend\Mvc\Router\Exception
;
14 use Zend\Stdlib\ArrayUtils
;
15 use Zend\Stdlib\RequestInterface
as Request
;
20 class Regex
implements RouteInterface
37 * Specification for URL assembly.
39 * Parameters accepting substitutions should be denoted as "%key%"
46 * List of assembled parameters.
50 protected $assembledParams = [];
53 * Create a new regex route.
55 * @param string $regex
57 * @param array $defaults
59 public function __construct($regex, $spec, array $defaults = [])
61 $this->regex
= $regex;
63 $this->defaults
= $defaults;
67 * factory(): defined by RouteInterface interface.
69 * @see \Zend\Mvc\Router\RouteInterface::factory()
70 * @param array|Traversable $options
72 * @throws \Zend\Mvc\Router\Exception\InvalidArgumentException
74 public static function factory($options = [])
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'] = [];
94 return new static($options['regex'], $options['spec'], $options['defaults']);
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')) {
110 $uri = $request->getUri();
111 $path = $uri->getPath();
113 if ($pathOffset !== null) {
114 $result = preg_match('(\G' . $this->regex
. ')', $path, $matches, null, $pathOffset);
116 $result = preg_match('(^' . $this->regex
. '$)', $path, $matches);
123 $matchedLength = strlen($matches[0]);
125 foreach ($matches as $key => $value) {
126 if (is_numeric($key) ||
is_int($key) ||
$value === '') {
127 unset($matches[$key]);
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
144 public function assemble(array $params = [], array $options = [])
147 $mergedParams = array_merge($this->defaults
, $params);
148 $this->assembledParams
= [];
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;
164 * getAssembledParams(): defined by RouteInterface interface.
166 * @see RouteInterface::getAssembledParams
169 public function getAssembledParams()
171 return $this->assembledParams
;