/administrator/components/com_widgetkit/vendor/yootheme/framework/src/Routing/Route.php

https://gitlab.com/vnsoftdev/amms · PHP · 152 lines · 62 code · 21 blank · 69 comment · 2 complexity · eb95d9b8f7a657f851ccb241dc0cfc57 MD5 · raw file

  1. <?php
  2. namespace YOOtheme\Framework\Routing;
  3. class Route
  4. {
  5. /**
  6. * @var string
  7. */
  8. protected $pattern;
  9. /**
  10. * @var mixed
  11. */
  12. protected $callable;
  13. /**
  14. * @var array
  15. */
  16. protected $methods = array();
  17. /**
  18. * @var array
  19. */
  20. protected $params = array();
  21. /**
  22. * @var array
  23. */
  24. protected $options = array();
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $pattern
  29. * @param mixed $callable
  30. */
  31. public function __construct($pattern, $callable)
  32. {
  33. $this->pattern = $pattern;
  34. $this->callable = $callable;
  35. }
  36. /**
  37. * Gets the route pattern.
  38. *
  39. * @return string
  40. */
  41. public function getPattern()
  42. {
  43. return $this->pattern;
  44. }
  45. /**
  46. * Gets the route callable.
  47. *
  48. * @return mixed
  49. */
  50. public function getCallable()
  51. {
  52. return $this->callable;
  53. }
  54. /**
  55. * Gets the supported HTTP methods.
  56. *
  57. * @return self
  58. */
  59. public function getMethods()
  60. {
  61. return $this->methods;
  62. }
  63. /**
  64. * Sets the supported HTTP methods.
  65. *
  66. * @param string|string[] $method
  67. * @return self
  68. */
  69. public function setMethods($method)
  70. {
  71. $this->methods = array_merge($this->methods, (array) $method);
  72. return $this;
  73. }
  74. /**
  75. * Gets the route parameters.
  76. *
  77. * @return array
  78. */
  79. public function getParams()
  80. {
  81. return $this->params;
  82. }
  83. /**
  84. * Returns the options.
  85. *
  86. * @return array
  87. */
  88. public function getOptions()
  89. {
  90. return $this->options;
  91. }
  92. /**
  93. * Sets the options.
  94. *
  95. * @param array $options
  96. */
  97. public function setOptions(array $options)
  98. {
  99. $this->options = $options;
  100. }
  101. /**
  102. * Matches the given URL?
  103. *
  104. * @param string $url
  105. * @return bool
  106. */
  107. public function matches($url)
  108. {
  109. $regex = '#^' . preg_replace_callback('#:([\w]+)#', array($this, 'matchesCallback'), str_replace(')', ')?', $this->pattern)) . '$#';
  110. if (!preg_match($regex, $url, $values)) {
  111. return false;
  112. }
  113. foreach ($this->params as $name => $null) {
  114. if (isset($values[$name])) {
  115. $this->params[$name] = urldecode($values[$name]);
  116. }
  117. }
  118. return true;
  119. }
  120. /**
  121. * Convert a URL parameter to regex.
  122. *
  123. * @param array $matches
  124. * @return string
  125. */
  126. protected function matchesCallback($matches)
  127. {
  128. $this->params[$matches[1]] = null;
  129. return '(?P<' . $matches[1] . '>[^/]+)';
  130. }
  131. }