1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
4
|
|
|
|
5
|
|
|
abstract class Rule |
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 array $storage |
45
|
|
|
* @param null|self $parent |
46
|
|
|
*/ |
47
|
|
|
public function __construct(string $key, array $storage, ?self $parent = null) |
48
|
|
|
{ |
49
|
|
|
$this->prepare(); |
50
|
|
|
$this->initializer($key, $storage); |
51
|
|
|
$this->pathInit(); |
52
|
|
|
if ($parent) { |
53
|
|
|
$this->afterPrepare($parent); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getProtocol(): string |
61
|
|
|
{ |
62
|
|
|
return $this->_protocol; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getHost(): string |
69
|
|
|
{ |
70
|
|
|
return $this->_host; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return Path |
75
|
|
|
*/ |
76
|
|
|
public function getPath(): Path |
77
|
|
|
{ |
78
|
|
|
return $this->path; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return null|array |
83
|
|
|
*/ |
84
|
|
|
public function getMethods(): ?array |
85
|
|
|
{ |
86
|
|
|
return $this->methods; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getDefaults(): array |
93
|
|
|
{ |
94
|
|
|
return (array)$this->defaults; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Path|null $path |
99
|
|
|
*/ |
100
|
|
|
protected function hinge(?Path $path): void |
101
|
|
|
{ |
102
|
|
|
if ($this->path && $path) { |
103
|
|
|
$this->path->hinge($path); |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->path = $path ?? $this->path; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param self $parent |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function afterPrepare(self $parent): void |
115
|
|
|
{ |
116
|
|
|
$this->hinge($parent->path); |
117
|
|
|
$this->_protocol = $parent->protocol ?? $parent->_protocol ?? $this->_protocol; |
118
|
|
|
$this->_host = $parent->host ?? $parent->_host ?? $this->_host; |
119
|
|
|
$this->_key = $parent->_key . '.' . $this->_key; |
120
|
|
|
$this->methods = $this->methods ?? $parent->methods; |
121
|
|
|
$this->defaults = \array_merge( |
122
|
|
|
(array)$parent->defaults, |
123
|
|
|
(array)$this->defaults |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* if this.path === string then new Path(string, []) |
129
|
|
|
* else this.path === array then new Path(...this.path) |
130
|
|
|
*/ |
131
|
|
|
protected function pathInit(): void |
132
|
|
|
{ |
133
|
|
|
if (\is_string($this->path)) { |
|
|
|
|
134
|
|
|
$this->path = new Path($this->path); |
135
|
|
|
} elseif (\is_array($this->path)) { |
|
|
|
|
136
|
|
|
$this->path = new Path(...$this->path); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function prepare(): void |
144
|
|
|
{ |
145
|
|
|
$this->_protocol = '\w+'; |
146
|
|
|
$this->_host = '[^\/]+'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|