1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DrMVC\Router; |
4
|
|
|
|
5
|
|
|
use Zend\Diactoros\ServerRequest; |
6
|
|
|
use Zend\Diactoros\Response; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Route |
10
|
|
|
* @package DrMVC |
11
|
|
|
*/ |
12
|
|
|
class Route implements Interfaces\Route |
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 \Zend\Diactoros\ServerRequest |
31
|
|
|
*/ |
32
|
|
|
private $_request; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \Zend\Diactoros\Response |
36
|
|
|
*/ |
37
|
|
|
private $_response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Route constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $method |
43
|
|
|
* @param string $pattern |
44
|
|
|
* @param $callable |
45
|
|
|
* @param ServerRequest $request |
46
|
|
|
* @param Response $response |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
string $method, |
50
|
|
|
string $pattern, |
51
|
|
|
$callable, |
52
|
|
|
ServerRequest $request = null, |
53
|
|
|
Response $response = null |
54
|
|
|
) { |
55
|
|
|
$this->setRoute($method, $pattern, $callable, $request, $response); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set variables of current class |
60
|
|
|
* |
61
|
|
|
* @param array $variables |
62
|
|
|
*/ |
63
|
|
|
public function setVariables(array $variables) |
64
|
|
|
{ |
65
|
|
|
$this->_variables = $variables; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return array of available variables |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getVariables(): array |
74
|
|
|
{ |
75
|
|
|
return $this->_variables; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set single route |
80
|
|
|
* |
81
|
|
|
* @param string $method |
82
|
|
|
* @param string $pattern |
83
|
|
|
* @param mixed $callable |
84
|
|
|
* @return Interfaces\Route |
85
|
|
|
*/ |
86
|
|
|
public function setRoute(string $method, string $pattern, $callable): Interfaces\Route |
87
|
|
|
{ |
88
|
|
|
return $this |
89
|
|
|
->setRegexp($pattern) |
90
|
|
|
->setCallback($callable); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $request |
95
|
|
|
* @return Interfaces\Route |
96
|
|
|
*/ |
97
|
|
|
public function setRequest($request): Interfaces\Route |
98
|
|
|
{ |
99
|
|
|
$this->_request = $request; |
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return ServerRequest |
105
|
|
|
*/ |
106
|
|
|
public function getRequest(): ServerRequest |
107
|
|
|
{ |
108
|
|
|
return $this->_request; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mixed $response |
113
|
|
|
* @return Interfaces\Route |
114
|
|
|
*/ |
115
|
|
|
public function setResponse($response): Interfaces\Route |
116
|
|
|
{ |
117
|
|
|
$this->_response = $response; |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Response |
123
|
|
|
*/ |
124
|
|
|
public function getResponse(): Response |
125
|
|
|
{ |
126
|
|
|
return $this->_response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set callable element or class |
131
|
|
|
* |
132
|
|
|
* @param mixed $callback |
133
|
|
|
* @return Interfaces\Route |
134
|
|
|
*/ |
135
|
|
|
public function setCallback($callback): Interfaces\Route |
136
|
|
|
{ |
137
|
|
|
$this->_callback = $callback; |
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return callable element |
143
|
|
|
* |
144
|
|
|
* @return Interfaces\Callback |
145
|
|
|
*/ |
146
|
|
|
public function getCallback(): Interfaces\Callback |
147
|
|
|
{ |
148
|
|
|
return $this->_callback; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set regexp of current route |
153
|
|
|
* |
154
|
|
|
* @param string $regexp |
155
|
|
|
* @return Interfaces\Route |
156
|
|
|
*/ |
157
|
|
|
public function setRegexp(string $regexp): Interfaces\Route |
158
|
|
|
{ |
159
|
|
|
$pattern = ['/</', '/>/']; |
160
|
|
|
$replace = ['(?P<', '>.+)']; |
161
|
|
|
$regexp = preg_replace($pattern, $replace, $regexp); |
162
|
|
|
$this->_regexp = '#^' . $regexp . '$#u'; |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return regexp of current route |
168
|
|
|
* |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function getRegexp(): string |
172
|
|
|
{ |
173
|
|
|
return $this->_regexp; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.