PageRenderTime 47ms CodeModel.GetById 41ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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