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;
90 public function getUrl()
95 public function setUrl($url)
99 // make sure that the URL is suffixed with a forward slash
100 if (substr($url, -1) !== '/') {
107 public function getTarget()
109 return $this->target;
112 public function setTarget($target)
114 $this->target = $target;
117 public function getMethods()
119 return $this->methods;
122 public function setMethods(array $methods)
124 $this->methods = $methods;
127 public function getName()
132 public function setName($name)
134 $this->name = (string)$name;
137 public function setFilters(array $filters, $parametersByName = false)
139 $this->filters = $filters;
140 $this->parametersByName = $parametersByName;
143 public function getRegex()
145 return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
148 private function substituteFilter($matches)
150 if (isset($matches[1], $this->filters[$matches[1]])) {
151 return $this->filters[$matches[1]];
157 public function getParameters()
159 return $this->parameters;
162 public function setParameters(array $parameters)
164 $this->parameters = $parameters;
167 public function dispatch()
169 $action = explode('::', $this->config['_controller']);
171 if ($this->parametersByName) {
172 $this->parameters = array($this->parameters);
175 $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
177 if (!is_null($this->action)) {
178 $instance = new $action[0];
179 call_user_func_array(array($instance, $this->action), $this->parameters);
181 $instance = new $action[0]($this->parameters);
185 public function getAction()
187 return $this->action;