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 string |
49
|
|
|
*/ |
50
|
|
|
public function getName(): string |
51
|
|
|
{ |
52
|
|
|
return $this->name ?: Helper::generateName($this->path); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setName(string $name): self |
61
|
|
|
{ |
62
|
|
|
$this->name = $name; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function getExtends(): array |
71
|
|
|
{ |
72
|
|
|
return $this->extends; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $extends |
77
|
|
|
*/ |
78
|
|
|
public function setExtends(array $extends): void |
79
|
|
|
{ |
80
|
|
|
$this->extends = $extends; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return array|null |
85
|
|
|
*/ |
86
|
|
|
public function getMethods(): ?array |
87
|
|
|
{ |
88
|
|
|
return $this->methods; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array|null $methods |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function setMethods(?array $methods): self |
97
|
|
|
{ |
98
|
|
|
$this->methods = $methods; |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function getDefaults(): array |
107
|
|
|
{ |
108
|
|
|
return $this->defaults; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $defaults |
113
|
|
|
* |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function setDefaults(array $defaults): self |
117
|
|
|
{ |
118
|
|
|
$this->defaults = $defaults; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getPath(): string |
127
|
|
|
{ |
128
|
|
|
return $this->path; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $path |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function setPath(string $path): self |
137
|
|
|
{ |
138
|
|
|
$this->path = $path; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function toArray(): array |
147
|
|
|
{ |
148
|
|
|
return [ |
149
|
|
|
$this->getName() => [ |
150
|
|
|
'type' => 'pattern', |
151
|
|
|
'path' => $this->getPath(), |
152
|
|
|
'methods' => $this->getMethods(), |
153
|
|
|
'defaults' => $this->getDefaults(), |
154
|
|
|
'extends' => $this->getExtends(), |
155
|
|
|
] |
156
|
|
|
]; |
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
|
|
|
|