1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Keppler\Url\Builder\Schemes\Https\Bags; |
5
|
|
|
|
6
|
|
|
use Keppler\Url\Interfaces\Mutable\MutableBagInterface; |
7
|
|
|
use Keppler\Url\Traits\Accessor; |
8
|
|
|
use Keppler\Url\Traits\Mutator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class HttpsMutableQuery |
12
|
|
|
* |
13
|
|
|
* @package Keppler\Url\Builder\Schemes\Https\Bags |
14
|
|
|
*/ |
15
|
|
|
class HttpsMutableQuery implements MutableBagInterface |
16
|
|
|
{ |
17
|
|
|
use Mutator; |
18
|
|
|
use Accessor; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* query = *( pchar / "/" / "?" ) |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $query = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array|null |
29
|
|
|
*/ |
30
|
|
|
public function first(): ?array |
31
|
|
|
{ |
32
|
|
|
return $this->firstInQuery($this->query); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array|null |
37
|
|
|
*/ |
38
|
|
|
public function last() |
39
|
|
|
{ |
40
|
|
|
return $this->lastInQuery($this->query); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string ...$args |
45
|
|
|
* |
46
|
|
|
* @return HttpsMutableQuery |
47
|
|
|
*/ |
48
|
|
|
public function forget(string ...$args): self |
49
|
|
|
{ |
50
|
|
|
foreach ($args as $item) { |
51
|
|
|
if (!$this->hasValueIn($this->query, $item)) { |
52
|
|
|
throw new \LogicException(sprintf('Cannot forget %s as it does not exist', |
53
|
|
|
$item)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->mutatorForgetKeyOrValue($this->query, $item); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return HttpsMutableQuery |
64
|
|
|
*/ |
65
|
|
|
public function forgetAll(): self |
66
|
|
|
{ |
67
|
|
|
$this->query = []; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string ...$args |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function only(string ...$args): array |
77
|
|
|
{ |
78
|
|
|
return $this->mutatorQueryOnlyValues($this->query, $args); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return \Generator |
83
|
|
|
*/ |
84
|
|
|
public function walkRecursive(): \Generator |
85
|
|
|
{ |
86
|
|
|
foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->query)) as $key => $value) { |
87
|
|
|
yield $key => $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
///////////////////////////////// |
92
|
|
|
/// INTERFACE IMPLEMENTATION /// |
93
|
|
|
//////////////////////////////// |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $key |
97
|
|
|
* @return mixed |
98
|
|
|
* @throws \Keppler\Url\Exceptions\ComponentNotFoundException |
99
|
|
|
*/ |
100
|
|
|
public function get($key) |
101
|
|
|
{ |
102
|
|
|
return $this->getKeyIn($this->query, $key); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $key |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
|
|
public function has($key): bool |
111
|
|
|
{ |
112
|
|
|
return $this->hasKeyIn($this->query, $key); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns all the components of the query |
117
|
|
|
* |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function all(): array |
121
|
|
|
{ |
122
|
|
|
return $this->query; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return the raw unaltered query |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function raw(): string |
131
|
|
|
{ |
132
|
|
|
if (!empty($this->query)) { |
133
|
|
|
return '?'.urldecode(http_build_query($this->query)); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return ''; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Returns the encoded query or path string |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function encoded(): string |
145
|
|
|
{ |
146
|
|
|
if (!empty($this->query)) { |
147
|
|
|
return '?'.http_build_query($this->query); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return ''; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Sets a given key => value to the query or path |
155
|
|
|
* Some bags should set a class property if they |
156
|
|
|
* contain multidimensional values by default |
157
|
|
|
* |
158
|
|
|
* @param $key |
159
|
|
|
* @param $value |
160
|
|
|
* |
161
|
|
|
* @return self |
162
|
|
|
*/ |
163
|
|
|
public function set($key, $value): self |
164
|
|
|
{ |
165
|
|
|
$this->query[$key] = $value; |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|