1 <?php
2 /**
3 * Route.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8
9 namespace Hawk;
10
11 /**
12 * This class describes the routes behavior
13 *
14 * @package Core\Router
15 */
16 class Route{
17 use Utils;
18
19 /**
20 * The route name
21 *
22 * @var string
23 */
24 private $name;
25
26 /**
27 * The route data, declared like '{dataname}' in the route definition
28 *
29 * @var array
30 */
31 private $data = array();
32
33 /**
34 * The necessary authentications to access the route
35 *
36 * @var array
37 */
38 private $auth = array();
39
40
41 /**
42 * The route URL
43 *
44 * @var string
45 */
46 public $url = '',
47
48 /**
49 * The route URL prefix
50 */
51 $prefix = '',
52
53 /**
54 * The action namespace
55 */
56 $namespace = '',
57
58 /**
59 * The pattern rules
60 *
61 * @var array
62 */
63 $where = array(),
64
65 /**
66 * The default values of the route parameters
67 *
68 * @param array
69 */
70 $default = array(),
71
72 /**
73 * The route action
74 *
75 * @param string
76 */
77 $action = '',
78
79 /**
80 * The route pattern
81 *
82 * @param string
83 */
84 $pattern = '';
85
86
87
88 /**
89 * Constructor
90 *
91 * @param string $name The route name
92 * @param string $url The route URI pattern
93 * @param array $param The route parameters, containing the pattern rules,
94 * the default values, the action associated with this route
95 */
96 public function __construct($name, $url, $param){
97 $this->name = $name;
98
99 $this->map($param);
100
101 $this->args = array();
102 $this->url = $this->prefix . $url;
103 $this->pattern = preg_replace_callback(
104 "/\{(\w+)\}/", function ($match) {
105 $this->args[] = $match[1];
106 $where = $this->where[$match[1]] ? $this->where[$match[1]] : '.*?';
107 return "(" . $where . ")";
108 }, $this->url
109 );
110
111
112 if($this->namespace) {
113 $this->action = $this->namespace . '\\' . $this->action;
114 }
115 }
116
117
118 /**
119 * Get the route name
120 *
121 * @return string the route name
122 */
123 public function getName(){
124 return $this->name;
125 }
126
127
128 /**
129 * Check if the route pattern match with a given URI, and if it matches, set the route data
130 *
131 * @param string $path The URI to check
132 *
133 * @return bool true if the URI match the route, else False
134 */
135 public function match($path){
136 if(preg_match('~^' . $this->pattern . '/?$~i', $path, $m)) {
137 // The URL match, let's test the filters to access this URL are OK
138 foreach(array_slice($m, 1) as $i => $var){
139 $this->setData($this->args[$i], $var);
140 }
141 return true;
142
143 }
144 return false;
145 }
146
147
148 /**
149 * Get the route data
150 *
151 * @param string $prop If set, the method will return the data value for this property.
152 * If not set, it will return the whole route data
153 *
154 * @return mixed If $prop is set, the data value for this property, else the whole route data
155 */
156 public function getData($prop = null){
157 if(!$prop) {
158 return $this->data;
159 }
160 else{
161 return $this->data[$prop];
162 }
163 }
164
165
166 /**
167 * Set the route data
168 *
169 * @param string $key The property name of the data to set
170 * @param mixed $value The value to set
171 */
172 public function setData($key, $value){
173 $this->data[$key] = $value;
174 }
175
176
177 /**
178 * Get the route action
179 *
180 * @return string The action associated with the route, formatted like : '<ControllerClass>.<method>'
181 */
182 public function getAction(){
183 return $this->action;
184 }
185
186
187 /**
188 * Get the route action controller class
189 */
190 public function getActionClassname(){
191 list($controller, $method) = explode('.', $this->action);
192
193 return $controller;
194 }
195
196 /**
197 * Get the route action method name
198 */
199 public function getActionMethodName(){
200 list($controller, $method) = explode('.', $this->action);
201
202 return $method;
203 }
204
205 /**
206 * Check of the route is accessible by the web client
207 *
208 * @return bool True if the route is accessible, False in other case
209 */
210 public function isAccessible(){
211 foreach($this->auth as $auth){
212 if(!$auth) {
213 return false;
214 }
215 }
216 return true;
217 }
218 }