OSDN Git Service

Small syntax fixes to avoid PHP notices and to pass tests
[php-libraries/Router.git] / src / PHPRouter / 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      *
31      * @var string[]
32      */
33     private $methods = array('GET', 'POST', 'PUT', 'DELETE');
34
35     /**
36      * Target for this route, can be anything.
37      * @var mixed
38      */
39     private $target;
40
41     /**
42      * The name of this route, used for reversed routing
43      * @var string
44      */
45     private $name;
46
47     /**
48      * Custom parameter filters for this route
49      * @var array
50      */
51     private $filters = array();
52
53     /**
54      * Array containing parameters passed through request URL
55      * @var array
56      */
57     private $parameters = array();
58
59     /**
60      * @var array
61      */
62     private $config;
63
64     /**
65      * @param       $resource
66      * @param array $config
67      */
68     public function __construct($resource, array $config)
69     {
70         $this->url     = $resource;
71         $this->config  = $config;
72         $this->methods = isset($config['methods']) ? $config['methods'] : array();
73         $this->target  = isset($config['target'])  ? $config['target']  : null;
74     }
75
76     public function getUrl()
77     {
78         return $this->url;
79     }
80
81     public function setUrl($url)
82     {
83         $url = (string)$url;
84
85         // make sure that the URL is suffixed with a forward slash
86         if (substr($url, -1) !== '/') {
87             $url .= '/';
88         }
89
90         $this->url = $url;
91     }
92
93     public function getTarget()
94     {
95         return $this->target;
96     }
97
98     public function setTarget($target)
99     {
100         $this->target = $target;
101     }
102
103     public function getMethods()
104     {
105         return $this->methods;
106     }
107
108     public function setMethods(array $methods)
109     {
110         $this->methods = $methods;
111     }
112
113     public function getName()
114     {
115         return $this->name;
116     }
117
118     public function setName($name)
119     {
120         $this->name = (string)$name;
121     }
122
123     public function setFilters(array $filters)
124     {
125         $this->filters = $filters;
126     }
127
128     public function getRegex()
129     {
130         return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->url);
131     }
132
133     private function substituteFilter($matches)
134     {
135         if (isset($matches[1]) && isset($this->filters[$matches[1]])) {
136             return $this->filters[$matches[1]];
137         }
138
139         return "([\w-%]+)";
140     }
141
142     public function getParameters()
143     {
144         return $this->parameters;
145     }
146
147     public function setParameters(array $parameters)
148     {
149         $this->parameters = $parameters;
150     }
151
152     public function dispatch()
153     {
154         $action = explode('::', $this->config['_controller']);
155         $instance = new $action[0];
156         call_user_func_array(array($instance, $action[1]), $this->parameters);
157     }
158 }