1 | <?php |
||
2 | |||
3 | namespace Bavix\Router; |
||
4 | |||
5 | use Bavix\Router\Rules\PatternRule; |
||
6 | |||
7 | class Match implements \Serializable, \JsonSerializable |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
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 | 7 | public function __construct(PatternRule $rule, string $subject, string $method) |
|
61 | { |
||
62 | 7 | $this->attributes = $rule->getDefaults(); |
|
63 | 7 | $this->urlData = \parse_url($subject); |
|
64 | 7 | $this->rule = $rule; |
|
65 | 7 | $this->method = $method; |
|
66 | 7 | $this->subject = $subject; |
|
67 | 7 | $this->test(); |
|
68 | 7 | } |
|
69 | |||
70 | /** |
||
71 | * check subject |
||
72 | */ |
||
73 | 7 | protected function test(): void |
|
74 | { |
||
75 | 7 | if (!$this->methodAllowed()) { |
|
76 | 1 | return; |
|
77 | } |
||
78 | |||
79 | 6 | $result = \preg_match($this->regex(), $this->subject, $matches); |
|
80 | 6 | $this->test = $result !== 0; |
|
81 | 6 | $this->setGroups(\array_filter( |
|
82 | 6 | $matches, |
|
83 | 6 | '\is_string', |
|
84 | 6 | \ARRAY_FILTER_USE_KEY |
|
85 | )); |
||
86 | 6 | } |
|
87 | |||
88 | /** |
||
89 | * check method |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | 7 | protected function methodAllowed(): bool |
|
94 | { |
||
95 | 7 | $this->test = $this->rule->getMethods() === null || |
|
96 | 2 | \in_array($this->method, $this->rule->getMethods(), true); |
|
97 | |||
98 | 7 | return $this->isTest(); |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @return bool |
||
103 | */ |
||
104 | 7 | public function isTest(): bool |
|
105 | { |
||
106 | 7 | return $this->test; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return string |
||
111 | */ |
||
112 | 6 | protected function regex(): string |
|
113 | { |
||
114 | 6 | return '~^' . $this->getPattern() . '$~u'; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @return string |
||
119 | */ |
||
120 | 6 | public function getPattern(): string |
|
121 | { |
||
122 | 6 | return Server::url( |
|
123 | 6 | $this->rule->getPath()->getPattern(), |
|
124 | 6 | $this->rule->getHost(), |
|
125 | 6 | $this->rule->getProtocol() |
|
126 | ); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getMethod(): string |
||
133 | { |
||
134 | return $this->method; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @return array |
||
139 | */ |
||
140 | public function getGroups(): array |
||
141 | { |
||
142 | return $this->groups; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param array $groups |
||
147 | */ |
||
148 | 6 | protected function setGroups(array $groups): void |
|
149 | { |
||
150 | 6 | foreach ($groups as $key => $value) { |
|
151 | 6 | if ($value !== '') { |
|
152 | 6 | $this->groups[$key] = $value; |
|
153 | } |
||
154 | } |
||
155 | |||
156 | 6 | $this->attributes = \array_merge( |
|
157 | 6 | $this->attributes, |
|
158 | 6 | $this->groups |
|
159 | ); |
||
160 | 6 | } |
|
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getProtocol(): ?string |
||
166 | { |
||
167 | return $this->urlData['scheme'] ?? null; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getHost(): ?string |
||
174 | { |
||
175 | return $this->urlData['host'] ?? null; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getPath(): ?string |
||
182 | { |
||
183 | return $this->urlData['path'] ?? null; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getQuery(): string |
||
190 | { |
||
191 | return $this->urlData['query'] ?? null; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public function getSubject(): string |
||
198 | { |
||
199 | return $this->subject; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @return PatternRule |
||
204 | */ |
||
205 | 1 | public function getRule(): PatternRule |
|
206 | { |
||
207 | 1 | return $this->rule; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return array |
||
212 | */ |
||
213 | 5 | public function getAttributes(): array |
|
214 | { |
||
215 | 5 | return $this->attributes; |
|
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 jsonSerialize(): array |
||
230 | { |
||
231 | return [ |
||
232 | 'urlData' => $this->urlData, |
||
233 | 'protocol' => $this->protocol, |
||
234 | 'host' => $this->host, |
||
235 | 'attributes ' => $this->attributes, |
||
236 | 'method' => $this->method, |
||
237 | 'subject' => $this->subject, |
||
238 | 'rule' => $this->rule, |
||
239 | 'test' => $this->test, |
||
240 | ]; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @inheritdoc |
||
245 | */ |
||
246 | public function serialize(): string |
||
247 | { |
||
248 | return \serialize($this->jsonSerialize()); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @inheritdoc |
||
253 | */ |
||
254 | public function unserialize($serialized): void |
||
255 | { |
||
256 | $data = \unserialize($serialized, (array)null); |
||
257 | foreach ($data as $key => $value) { |
||
258 | $this->{$key} = $value; |
||
259 | } |
||
260 | } |
||
261 | |||
262 | } |
||
263 |