HttpsMutableQuery   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

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

12 Methods

Rating   Name   Duplication   Size   Complexity  
A first() 0 3 1
A only() 0 3 1
A all() 0 3 1
A get() 0 3 1
A walkRecursive() 0 4 2
A set() 0 5 1
A has() 0 3 1
A forgetAll() 0 5 1
A encoded() 0 7 2
A last() 0 3 1
A raw() 0 7 2
A forget() 0 12 3
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