1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Router; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Route |
10
|
|
|
* @package DrMVC |
11
|
|
|
*/ |
12
|
|
|
class Route implements RouteInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $_variables = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $_regexp; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var mixed |
26
|
|
|
*/ |
27
|
|
|
private $_callback; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ServerRequestInterface |
31
|
|
|
*/ |
32
|
|
|
private $_request; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ResponseInterface |
36
|
|
|
*/ |
37
|
|
|
private $_response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $_method; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Route constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $method Method of received query |
48
|
|
|
* @param string $regexp Regular expression |
49
|
|
|
* @param callable|string $callable Class name or callback |
50
|
|
|
* @param ServerRequestInterface $request PSR-7 request |
51
|
|
|
* @param ResponseInterface $response RSP-7 response |
52
|
|
|
*/ |
53
|
|
|
public function __construct( |
54
|
|
|
string $method, |
55
|
|
|
string $regexp, |
56
|
|
|
$callable, |
57
|
|
|
ServerRequestInterface $request = null, |
58
|
|
|
ResponseInterface $response = null |
59
|
|
|
) { |
60
|
|
|
$this |
61
|
|
|
->setRoute($method, $regexp, $callable) |
62
|
|
|
->setRequest($request) |
63
|
|
|
->setResponse($response); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set current method of object |
68
|
|
|
* |
69
|
|
|
* @param string $method |
70
|
|
|
* @return RouteInterface |
71
|
|
|
*/ |
72
|
|
|
public function setMethod(string $method): RouteInterface |
73
|
|
|
{ |
74
|
|
|
$this->_method = $method; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get method of current object |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getMethod(): string |
84
|
|
|
{ |
85
|
|
|
return $this->_method; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set variables of current class |
90
|
|
|
* |
91
|
|
|
* @param array $variables |
92
|
|
|
* @return RouteInterface |
93
|
|
|
*/ |
94
|
|
|
public function setVariables(array $variables): RouteInterface |
95
|
|
|
{ |
96
|
|
|
$this->_variables = $variables; |
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return array of available variables |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getVariables(): array |
106
|
|
|
{ |
107
|
|
|
return $this->_variables; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set single route |
112
|
|
|
* |
113
|
|
|
* @param string $method Method of received query |
114
|
|
|
* @param string $regexp Regular expression |
115
|
|
|
* @param callable|string $callable Class name or callback |
116
|
|
|
* @return RouteInterface |
117
|
|
|
*/ |
118
|
|
|
public function setRoute( |
119
|
|
|
string $method, |
120
|
|
|
string $regexp, |
121
|
|
|
$callable |
122
|
|
|
): RouteInterface { |
123
|
|
|
return $this |
124
|
|
|
->setMethod($method) |
125
|
|
|
->setRegexp($regexp) |
126
|
|
|
->setCallback($callable); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set PSR-7 request |
131
|
|
|
* |
132
|
|
|
* @param ServerRequestInterface $request - PSR-7 request |
133
|
|
|
* @return RouteInterface |
134
|
|
|
*/ |
135
|
|
|
public function setRequest(ServerRequestInterface $request = null): RouteInterface |
136
|
|
|
{ |
137
|
|
|
if (null !== $request) { |
138
|
|
|
$this->_request = $request; |
139
|
|
|
} |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get PSR request |
145
|
|
|
* |
146
|
|
|
* @return ServerRequestInterface|null |
147
|
|
|
*/ |
148
|
|
|
public function getRequest() |
149
|
|
|
{ |
150
|
|
|
return $this->_request; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set RSR response |
155
|
|
|
* |
156
|
|
|
* @param mixed $response |
157
|
|
|
* @return RouteInterface |
158
|
|
|
*/ |
159
|
|
|
public function setResponse(ResponseInterface $response = null): RouteInterface |
160
|
|
|
{ |
161
|
|
|
if (!empty($response)) { |
162
|
|
|
$this->_response = $response; |
163
|
|
|
} |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Get RSR-7 response |
169
|
|
|
* |
170
|
|
|
* @return ResponseInterface|null |
171
|
|
|
*/ |
172
|
|
|
public function getResponse() |
173
|
|
|
{ |
174
|
|
|
return $this->_response; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Set callable element or class |
179
|
|
|
* |
180
|
|
|
* @param mixed $callback |
181
|
|
|
* @return RouteInterface |
182
|
|
|
*/ |
183
|
|
|
public function setCallback($callback): RouteInterface |
184
|
|
|
{ |
185
|
|
|
$this->_callback = $callback; |
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Return callable element |
191
|
|
|
* |
192
|
|
|
* @return callable|string |
193
|
|
|
*/ |
194
|
|
|
public function getCallback() |
195
|
|
|
{ |
196
|
|
|
return $this->_callback; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set regexp of current route |
201
|
|
|
* |
202
|
|
|
* @param string $regexp |
203
|
|
|
* @return RouteInterface |
204
|
|
|
*/ |
205
|
|
|
public function setRegexp(string $regexp): RouteInterface |
206
|
|
|
{ |
207
|
|
|
$pattern = ['/</', '/>/']; |
208
|
|
|
$replace = ['(?P<', '>.+)']; |
209
|
|
|
$regexp = preg_replace($pattern, $replace, $regexp); |
210
|
|
|
$this->_regexp = '#^' . $regexp . '$#u'; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Return regexp of current route |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getRegexp(): string |
220
|
|
|
{ |
221
|
|
|
return $this->_regexp; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|