HttpMutableQuery   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 154
rs 10
c 0
b 0
f 0
wmc 17

12 Methods

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