HttpsImmutableQuery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A raw() 0 3 1
A walkRecursive() 0 4 2
A all() 0 3 1
A first() 0 3 1
A last() 0 3 1
A has() 0 3 1
A __construct() 0 9 2
A buildFromParsed() 0 3 1
A get() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Keppler\Url\Scheme\Schemes\Https\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 HttpsImmutableQuery
12
 * @package Keppler\Url\Schemes\Http\Bags
13
 */
14
class HttpsImmutableQuery 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
     * HttpsImmutableQuery 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
    public function last()
93
    {
94
        return $this->lastInQuery($this->query);
95
    }
96
97
    /**
98
     * @return \Generator
99
     */
100
    public function walkRecursive(): \Generator
101
    {
102
        foreach (new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->query)) as $key => $value) {
103
            yield $key => $value;
104
        }
105
    }
106
107
/////////////////////////////////
108
/// INTERFACE IMPLEMENTATION  ///
109
////////////////////////////////
110
111
    /**
112
     * Returns all the components of the query
113
     *
114
     * @return array
115
     */
116
    public function all(): array
117
    {
118
        return $this->query;
119
    }
120
121
    /**
122
     * Return the raw unaltered query
123
     *
124
     * @return string
125
     */
126
    public function raw(): string
127
    {
128
        return $this->raw;
129
    }
130
}