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
|
<?php /** * Klein (klein.php) - A fast & flexible router for PHP * * @author Chris O'Hara <cohara87@gmail.com> * @author Trevor Suarez (Rican7) (contributor and v2 refactorer) * @copyright (c) Chris O'Hara * @link https://github.com/klein/klein.php * @license MIT */
namespace Klein;
use InvalidArgumentException;
/** * Route * * Class to represent a route definition */ class Route {
/** * Properties */
/** * The callback method to execute when the route is matched * * Any valid "callable" type is allowed * * @link http://php.net/manual/en/language.types.callable.php * @type callable */ protected $callback;
/** * The URL path to match * * Allows for regular expression matching and/or basic string matching * * Examples: * - '/posts' * - '/posts/[:post_slug]' * - '/posts/[i:id]' * * @type string */ protected $path;
/** * The HTTP method to match * * May either be represented as a string or an array containing multiple methods to match * * Examples: * - 'POST' * - array('GET', 'POST') * * @type string|array */ protected $method;
/** * Whether or not to count this route as a match when counting total matches * * @type boolean */ protected $count_match;
/** * The name of the route * * Mostly used for reverse routing * * @type string */ protected $name;
/** * Methods */
/** * Constructor * * @param callable $callback * @param string $path * @param string|array $method * @param boolean $count_match */ public function __construct($callback, $path = null, $method = null, $count_match = true, $name = null) { // Initialize some properties (use our setters so we can validate param types) $this->setCallback($callback); $this->setPath($path); $this->setMethod($method); $this->setCountMatch($count_match); $this->setName($name); }
/** * Get the callback * * @return callable */ public function getCallback() { return $this->callback; }
/** * Set the callback * * @param callable $callback * @throws InvalidArgumentException If the callback isn't a callable * @return Route */ public function setCallback($callback) { if (!is_callable($callback)) { throw new InvalidArgumentException('Expected a callable. Got an uncallable '. gettype($callback)); }
$this->callback = $callback;
return $this; }
/** * Get the path * * @return string */ public function getPath() { return $this->path; }
/** * Set the path * * @param string $path * @return Route */ public function setPath($path) { $this->path = (string) $path;
return $this; }
/** * Get the method * * @return string|array */ public function getMethod() { return $this->method; }
/** * Set the method * * @param string|array|null $method * @throws InvalidArgumentException If a non-string or non-array type is passed * @return Route */ public function setMethod($method) { // Allow null, otherwise expect an array or a string if (null !== $method && !is_array($method) && !is_string($method)) { throw new InvalidArgumentException('Expected an array or string. Got a '. gettype($method)); }
$this->method = $method;
return $this; }
/** * Get the count_match * * @return boolean */ public function getCountMatch() { return $this->count_match; }
/** * Set the count_match * * @param boolean $count_match * @return Route */ public function setCountMatch($count_match) { $this->count_match = (boolean) $count_match;
return $this; }
/** * Get the name * * @return string */ public function getName() { return $this->name; }
/** * Set the name * * @param string $name * @return Route */ public function setName($name) { if (null !== $name) { $this->name = (string) $name; } else { $this->name = $name; }
return $this; }
/** * Magic "__invoke" method * * Allows the ability to arbitrarily call this instance like a function * * @param mixed $args Generic arguments, magically accepted * @return mixed */ public function __invoke($args = null) { $args = func_get_args();
return call_user_func_array( $this->callback, $args ); } }
|