3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license.
20 use Fig\Http\Message\RequestMethodInterface;
31 * Accepted HTTP methods for this route.
34 private $methods = array(
35 RequestMethodInterface::METHOD_GET,
36 RequestMethodInterface::METHOD_POST,
37 RequestMethodInterface::METHOD_PUT,
38 RequestMethodInterface::METHOD_DELETE,
42 * Target for this route, can be anything.
48 * The name of this route, used for reversed routing
54 * Custom parameter filters for this route
57 private $filters = array();
60 * Array containing parameters passed through request URL
63 private $parameters = array();
66 * Set named parameters to target method
67 * @example [ [0] => [ ["link_id"] => "12312" ] ]
70 private $parametersByName;
79 * @param array $config
81 public function __construct($resource, array $config)
83 $this->url = $resource;
84 $this->config = $config;
85 $this->methods = isset($config['methods']) ? (array) $config['methods'] : array();
86 $this->target = isset($config['target']) ? $config['target'] : null;
87 $this->name = isset($config['name']) ? $config['name'] : null;
88 $this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
91 public function getUrl()
96 public function setUrl($url)
100 // make sure that the URL is suffixed with a forward slash
101 if (substr($url, -1) !== '/') {
108 public function getTarget()
110 return $this->target;
113 public function setTarget($target)
115 $this->target = $target;
118 public function getMethods()
120 return $this->methods;
123 public function setMethods(array $methods)
125 $this->methods = $methods;
128 public function getName()
133 public function setName($name)
135 $this->name = (string)$name;
138 public function setFilters(array $filters, $parametersByName = false)
140 $this->filters = $filters;
141 $this->parametersByName = $parametersByName;
144 public function getRegex()
146 return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
149 private function substituteFilter($matches)
151 if (isset($matches[1], $this->filters[$matches[1]])) {
152 return $this->filters[$matches[1]];
158 public function getParameters()
160 return $this->parameters;
163 public function setParameters(array $parameters)
165 $this->parameters = array_merge($this->parameters, $parameters);
168 public function dispatch()
170 $action = explode('::', $this->config['_controller']);
172 if ($this->parametersByName) {
173 $this->parameters = array($this->parameters);
176 $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
178 if (!is_null($this->action)) {
179 $instance = new $action[0];
180 call_user_func_array(array($instance, $this->action), $this->parameters);
182 $instance = new $action[0]($this->parameters);
186 public function getAction()
188 return $this->action;