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