1 | <?php |
||
22 | class Router implements \IteratorAggregate |
||
23 | { |
||
24 | /** @var string The base path. */ |
||
25 | private $basePath; |
||
26 | |||
27 | /** @var array The routes. */ |
||
28 | private $routes; |
||
29 | |||
30 | /** |
||
31 | * Construct a Router object with the given routers. |
||
32 | * |
||
33 | * @param string $basePath = '' |
||
34 | */ |
||
35 | 19 | public function __construct($basePath = '') |
|
40 | |||
41 | /** |
||
42 | * Returns the base path. |
||
43 | * |
||
44 | * @return string the base path. |
||
45 | */ |
||
46 | 2 | public function getBasePath() |
|
50 | |||
51 | /** |
||
52 | * Set the base path. |
||
53 | * |
||
54 | * @param string $basePath |
||
55 | * @return $this |
||
56 | */ |
||
57 | 2 | public function setBasePath($basePath) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 1 | public function getIterator() |
|
71 | |||
72 | /** |
||
73 | * Returns the number of key-value mappings in the router map. |
||
74 | * |
||
75 | * @return int the number of key-value mappings in the router map. |
||
76 | */ |
||
77 | 1 | public function count() |
|
87 | |||
88 | /** |
||
89 | * Returns true if the router map contains no key-value mappings. |
||
90 | * |
||
91 | * @return bool true if the router map contains no key-value mappings. |
||
92 | */ |
||
93 | 2 | public function isEmpty() |
|
97 | |||
98 | /** |
||
99 | * Returns true if the router map contains a mapping for the specified method & route. |
||
100 | * |
||
101 | * @param string $method |
||
102 | * @param string $route |
||
103 | * @return bool true if the static route map contains a mapping for the specified method & route. |
||
104 | */ |
||
105 | 2 | public function contains($method, $route) |
|
109 | |||
110 | /** |
||
111 | * Returns true if the router map maps one or more routes to the specified callable. |
||
112 | * |
||
113 | * @param callable $callable |
||
114 | * @return bool true if the router map maps one or more routes to the specified callable. |
||
115 | */ |
||
116 | 1 | public function containsCallable(callable $callable) |
|
129 | |||
130 | /** |
||
131 | * Returns the callable to which the specified route is mapped, or null if the router map contains no mapping for the route. |
||
132 | * |
||
133 | * @param string $method |
||
134 | * @param string $route |
||
135 | * @return callable the callable to which the specified route is mapped, or null if the router map contains no mapping for the route. |
||
136 | */ |
||
137 | 4 | public function get($method, $route) |
|
151 | |||
152 | /** |
||
153 | * Associates the specified callable with the specified method & route in the route map. |
||
154 | * |
||
155 | * @param string|array $methods |
||
156 | * @param string $route |
||
157 | * @param callable $callable |
||
158 | * @return $this |
||
159 | */ |
||
160 | 19 | public function set($methods, $route, callable $callable) |
|
172 | |||
173 | /** |
||
174 | * Adds a method & route to the to the route map. |
||
175 | * |
||
176 | * @param string $method |
||
177 | * @param string $route |
||
178 | * @param callable $callable |
||
179 | */ |
||
180 | 19 | private function add(string $method, string $route, callable $callable) |
|
194 | |||
195 | /** |
||
196 | * Creates a regex-enabled pattern from the route |
||
197 | * |
||
198 | * @param string $route |
||
199 | * @return string the pattern string. |
||
200 | */ |
||
201 | 19 | private function createPattern(string $route) |
|
205 | |||
206 | /** |
||
207 | * Removes the mapping for the specified route from the router map if present. |
||
208 | * |
||
209 | * @param string $method |
||
210 | * @param string $route |
||
211 | * @return null |
||
212 | */ |
||
213 | 3 | public function remove($method, $route) |
|
229 | |||
230 | /** |
||
231 | * Removes all of the mappings from the router map. |
||
232 | * |
||
233 | * @return null |
||
234 | */ |
||
235 | 1 | public function clear() |
|
239 | |||
240 | /** |
||
241 | * Returns the result of the given route's callable. |
||
242 | * |
||
243 | * @param string|null $method = new ServerRequest()->getMethod() |
||
244 | * @param string|null $route = new ServerRequest()->getUri()->getPath() |
||
245 | * @return mixed the result of the given route's callable. |
||
246 | * @throws ServerResponseException |
||
247 | */ |
||
248 | 8 | public function resolve($method = null, $route = null) |
|
266 | |||
267 | /** |
||
268 | * Returns the callable to which the specied method and route are mapped. |
||
269 | * |
||
270 | * @param string $method |
||
271 | * @param string $route |
||
272 | * @return array the callable to which the specied method and route are mapped and the route matches. |
||
273 | * @throws ServerResponseException |
||
274 | */ |
||
275 | 8 | private function getCallable($method, $route) |
|
290 | |||
291 | /** |
||
292 | * Returns the result of the callable. |
||
293 | * |
||
294 | * @param array the callable and the route matches. |
||
295 | * @return mixed the result the callable. |
||
296 | */ |
||
297 | 6 | private function callCallable($callable) |
|
301 | } |
||
302 |