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
|
|
|
* Rule constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $key |
44
|
|
|
* @param iterable $storage |
45
|
|
|
* @param null|self $parent |
46
|
|
|
*/ |
47
|
5 |
|
public function __construct(string $key, $storage, ?self $parent = null) |
48
|
|
|
{ |
49
|
5 |
|
$this->prepare(); |
50
|
5 |
|
$this->initializer($key, $storage); |
|
|
|
|
51
|
5 |
|
$this->pathInit(); |
52
|
5 |
|
if ($parent) { |
53
|
2 |
|
$this->afterPrepare($parent); |
54
|
|
|
} |
55
|
5 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getName(): string |
61
|
|
|
{ |
62
|
|
|
return $this->_name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
3 |
|
public function getProtocol(): string |
69
|
|
|
{ |
70
|
3 |
|
return $this->_protocol; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
3 |
|
public function getHost(): string |
77
|
|
|
{ |
78
|
3 |
|
return $this->_host; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Path |
83
|
|
|
*/ |
84
|
3 |
|
public function getPath(): Path |
85
|
|
|
{ |
86
|
3 |
|
return $this->path; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return null|array |
91
|
|
|
*/ |
92
|
4 |
|
public function getMethods(): ?array |
93
|
|
|
{ |
94
|
4 |
|
return $this->methods; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
4 |
|
public function getDefaults(): array |
101
|
|
|
{ |
102
|
4 |
|
return (array)$this->defaults; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Path|null $path |
107
|
|
|
*/ |
108
|
2 |
|
protected function hinge(?Path $path): void |
109
|
|
|
{ |
110
|
2 |
|
if ($this->path && $path) { |
111
|
2 |
|
$this->path->hinge($path); |
112
|
2 |
|
return; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->path = $path ?? $this->path; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param self $parent |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
2 |
|
protected function afterPrepare(self $parent): void |
123
|
|
|
{ |
124
|
2 |
|
$this->hinge($parent->path); |
125
|
2 |
|
$this->_protocol = $parent->protocol ?? $parent->_protocol ?? $this->_protocol; |
126
|
2 |
|
$this->_host = $parent->host ?? $parent->_host ?? $this->_host; |
127
|
2 |
|
$this->_name = $parent->_name . '.' . $this->_name; |
128
|
2 |
|
$this->methods = $this->methods ?? $parent->methods; |
129
|
2 |
|
$this->defaults = \array_merge( |
130
|
2 |
|
(array)$parent->defaults, |
131
|
2 |
|
(array)$this->defaults |
132
|
|
|
); |
133
|
2 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* if this.path === string then new Path(string, []) |
137
|
|
|
* else this.path === array then new Path(...this.path) |
138
|
|
|
*/ |
139
|
5 |
|
protected function pathInit(): void |
140
|
|
|
{ |
141
|
5 |
|
if (\is_string($this->path)) { |
|
|
|
|
142
|
4 |
|
$this->path = new Path($this->path); |
143
|
1 |
|
} elseif (\is_array($this->path)) { |
|
|
|
|
144
|
|
|
$this->path = new Path(...$this->path); |
145
|
|
|
} |
146
|
5 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return void |
150
|
|
|
*/ |
151
|
5 |
|
protected function prepare(): void |
152
|
|
|
{ |
153
|
5 |
|
$this->_protocol = '\w+'; |
154
|
5 |
|
$this->_host = '[^\/]+'; |
155
|
5 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return array |
159
|
|
|
* @throws |
160
|
|
|
*/ |
161
|
|
|
protected function serializeArray(): array |
162
|
|
|
{ |
163
|
|
|
$properties = []; |
164
|
|
|
$reflation = new \ReflectionClass($this); |
165
|
|
|
$protected = $reflation->getProperties(\ReflectionProperty::IS_PROTECTED); |
166
|
|
|
foreach ($protected as $property) { |
167
|
|
|
$properties[] = $property->name; |
168
|
|
|
} |
169
|
|
|
return $properties; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @inheritdoc |
174
|
|
|
*/ |
175
|
|
|
public function serialize(): string |
176
|
|
|
{ |
177
|
|
|
return \serialize($this->serializeArray()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @inheritdoc |
182
|
|
|
*/ |
183
|
|
|
public function unserialize($serialized): void |
184
|
|
|
{ |
185
|
|
|
$data = \unserialize($serialized, null); |
186
|
|
|
foreach ($data as $key => $value) { |
187
|
|
|
$this->{$key} = $value; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @inheritdoc |
193
|
|
|
*/ |
194
|
|
|
public function jsonSerialize(): array |
195
|
|
|
{ |
196
|
|
|
$data = []; |
197
|
|
|
foreach ($this->serializeArray() as $key => $value) { |
198
|
|
|
$key = \ltrim($value, '_'); |
199
|
|
|
$data[$key] = $this->$value; |
200
|
|
|
} |
201
|
|
|
return $data; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|