OSDN Git Service

Merge config params with query params #101
[php-libraries/Router.git] / src / Route.php
1 <?php
2 /**
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.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license.
17  */
18 namespace PHPRouter;
19
20 use Fig\Http\Message\RequestMethodInterface;
21
22 class Route
23 {
24     /**
25      * URL of this Route
26      * @var string
27      */
28     private $url;
29
30     /**
31      * Accepted HTTP methods for this route.
32      * @var string[]
33      */
34     private $methods = array(
35         RequestMethodInterface::METHOD_GET,
36         RequestMethodInterface::METHOD_POST,
37         RequestMethodInterface::METHOD_PUT,
38         RequestMethodInterface::METHOD_DELETE,
39     );
40
41     /**
42      * Target for this route, can be anything.
43      * @var mixed
44      */
45     private $target;
46
47     /**
48      * The name of this route, used for reversed routing
49      * @var string
50      */
51     private $name;
52
53     /**
54      * Custom parameter filters for this route
55      * @var array
56      */
57     private $filters = array();
58
59     /**
60      * Array containing parameters passed through request URL
61      * @var array
62      */
63     private $parameters = array();
64
65     /**
66      * Set named parameters to target method
67      * @example [ [0] => [ ["link_id"] => "12312" ] ]
68      * @var bool
69      */
70     private $parametersByName;
71
72     /**
73      * @var array
74      */
75     private $config;
76
77     /**
78      * @param       $resource
79      * @param array $config
80      */
81     public function __construct($resource, array $config)
82     {
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();
89     }
90
91     public function getUrl()
92     {
93         return $this->url;
94     }
95
96     public function setUrl($url)
97     {
98         $url = (string)$url;
99
100         // make sure that the URL is suffixed with a forward slash
101         if (substr($url, -1) !== '/') {
102             $url .= '/';
103         }
104
105         $this->url = $url;
106     }
107
108     public function getTarget()
109     {
110         return $this->target;
111     }
112
113     public function setTarget($target)
114     {
115         $this->target = $target;
116     }
117
118     public function getMethods()
119     {
120         return $this->methods;
121     }
122
123     public function setMethods(array $methods)
124     {
125         $this->methods = $methods;
126     }
127
128     public function getName()
129     {
130         return $this->name;
131     }
132
133     public function setName($name)
134     {
135         $this->name = (string)$name;
136     }
137
138     public function setFilters(array $filters, $parametersByName = false)
139     {
140         $this->filters          = $filters;
141         $this->parametersByName = $parametersByName;
142     }
143
144     public function getRegex()
145     {
146         return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
147     }
148
149     private function substituteFilter($matches)
150     {
151         if (isset($matches[1], $this->filters[$matches[1]])) {
152             return $this->filters[$matches[1]];
153         }
154
155         return '([\w-%]+)';
156     }
157
158     public function getParameters()
159     {
160         return $this->parameters;
161     }
162
163     public function setParameters(array $parameters)
164     {
165         $this->parameters = array_merge($this->parameters, $parameters);
166     }
167
168     public function dispatch()
169     {
170         $action = explode('::', $this->config['_controller']);
171
172         if ($this->parametersByName) {
173             $this->parameters = array($this->parameters);
174         }
175
176         $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
177
178         if (!is_null($this->action)) {
179             $instance = new $action[0];
180             call_user_func_array(array($instance, $this->action), $this->parameters);
181         } else {
182             $instance = new $action[0]($this->parameters);
183         }
184     }
185
186     public function getAction()
187     {
188         return $this->action;
189     }
190 }