OSDN Git Service

fix subsequent align
[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 = null;
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'] : [];
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
134         if ($parametersByName) {
135             $this->parametersByName = true;
136         }
137     }
138
139     public function getRegex()
140     {
141         return preg_replace_callback("/(:\w+)/", array(&$this, 'substituteFilter'), $this->url);
142     }
143
144     private function substituteFilter($matches)
145     {
146         if (isset($matches[1]) && isset($this->filters[$matches[1]])) {
147             return $this->filters[$matches[1]];
148         }
149
150         return "([\w-%]+)";
151     }
152
153     public function getParameters()
154     {
155         return $this->parameters;
156     }
157
158     public function setParameters(array $parameters)
159     {
160         $this->parameters = $parameters;
161     }
162
163     public function dispatch()
164     {
165         $action = explode('::', $this->config['_controller']);
166         $instance = new $action[0];
167
168         if ($this->parametersByName) {
169             $this->parameters = array($this->parameters);
170         }
171
172         call_user_func_array(array($instance, $action[1]), $this->parameters);
173     }
174 }