forked from dannyvankooten/PHP-Router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoute.php
333 lines (304 loc) · 11 KB
/
Route.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
* Returns true if all the keys in the array are integers.
*
* @param array $array
* @return boolean
*/
function is_indexed(array &$array) {
foreach (array_keys($array) as $key) {
if (!is_int($key))
return false;
}
return true;
}
/**
* Route class.
*
* Represents a route.
*
* @package AS-Router
* @license MIT
*/
class Route {
/**
* URL of this Route
* @var string
*/
private $url;
/**
* Regex of this Route.
* @var string
*/
private $regex;
/**
* Accepted HTTP methods for this route
* @var array
*/
private $methods = array('GET', 'POST');
/**
* Target for this route, can be anything.
* @var array
*/
private $target = array('controller' => null, 'action' => null);
/**
* Custom parameter filters for this route
* @var array
*/
private $filters = array();
/**
* Array containing parameters passed through request URL.
* @var array
*/
private $parameters = array();
/**
* Returns the route's URL.
* @return string
*/
public function getUrl() {
return $this->url;
}
/**
* Returns the route's full URL.
* @return string
*/
public function getRequestUrl() {
return $this->parameters['requestURL'];
}
/**
* Sets the route's URL.
* @param string $url
*/
public function setUrl($url) {
// make sure that the URL is suffixed with a forward slash
if ('/' != substr($url, -1))
$url .= '/';
$this->url = $url;
}
/**
* Returns the route's target.
* @return mixed
*/
public function getTarget() {
return $this->target;
}
/**
* Sets the route's target.
* @param array $target
*/
public function setTarget(array $target) {
if (empty($target)) {
return;
} elseif (is_indexed($target) && 2 == count($target)) {
$this->target['controller'] = $target[0];
$this->target['action'] = $target[1];
} elseif (is_indexed($target) && 1 == count($target)) {
// We assume that only the controller is given, the action is in the url
$this->target['controller'] = $target[0];
} elseif (false == is_indexed($target)) {
if (isset($target['controller']))
$this->target['controller'] = $target['controller'];
if (isset($target['action']))
$this->target['action'] = $target['action'];
}
}
/**
* MatcheTarget method.
*
* Determines if the route matches the given target and parameter.
*
* @param type $target
* @param array $params
* @return boolean
*/
public function match($target, array $params = array()) {
$internalParamsCount = substr_count($this->getUrl(), ':');
$internalPresetCount = count($this->getParameters());
if (isset($this->target['controller']) && $this->target['controller'] == $target['controller']) {
// Route's controller defined and matches
if (isset($this->target['action']) && $this->target['action'] == $target['action']) {
// Route's action defined and matches
if ($internalParamsCount == count($params)) {
// Route's parameters are good number
if ($this->parameters == $params) {
// Parameters pre-set
return true;
} else {
// Parameters are injected
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (false === strpos($this->getUrl(), ':' . $key))
return false;
}
return true;
}
} elseif (($internalParamsCount + $internalPresetCount) == count($params)) {
// We have a mix of injected and pre-set
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (false === strpos($this->getUrl(), ':' . $key) && !isset($this->parameters[$key]))
return false;
}
return true;
}
} elseif (!isset($this->target['action']) || null === $this->target['action']) {
// Route's action is injected
if (($internalParamsCount - 1) == count($params)) {
// Route's parameters are good number (action injected)
if ($this->parameters == $params) {
// Parameters pre-set
return true;
} else {
// Parameters are injected
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (false === strpos($this->getUrl(), ':' . $key))
return false;
}
return true;
}
} elseif (($internalParamsCount + $internalPresetCount - 1) == count($params)) {
// We have a mix of injected and pre-set
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (false === strpos($this->getUrl(), ':' . $key) && !isset($this->parameters[$key]))
return false;
}
return true;
}
}
} elseif (!isset($this->target['controller']) || null === $this->target['controller']) {
// Route's controller is injected
if (isset($this->target['action']) && $this->target['action'] == $target['action']) {
// Route's action defined and matches
if (($internalParamsCount - 1) == count($params)) {
// Route's parameters are good number (controller injected)
if ($this->parameters == $params) {
// Parameters pre-set
return true;
} else {
// Parameters are injected
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (strpos($this->getUrl(), ':' . $key) === false)
return false;
}
return true;
}
} elseif (($internalParamsCount + $internalPresetCount - 1) == count($params)) {
// We have a mix of injected and pre-set
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (false === strpos($this->getUrl(), ':' . $key) && !isset($this->parameters[$key]))
return false;
}
return true;
}
} elseif (!isset($this->target['action']) || null === $this->target['action']) {
// Route's action is injected
if (($internalParamsCount - 2) == count($params)) {
// Route's parameters are good number (controller + action injected)
if ($this->parameters == $params) {
// Parameters pre-set
return true;
} else {
// Parameters are injected
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (strpos($this->getUrl(), ':' . $key) === false)
return false;
}
return true;
}
} elseif (($internalParamsCount + $internalPresetCount - 2) == count($params)) {
// We have a mix of injected and pre-set
foreach (array_keys($params) as $key) {
// Making sure that the right parameters have been passed.
if (false === strpos($this->getUrl(), ':' . $key) && !isset($this->parameters[$key]))
return false;
}
return true;
}
}
}
return false;
}
/**
* Returns the route's HTTP methods.
* @return array
*/
public function getMethods() {
return $this->methods;
}
/**
* Sets the route's HTTP methods.
* @param array $methods
*/
public function setMethods(array $methods) {
$this->methods = $methods;
}
/**
* Sets the route's filters.
* @param array $filters
*/
public function setFilters(array $filters) {
$this->filters = $filters;
}
/**
* Returns the route's regex.
* @return string
*/
public function getRegex() {
if (empty($this->regex))
$this->regex = preg_replace_callback("@:(\w+)@", array(&$this, 'substituteFilter'), $this->url);
return $this->regex;
}
/**
* Returns the regex associated with a parameter.
*
* If no regex are assigned and it doesn't match the name of a
* pre-defined regex, it will match by default any alpha-numeric or dash.
*
* There are a few pre-defined regexes:
* * id -> <code>([\d]+)</code>
* * year -> <code>([12][\d]{3})</code>
* * shortyear -> <code>([\d]{2})</code>
* * month -> <code>(0?[1-9]|1[012])</code>
* * day -> <code>(0?[1-9]|[12][\d]|3[01])</code>
*
* @param array $matches
* @return string
*/
private function substituteFilter($matches) {
if (isset($this->filters[$matches[1]]))
return $this->filters[$matches[1]];
switch ($matches[1]) {
case 'id':
return '([\d]+)';
case 'year':
return '([12][\d]{3})';
case 'shortyear':
return '([\d]{2})';
case 'month':
return '(0?[1-9]|1[012])';
case 'day':
return '(0?[1-9]|[12][\d]|3[01])';
default:
return '([\d\w_-]+)';
}
}
/**
* Returns the route's parameters.
* @return array
*/
public function getParameters() {
return $this->parameters;
}
/**
* Sets the route's parameters.
* @param array $parameters
*/
public function setParameters(array $parameters) {
$this->parameters = $parameters;
}
}