OSDN Git Service

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