1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
4
|
|
|
|
5
|
|
|
abstract class Rule implements \Serializable, \JsonSerializable |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
use Attachable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $_protocol; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $_host; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var null|string |
22
|
|
|
*/ |
23
|
|
|
protected $type; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var null|Path |
27
|
|
|
*/ |
28
|
|
|
protected $path; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var null|array |
32
|
|
|
*/ |
33
|
|
|
protected $methods; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $defaults; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $middleware; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Rule constructor. |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* @param iterable $storage |
50
|
|
|
* @param null|self $parent |
51
|
|
|
*/ |
52
|
5 |
|
public function __construct(string $key, $storage, ?self $parent = null) |
53
|
|
|
{ |
54
|
5 |
|
$this->prepare(); |
55
|
5 |
|
$this->initializer($key, $storage); |
|
|
|
|
56
|
5 |
|
$this->pathInit(); |
57
|
5 |
|
if ($parent) { |
58
|
2 |
|
$this->afterPrepare($parent); |
59
|
|
|
} |
60
|
5 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getName(): string |
66
|
|
|
{ |
67
|
|
|
return $this->_name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
3 |
|
public function getProtocol(): string |
74
|
|
|
{ |
75
|
3 |
|
return $this->_protocol; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
3 |
|
public function getHost(): string |
82
|
|
|
{ |
83
|
3 |
|
return $this->_host; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return Path |
88
|
|
|
*/ |
89
|
3 |
|
public function getPath(): Path |
90
|
|
|
{ |
91
|
3 |
|
return $this->path; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return null|array |
96
|
|
|
*/ |
97
|
4 |
|
public function getMethods(): ?array |
98
|
|
|
{ |
99
|
4 |
|
return $this->methods; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
4 |
|
public function getDefaults(): array |
106
|
|
|
{ |
107
|
4 |
|
return (array)$this->defaults; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Path|null $path |
112
|
|
|
*/ |
113
|
2 |
|
protected function hinge(?Path $path): void |
114
|
|
|
{ |
115
|
2 |
|
if ($this->path && $path) { |
116
|
2 |
|
$this->path->hinge($path); |
117
|
2 |
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->path = $path ?? $this->path; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $name |
125
|
|
|
* @param self $parent |
126
|
|
|
*/ |
127
|
2 |
|
protected function arrayMerge(string $name, self $parent): void |
128
|
|
|
{ |
129
|
2 |
|
$this->$name = \array_merge( |
130
|
2 |
|
(array)$parent->$name, |
131
|
2 |
|
(array)$this->$name |
132
|
|
|
); |
133
|
2 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param self $parent |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
2 |
|
protected function afterPrepare(self $parent): void |
140
|
|
|
{ |
141
|
2 |
|
$this->hinge($parent->path); |
142
|
2 |
|
$this->_protocol = $parent->protocol ?? $parent->_protocol ?? $this->_protocol; |
143
|
2 |
|
$this->_host = $parent->host ?? $parent->_host ?? $this->_host; |
144
|
2 |
|
$this->_name = $parent->_name . '.' . $this->_name; |
145
|
2 |
|
$this->methods = $this->methods ?? $parent->methods; |
146
|
2 |
|
$this->arrayMerge('defaults', $parent); |
147
|
2 |
|
$this->arrayMerge('middleware', $parent); |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* if this.path === string then new Path(string, []) |
152
|
|
|
* else this.path === array then new Path(...this.path) |
153
|
|
|
*/ |
154
|
5 |
|
protected function pathInit(): void |
155
|
|
|
{ |
156
|
5 |
|
if (\is_string($this->path)) { |
|
|
|
|
157
|
4 |
|
$this->path = new Path($this->path); |
158
|
1 |
|
} elseif (\is_array($this->path)) { |
|
|
|
|
159
|
|
|
$this->path = new Path(...$this->path); |
160
|
|
|
} |
161
|
5 |
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
5 |
|
protected function prepare(): void |
167
|
|
|
{ |
168
|
5 |
|
$this->_protocol = '\w+'; |
169
|
5 |
|
$this->_host = '[^\/]+'; |
170
|
5 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
* @throws \ReflectionException |
175
|
|
|
*/ |
176
|
|
|
protected function serializeArray(): array |
177
|
|
|
{ |
178
|
|
|
$properties = []; |
179
|
|
|
$reflation = new \ReflectionClass($this); |
180
|
|
|
$protected = $reflation->getProperties(\ReflectionProperty::IS_PROTECTED); |
181
|
|
|
foreach ($protected as $property) { |
182
|
|
|
$properties[] = $property->name; |
183
|
|
|
} |
184
|
|
|
return $properties; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array |
189
|
|
|
* @throws \ReflectionException |
190
|
|
|
*/ |
191
|
|
|
public function __sleep(): array |
192
|
|
|
{ |
193
|
|
|
return \array_keys($this->serializeArray()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @inheritdoc |
198
|
|
|
* @throws \ReflectionException |
199
|
|
|
*/ |
200
|
|
|
public function serialize(): string |
201
|
|
|
{ |
202
|
|
|
$data = []; |
203
|
|
|
foreach ($this->serializeArray() as $key => $value) { |
204
|
|
|
$data[$value] = $this->$value; |
205
|
|
|
} |
206
|
|
|
return \serialize($data); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @inheritdoc |
211
|
|
|
*/ |
212
|
|
|
public function unserialize($serialized): void |
213
|
|
|
{ |
214
|
|
|
/** |
215
|
|
|
* @var string $serialized |
216
|
|
|
* @var array $data |
217
|
|
|
*/ |
218
|
|
|
$data = \unserialize($serialized, (array)null); |
219
|
|
|
foreach ($data as $key => $value) { |
220
|
|
|
$this->{$key} = $value; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @inheritdoc |
226
|
|
|
* @throws \ReflectionException |
227
|
|
|
*/ |
228
|
|
|
public function jsonSerialize(): array |
229
|
|
|
{ |
230
|
|
|
$data = []; |
231
|
|
|
foreach ($this->serializeArray() as $key => $value) { |
232
|
|
|
$key = \ltrim($value, '_'); |
233
|
|
|
$data[$key] = $this->$value; |
234
|
|
|
} |
235
|
|
|
return $data; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|