|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Keppler\Url\Builder\Schemes\Http\Bags; |
|
5
|
|
|
|
|
6
|
|
|
use Keppler\Url\Exceptions\ComponentNotFoundException; |
|
7
|
|
|
use Keppler\Url\Interfaces\Mutable\MutableBagInterface; |
|
8
|
|
|
use Keppler\Url\Traits\Accessor; |
|
9
|
|
|
use Keppler\Url\Traits\Mutator; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class HttpMutablePath |
|
13
|
|
|
* @package Keppler\Url\Builder\Schemes\Https\Bags |
|
14
|
|
|
*/ |
|
15
|
|
|
class HttpMutablePath implements MutableBagInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use Mutator; |
|
18
|
|
|
use Accessor; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* path = path-abempty ; begins with "/" or is empty |
|
22
|
|
|
* |
|
23
|
|
|
* |
|
24
|
|
|
* / path-absolute ; begins with "/" but not "//" |
|
25
|
|
|
* / path-noscheme ; begins with a non-colon segment |
|
26
|
|
|
* / path-rootless ; begins with a segment |
|
27
|
|
|
* / path-empty ; zero characters |
|
28
|
|
|
* |
|
29
|
|
|
* path-abempty = *( "/" segment ) |
|
30
|
|
|
* path-absolute = "/" [ segment-nz *( "/" segment ) ] |
|
31
|
|
|
* path-noscheme = segment-nz-nc *( "/" segment ) |
|
32
|
|
|
* path-rootless = segment-nz *( "/" segment ) |
|
33
|
|
|
* path-empty = 0<pchar> |
|
34
|
|
|
* segment = *pchar |
|
35
|
|
|
* segment-nz = 1*pchar |
|
36
|
|
|
* segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" ) |
|
37
|
|
|
* ; non-zero-length segment without any colon ":" |
|
38
|
|
|
* |
|
39
|
|
|
* pchar = unreserved / pct-encoded / sub-delims / ":" / "@ |
|
40
|
|
|
* |
|
41
|
|
|
* @see https://tools.ietf.org/html/rfc3986#page-22 |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $path = []; |
|
46
|
|
|
|
|
47
|
|
|
////////////////////////// |
|
48
|
|
|
/// GETTER FUNCTIONS /// |
|
49
|
|
|
//////////////////////// |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getPath(): array |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->path; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return string|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function first(): ?string |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->firstInPath($this->path); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function last(): ?string |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->lastInPath($this->path); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $value |
|
77
|
|
|
* |
|
78
|
|
|
* @return HttpMutablePath |
|
79
|
|
|
*/ |
|
80
|
|
|
public function append(string $value): self |
|
81
|
|
|
{ |
|
82
|
|
|
$this->mutatorAppend($this->path, $value); |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $value |
|
89
|
|
|
* |
|
90
|
|
|
* @return HttpMutablePath |
|
91
|
|
|
*/ |
|
92
|
|
|
public function prepend(string $value): self |
|
93
|
|
|
{ |
|
94
|
|
|
$this->mutatorPrepend($this->path, $value); |
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $value |
|
101
|
|
|
* @param string|null $first |
|
102
|
|
|
* @param string|null $last |
|
103
|
|
|
* |
|
104
|
|
|
* @return HttpMutablePath |
|
105
|
|
|
* @throws ComponentNotFoundException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function putInBetween(string $value, string $first = null, string $last = null): self |
|
108
|
|
|
{ |
|
109
|
|
|
if (null === $first && null === $last) { |
|
110
|
|
|
throw new \LogicException('Cannot put value if neither first or last is defined'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (!$this->hasValueIn($this->path, $first) && !$this->hasValueIn($this->path, $last)) { |
|
114
|
|
|
throw new ComponentNotFoundException(sprintf('No component found matching either %s %s', $first, $last)); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->mutatorPutInBetweenKeys($this->path, $value, $first, $last); |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param string $before |
|
124
|
|
|
* @param string $value |
|
125
|
|
|
* |
|
126
|
|
|
* @return HttpMutablePath |
|
127
|
|
|
* @throws \LogicException |
|
128
|
|
|
*/ |
|
129
|
|
|
public function putBefore(string $before, string $value) : self |
|
130
|
|
|
{ |
|
131
|
|
|
if (!$this->hasValueIn($this->path, $before)) { |
|
132
|
|
|
throw new \LogicException(sprintf('Cannot put value %s before %s as %s does not exist', $value, $before, $before)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
$this->path = $this->mutatorPutBeforeValue($this->path, $before, $value); |
|
136
|
|
|
|
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param string $after |
|
142
|
|
|
* @param string $value |
|
143
|
|
|
* |
|
144
|
|
|
* @return HttpMutablePath |
|
145
|
|
|
* @throws \LogicException |
|
146
|
|
|
*/ |
|
147
|
|
|
public function putAfter(string $after, string $value): self |
|
148
|
|
|
{ |
|
149
|
|
|
if (!$this->hasValueIn($this->path, $after)) { |
|
150
|
|
|
throw new \LogicException(sprintf('Cannot put value %s after %s as %s does not exist', $value, $after, $after)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$this->path = $this->mutatorPutAfterValue($this->path, $after, $value); |
|
154
|
|
|
|
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param string ...$args |
|
160
|
|
|
* |
|
161
|
|
|
* @return HttpMutablePath |
|
162
|
|
|
*/ |
|
163
|
|
|
public function forget(string ...$args): self |
|
164
|
|
|
{ |
|
165
|
|
|
foreach ($args as $item) { |
|
166
|
|
|
if (!$this->hasValueIn($this->path, $item)) { |
|
167
|
|
|
throw new \LogicException(sprintf('Cannot forget %s as it does not exist', $item)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$this->mutatorForgetKeyOrValue($this->path, $item); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return HttpMutablePath |
|
178
|
|
|
*/ |
|
179
|
|
|
public function forgetAll(): self |
|
180
|
|
|
{ |
|
181
|
|
|
$this->path = []; |
|
182
|
|
|
|
|
183
|
|
|
return $this; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param string ...$args |
|
188
|
|
|
* @return array |
|
189
|
|
|
*/ |
|
190
|
|
|
public function only(string ...$args): array |
|
191
|
|
|
{ |
|
192
|
|
|
return$this->mutatorOnlyPathValues($this->path, $args); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
///////////////////////////////// |
|
196
|
|
|
/// INTERFACE IMPLEMENTATION /// |
|
197
|
|
|
/////////////////////////////// |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param $key |
|
201
|
|
|
* @return mixed |
|
202
|
|
|
* @throws ComponentNotFoundException |
|
203
|
|
|
*/ |
|
204
|
|
|
public function get($key) |
|
205
|
|
|
{ |
|
206
|
|
|
return $this->getKeyIn($this->path, $key); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param $key |
|
211
|
|
|
* @return bool |
|
212
|
|
|
*/ |
|
213
|
|
|
public function has($key): bool |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->hasKeyIn($this->path, $key); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Returns all the components of the query or path |
|
220
|
|
|
* |
|
221
|
|
|
* @return array |
|
222
|
|
|
*/ |
|
223
|
|
|
public function all(): array |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->path; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Return the raw unaltered query or path |
|
230
|
|
|
* |
|
231
|
|
|
* @return string |
|
232
|
|
|
*/ |
|
233
|
|
|
public function raw(): string |
|
234
|
|
|
{ |
|
235
|
|
|
if (empty($this->path)) { |
|
236
|
|
|
return ''; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
$path = '/'; |
|
240
|
|
|
foreach ($this->path as $element) { |
|
241
|
|
|
$path .= $element.'/'; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $path; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Returns the encoded query or path string |
|
249
|
|
|
* |
|
250
|
|
|
* @return string |
|
251
|
|
|
*/ |
|
252
|
|
|
public function encoded(): string |
|
253
|
|
|
{ |
|
254
|
|
|
if (empty($this->path)) { |
|
255
|
|
|
return ''; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
$path = '/'; |
|
259
|
|
|
foreach ($this->path as $element) { |
|
260
|
|
|
$path .= urlencode($element).'/'; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
return $path; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param $key |
|
268
|
|
|
* @param $value |
|
269
|
|
|
* |
|
270
|
|
|
* @return HttpMutablePath |
|
271
|
|
|
*/ |
|
272
|
|
|
public function set($key, $value): self |
|
273
|
|
|
{ |
|
274
|
|
|
$this->path[$key] = $value; |
|
275
|
|
|
|
|
276
|
|
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|