HttpImmutableQuery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A raw() 0 3 1
A last() 0 3 1
A get() 0 3 1
A all() 0 3 1
A walkRecursive() 0 4 2
A __construct() 0 9 2
A buildFromParsed() 0 3 1
A first() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Scheme\Schemes\Http\Bags;
5
6
use Keppler\Url\Interfaces\Immutable\ImmutableBagInterface;
7
use Keppler\Url\Scheme\Schemes\AbstractImmutable;
8
use Keppler\Url\Traits\Accessor;
9
10
/**
11
 * Class HttpImmutableQuery
12
 * @package Keppler\Url\Schemes\Http\Bags
13
 */
14
class HttpImmutableQuery extends AbstractImmutable implements ImmutableBagInterface
15
{
16
    use Accessor;
17
18
    /**
19
     * query = *( pchar / "/" / "?" )
20
     *
21
     * @var array
22
     */
23
    private $query = [];
24
25
    /**
26
     * @var string
27
     */
28
    private $raw = '';
29
30
    /**
31
     * This should be the ONLY entry point and it should accept ONLY the raw string
32
     *
33
     * HttpImmutableQuery constructor.
34
     *
35
     * @param string $raw
36
     */
37
    public function __construct(string $raw = '')
38
    {
39
        // Leave the class with defaults if no valid raw string is provided
40
        if ('' !== trim($raw)) {
41
            $this->raw = $raw;
42
43
            $result = [];
44
            parse_str($raw, $result);
45
            $this->buildFromParsed($result);
46
        }
47
    }
48
49
///////////////////////////
50
/// PRIVATE FUNCTIONS  ///
51
/////////////////////////
52
53
    /**
54
     * @param $result
55
     */
56
    private function buildFromParsed($result)
57
    {
58
        $this->query = $result;
59
    }
60
61
//////////////////////////
62
/// GETTER FUNCTIONS  ///
63
////////////////////////
64
65
    /**
66
     * @param $key
67
     * @return mixed
68
     * @throws \Keppler\Url\Exceptions\ComponentNotFoundException
69
     */
70
    public function get($key)
71
    {
72
        return $this->getKeyIn($this->query, $key);
73
    }
74
75
    /**
76
     * @param $key
77
     * @return bool
78
     */
79
    public function has($key): bool
80
    {
81
        return $this->hasKeyIn($this->query, $key);
82
    }
83
84
    /**
85
     * @return array|null
86
     */
87
    public function first(): ?array
88
    {
89
        return $this->firstInQuery($this->query);
90
    }
91
92
    /**
93
     * @return array|null
94
     */
95
    public function last(): ?array
96
    {
97
        return $this->lastInQuery($this->query);
98
    }
99
100
    /**
101
     * @return \Generator
102
     */
103
    public function walkRecursive(): \Generator
104
    {
105
        foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->query)) as $key => $value) {
106
            yield $key => $value;
107
        }
108
    }
109
110
111
/////////////////////////////////
112
/// INTERFACE IMPLEMENTATION  ///
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
        return $this->raw;
133
    }
134
}