|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Router; |
|
4
|
|
|
|
|
5
|
|
|
class Pattern implements PatternResolution |
|
6
|
|
|
{ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* if methods === null -> full methods |
|
10
|
|
|
* |
|
11
|
|
|
* @var null|array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $methods; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $extends = []; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $defaults = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $name; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $path; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Pattern constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $path |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(string $path, ?string $name) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->path = $path; |
|
44
|
|
|
$this->name = $name; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
public function toArray(): array |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
|
|
$this->getName() => [ |
|
54
|
|
|
'type' => 'pattern', |
|
55
|
|
|
'path' => $this->getPath(), |
|
56
|
|
|
'methods' => $this->getMethods(), |
|
57
|
|
|
'defaults' => $this->getDefaults(), |
|
58
|
|
|
'extends' => $this->getExtends(), |
|
59
|
|
|
] |
|
60
|
|
|
]; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getName(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->name ?: Helper::generateName($this->path); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
* |
|
74
|
|
|
* @return $this |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setName(string $name): self |
|
77
|
|
|
{ |
|
78
|
|
|
$this->name = $name; |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getPath(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->path; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param string $path |
|
93
|
|
|
* |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setPath(string $path): self |
|
97
|
|
|
{ |
|
98
|
|
|
$this->path = $path; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array|null |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getMethods(): ?array |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->methods; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array|null $methods |
|
113
|
|
|
* |
|
114
|
|
|
* @return $this |
|
115
|
|
|
*/ |
|
116
|
|
|
public function setMethods(?array $methods): self |
|
117
|
|
|
{ |
|
118
|
|
|
$this->methods = $methods; |
|
119
|
|
|
|
|
120
|
|
|
return $this; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getDefaults(): array |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->defaults; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param array $defaults |
|
133
|
|
|
* |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setDefaults(array $defaults): self |
|
137
|
|
|
{ |
|
138
|
|
|
$this->defaults = $defaults; |
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getExtends(): array |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->extends; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param array $extends |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setExtends(array $extends): void |
|
155
|
|
|
{ |
|
156
|
|
|
$this->extends = $extends; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param array $methods |
|
161
|
|
|
* |
|
162
|
|
|
* @return Pattern |
|
163
|
|
|
*/ |
|
164
|
|
|
public function methods(array $methods): Pattern |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->setMethods($methods); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return Pattern |
|
171
|
|
|
*/ |
|
172
|
|
|
public function any(): self |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->setMethods(null); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return Pattern |
|
179
|
|
|
*/ |
|
180
|
|
|
public function get(): self |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->setMethods(['GET', 'HEAD']); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return Pattern |
|
187
|
|
|
*/ |
|
188
|
|
|
public function post(): self |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->setMethods(['POST']); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return Pattern |
|
195
|
|
|
*/ |
|
196
|
|
|
public function put(): self |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->setMethods(['PUT']); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @return Pattern |
|
203
|
|
|
*/ |
|
204
|
|
|
public function patch(): self |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->setMethods(['PATCH']); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return Pattern |
|
211
|
|
|
*/ |
|
212
|
|
|
public function delete(): self |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->setMethods(['DELETE']); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
} |
|
218
|
|
|
|