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