/vendor/zendframework/zend-mvc/src/Router/Http/Regex.php

https://gitlab.com/yousafsyed/easternglamor · PHP · 173 lines · 80 code · 25 blank · 68 comment · 13 complexity · c4740e8deb6806eb357f4590857caea3 MD5 · raw file

  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. namespace Zend\Mvc\Router\Http;
  10. use Traversable;
  11. use Zend\Mvc\Router\Exception;
  12. use Zend\Stdlib\ArrayUtils;
  13. use Zend\Stdlib\RequestInterface as Request;
  14. /**
  15. * Regex route.
  16. */
  17. class Regex implements RouteInterface
  18. {
  19. /**
  20. * Regex to match.
  21. *
  22. * @var string
  23. */
  24. protected $regex;
  25. /**
  26. * Default values.
  27. *
  28. * @var array
  29. */
  30. protected $defaults;
  31. /**
  32. * Specification for URL assembly.
  33. *
  34. * Parameters accepting substitutions should be denoted as "%key%"
  35. *
  36. * @var string
  37. */
  38. protected $spec;
  39. /**
  40. * List of assembled parameters.
  41. *
  42. * @var array
  43. */
  44. protected $assembledParams = array();
  45. /**
  46. * Create a new regex route.
  47. *
  48. * @param string $regex
  49. * @param string $spec
  50. * @param array $defaults
  51. */
  52. public function __construct($regex, $spec, array $defaults = array())
  53. {
  54. $this->regex = $regex;
  55. $this->spec = $spec;
  56. $this->defaults = $defaults;
  57. }
  58. /**
  59. * factory(): defined by RouteInterface interface.
  60. *
  61. * @see \Zend\Mvc\Router\RouteInterface::factory()
  62. * @param array|Traversable $options
  63. * @return Regex
  64. * @throws \Zend\Mvc\Router\Exception\InvalidArgumentException
  65. */
  66. public static function factory($options = array())
  67. {
  68. if ($options instanceof Traversable) {
  69. $options = ArrayUtils::iteratorToArray($options);
  70. } elseif (!is_array($options)) {
  71. throw new Exception\InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
  72. }
  73. if (!isset($options['regex'])) {
  74. throw new Exception\InvalidArgumentException('Missing "regex" in options array');
  75. }
  76. if (!isset($options['spec'])) {
  77. throw new Exception\InvalidArgumentException('Missing "spec" in options array');
  78. }
  79. if (!isset($options['defaults'])) {
  80. $options['defaults'] = array();
  81. }
  82. return new static($options['regex'], $options['spec'], $options['defaults']);
  83. }
  84. /**
  85. * match(): defined by RouteInterface interface.
  86. *
  87. * @param Request $request
  88. * @param int $pathOffset
  89. * @return RouteMatch|null
  90. */
  91. public function match(Request $request, $pathOffset = null)
  92. {
  93. if (!method_exists($request, 'getUri')) {
  94. return;
  95. }
  96. $uri = $request->getUri();
  97. $path = $uri->getPath();
  98. if ($pathOffset !== null) {
  99. $result = preg_match('(\G' . $this->regex . ')', $path, $matches, null, $pathOffset);
  100. } else {
  101. $result = preg_match('(^' . $this->regex . '$)', $path, $matches);
  102. }
  103. if (!$result) {
  104. return;
  105. }
  106. $matchedLength = strlen($matches[0]);
  107. foreach ($matches as $key => $value) {
  108. if (is_numeric($key) || is_int($key) || $value === '') {
  109. unset($matches[$key]);
  110. } else {
  111. $matches[$key] = rawurldecode($value);
  112. }
  113. }
  114. return new RouteMatch(array_merge($this->defaults, $matches), $matchedLength);
  115. }
  116. /**
  117. * assemble(): Defined by RouteInterface interface.
  118. *
  119. * @see \Zend\Mvc\Router\RouteInterface::assemble()
  120. * @param array $params
  121. * @param array $options
  122. * @return mixed
  123. */
  124. public function assemble(array $params = array(), array $options = array())
  125. {
  126. $url = $this->spec;
  127. $mergedParams = array_merge($this->defaults, $params);
  128. $this->assembledParams = array();
  129. foreach ($mergedParams as $key => $value) {
  130. $spec = '%' . $key . '%';
  131. if (strpos($url, $spec) !== false) {
  132. $url = str_replace($spec, rawurlencode($value), $url);
  133. $this->assembledParams[] = $key;
  134. }
  135. }
  136. return $url;
  137. }
  138. /**
  139. * getAssembledParams(): defined by RouteInterface interface.
  140. *
  141. * @see RouteInterface::getAssembledParams
  142. * @return array
  143. */
  144. public function getAssembledParams()
  145. {
  146. return $this->assembledParams;
  147. }
  148. }