|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Router\Rules\PatternRule; |
|
6
|
|
|
|
|
7
|
|
|
class Match implements \Serializable, \JsonSerializable |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $urlData; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $protocol; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $host; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $attributes = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $groups = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $method; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $subject; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var PatternRule |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $rule; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $test; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param PatternRule $rule |
|
57
|
|
|
* @param string $subject |
|
58
|
|
|
* @param string $method |
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function __construct(PatternRule $rule, string $subject, string $method) |
|
61
|
|
|
{ |
|
62
|
5 |
|
$this->attributes = $rule->getDefaults(); |
|
63
|
5 |
|
$this->urlData = \parse_url($subject); |
|
64
|
5 |
|
$this->rule = $rule; |
|
65
|
5 |
|
$this->method = $method; |
|
66
|
5 |
|
$this->subject = $subject; |
|
67
|
5 |
|
$this->test(); |
|
68
|
5 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getMethod(): string |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->method; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getGroups(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->groups; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getProtocol(): ?string |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->urlData['scheme'] ?? null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getHost(): ?string |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->urlData['host'] ?? null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getPath(): ?string |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->urlData['path'] ?? null; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getQuery(): string |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->urlData['query'] ?? null; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getSubject(): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->subject; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return PatternRule |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function getRule(): PatternRule |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->rule; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
3 |
|
public function getAttributes(): array |
|
138
|
|
|
{ |
|
139
|
3 |
|
return $this->attributes; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
4 |
|
public function getPattern(): string |
|
146
|
|
|
{ |
|
147
|
4 |
|
return Server::url( |
|
148
|
4 |
|
$this->rule->getPath()->getPattern(), |
|
149
|
4 |
|
$this->rule->getHost(), |
|
150
|
4 |
|
$this->rule->getProtocol() |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
5 |
|
public function isTest(): bool |
|
158
|
|
|
{ |
|
159
|
5 |
|
return $this->test; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param array $groups |
|
164
|
|
|
*/ |
|
165
|
4 |
|
protected function setGroups(array $groups): void |
|
166
|
|
|
{ |
|
167
|
4 |
|
foreach ($groups as $key => $value) { |
|
168
|
4 |
|
if ($value !== '') { |
|
169
|
4 |
|
$this->groups[$key] = $value; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
4 |
|
$this->attributes = \array_merge( |
|
174
|
4 |
|
$this->attributes, |
|
175
|
4 |
|
$this->groups |
|
176
|
|
|
); |
|
177
|
4 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* check method |
|
181
|
|
|
* |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
5 |
|
protected function methodAllowed(): bool |
|
185
|
|
|
{ |
|
186
|
5 |
|
$this->test = $this->rule->getMethods() === null || |
|
187
|
2 |
|
\in_array($this->method, $this->rule->getMethods(), true); |
|
188
|
|
|
|
|
189
|
5 |
|
return $this->isTest(); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* check subject |
|
194
|
|
|
*/ |
|
195
|
5 |
|
protected function test(): void |
|
196
|
|
|
{ |
|
197
|
5 |
|
if (!$this->methodAllowed()) { |
|
198
|
1 |
|
return; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
4 |
|
$result = \preg_match($this->regex(), $this->subject, $matches); |
|
202
|
4 |
|
$this->test = $result !== 0; |
|
203
|
4 |
|
$this->setGroups(\array_filter( |
|
204
|
4 |
|
$matches, |
|
205
|
4 |
|
'\is_string', |
|
206
|
4 |
|
\ARRAY_FILTER_USE_KEY |
|
207
|
|
|
)); |
|
208
|
4 |
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
4 |
|
protected function regex(): string |
|
214
|
|
|
{ |
|
215
|
4 |
|
return '~^' . $this->getPattern() . '$~u'; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
public function __sleep(): array |
|
222
|
|
|
{ |
|
223
|
|
|
return \array_keys($this->jsonSerialize()); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @inheritdoc |
|
228
|
|
|
*/ |
|
229
|
|
|
public function serialize(): string |
|
230
|
|
|
{ |
|
231
|
|
|
return \serialize($this->jsonSerialize()); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @inheritdoc |
|
236
|
|
|
*/ |
|
237
|
|
|
public function unserialize($serialized): void |
|
238
|
|
|
{ |
|
239
|
|
|
$data = \unserialize($serialized, (array)null); |
|
240
|
|
|
foreach ($data as $key => $value) { |
|
241
|
|
|
$this->{$key} = $value; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @inheritdoc |
|
247
|
|
|
*/ |
|
248
|
|
|
public function jsonSerialize(): array |
|
249
|
|
|
{ |
|
250
|
|
|
return [ |
|
251
|
|
|
'urlData' => $this->urlData, |
|
252
|
|
|
'protocol' => $this->protocol, |
|
253
|
|
|
'host' => $this->host, |
|
254
|
|
|
'attributes ' => $this->attributes , |
|
255
|
|
|
'method' => $this->method, |
|
256
|
|
|
'subject' => $this->subject, |
|
257
|
|
|
'rule' => $this->rule, |
|
258
|
|
|
'test' => $this->test, |
|
259
|
|
|
]; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
} |
|
263
|
|
|
|