OSDN Git Service

Merge pull request #92 from dannyvankooten/enhancement/add-docheader
[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     }
89
90     public function getUrl()
91     {
92         return $this->url;
93     }
94
95     public function setUrl($url)
96     {
97         $url = (string)$url;
98
99         // make sure that the URL is suffixed with a forward slash
100         if (substr($url, -1) !== '/') {
101             $url .= '/';
102         }
103
104         $this->url = $url;
105     }
106
107     public function getTarget()
108     {
109         return $this->target;
110     }
111
112     public function setTarget($target)
113     {
114         $this->target = $target;
115     }
116
117     public function getMethods()
118     {
119         return $this->methods;
120     }
121
122     public function setMethods(array $methods)
123     {
124         $this->methods = $methods;
125     }
126
127     public function getName()
128     {
129         return $this->name;
130     }
131
132     public function setName($name)
133     {
134         $this->name = (string)$name;
135     }
136
137     public function setFilters(array $filters, $parametersByName = false)
138     {
139         $this->filters          = $filters;
140         $this->parametersByName = $parametersByName;
141     }
142
143     public function getRegex()
144     {
145         return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
146     }
147
148     private function substituteFilter($matches)
149     {
150         if (isset($matches[1], $this->filters[$matches[1]])) {
151             return $this->filters[$matches[1]];
152         }
153
154         return '([\w-%]+)';
155     }
156
157     public function getParameters()
158     {
159         return $this->parameters;
160     }
161
162     public function setParameters(array $parameters)
163     {
164         $this->parameters = $parameters;
165     }
166
167     public function dispatch()
168     {
169         $action = explode('::', $this->config['_controller']);
170
171         if ($this->parametersByName) {
172             $this->parameters = array($this->parameters);
173         }
174
175         $this->action = !empty($action[1]) && trim($action[1]) !== '' ? $action[1] : null;
176
177         if (!is_null($this->action)) {
178             $instance = new $action[0];
179             call_user_func_array(array($instance, $this->action), $this->parameters);
180         } else {
181             $instance = new $action[0]($this->parameters);
182         }
183     }
184
185     public function getAction()
186     {
187         return $this->action;
188     }
189 }