1
|
|
|
<?php |
2
|
|
|
namespace Mezon\Router; |
3
|
|
|
|
4
|
|
|
class RoutesSet |
5
|
|
|
{ |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Mapping of routes to their execution functions for GET requests |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private $getRoutes = []; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Mapping of routes to their execution functions for GET requests |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $postRoutes = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Mapping of routes to their execution functions for PUT requests |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $putRoutes = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Mapping of routes to their execution functions for DELETE requests |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $deleteRoutes = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Method returns list of routes for the HTTP method. |
37
|
|
|
* |
38
|
|
|
* @param string $method |
39
|
|
|
* HTTP Method |
40
|
|
|
* @return array Routes |
41
|
|
|
*/ |
42
|
|
|
public function &getRoutesForMethod(string $method): array |
43
|
|
|
{ |
44
|
|
|
switch ($method) { |
45
|
|
|
case ('GET'): |
46
|
|
|
$result = &$this->getRoutes; |
47
|
|
|
break; |
48
|
|
|
|
49
|
|
|
case ('POST'): |
50
|
|
|
$result = &$this->postRoutes; |
51
|
|
|
break; |
52
|
|
|
|
53
|
|
|
case ('PUT'): |
54
|
|
|
$result = &$this->putRoutes; |
55
|
|
|
break; |
56
|
|
|
|
57
|
|
|
case ('DELETE'): |
58
|
|
|
$result = &$this->deleteRoutes; |
59
|
|
|
break; |
60
|
|
|
|
61
|
|
|
default: |
62
|
|
|
throw (new \Exception('Unsupported request method')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Method clears router data. |
70
|
|
|
*/ |
71
|
|
|
public function clear() |
72
|
|
|
{ |
73
|
|
|
$this->getRoutes = []; |
74
|
|
|
|
75
|
|
|
$this->postRoutes = []; |
76
|
|
|
|
77
|
|
|
$this->putRoutes = []; |
78
|
|
|
|
79
|
|
|
$this->deleteRoutes = []; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Method returns true if the router exists |
84
|
|
|
* |
85
|
|
|
* @param string $route |
86
|
|
|
* checking route |
87
|
|
|
* @return bool true if the router exists, false otherwise |
88
|
|
|
*/ |
89
|
|
|
public function routeExists(string $route): bool |
90
|
|
|
{ |
91
|
|
|
$allRoutes = array_merge($this->deleteRoutes, $this->putRoutes, $this->postRoutes, $this->getRoutes); |
92
|
|
|
|
93
|
|
|
return isset($allRoutes[$route]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Method rturns all available routes |
98
|
|
|
*/ |
99
|
|
|
public function getAllRoutesTrace() |
100
|
|
|
{ |
101
|
|
|
return (count($this->getRoutes) ? 'GET:' . implode(', ', array_keys($this->getRoutes)) . '; ' : '') . |
102
|
|
|
(count($this->postRoutes) ? 'POST:' . implode(', ', array_keys($this->postRoutes)) . '; ' : '') . |
103
|
|
|
(count($this->putRoutes) ? 'PUT:' . implode(', ', array_keys($this->putRoutes)) . '; ' : '') . |
104
|
|
|
(count($this->deleteRoutes) ? 'DELETE:' . implode(', ', array_keys($this->deleteRoutes)) : ''); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Additing route for GET request |
109
|
|
|
* |
110
|
|
|
* @param string $route route |
111
|
|
|
* @param object $object callback object |
112
|
|
|
* @param string $method callback method |
113
|
|
|
*/ |
114
|
|
|
public function addGetRoute(string $route, object $object, string $method):void{ |
115
|
|
|
$this->getRoutes["/$route/"] = [ |
116
|
|
|
$object, |
117
|
|
|
$method |
118
|
|
|
]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Additing route for GET request |
123
|
|
|
* |
124
|
|
|
* @param string $route route |
125
|
|
|
* @param object $object callback object |
126
|
|
|
* @param string $method callback method |
127
|
|
|
*/ |
128
|
|
|
public function addPostRoute(string $route, object $object, string $method):void{ |
129
|
|
|
$this->postRoutes["/$route/"] = [ |
130
|
|
|
$object, |
131
|
|
|
$method |
132
|
|
|
]; |
133
|
|
|
} |
134
|
|
|
} |