Completed
Push — master ( 58e367...bcfef7 )
by BENOIT
01:08
created

Pairs::getIterator()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 6
nop 0
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\QueryString;
4
5
use IteratorAggregate;
6
use Traversable;
7
8
final class Pairs implements IteratorAggregate
9
{
10
    /**
11
     * @var string
12
     */
13
    private $queryString;
14
15
    /**
16
     * @var bool
17
     */
18
    private $decodeKeys;
19
20
    /**
21
     * @var bool
22
     */
23
    private $decodeValues;
24
25
    /**
26
     * @var null|string
27
     */
28
    private $separator;
29
30
    /**
31
     * Pairs constructor.
32
     */
33
    public function __construct(
34
        string $queryString,
35
        bool $decodeKeys = false,
36
        bool $decodeValues = false,
37
        string $separator = null
38
    ) {
39
40
        $this->queryString = $queryString;
41
        $this->decodeKeys = $decodeKeys;
42
        $this->decodeValues = $decodeValues;
43
        $this->separator = $separator;
44
    }
45
46
    /**
47
     * @param string $queryString
48
     * @return Pairs
49
     */
50
    public function withQueryString(string $queryString): self
51
    {
52
        $clone = clone $this;
53
        $clone->queryString = $queryString;
54
        return $clone;
55
    }
56
57
    /**
58
     * @param bool $decodeKeys
59
     * @return Pairs
60
     */
61
    public function withDecodeKeys(bool $decodeKeys): self
62
    {
63
        $clone = clone $this;
64
        $clone->decodeKeys = $decodeKeys;
65
        return $clone;
66
    }
67
68
    /**
69
     * @param bool $decodeValues
70
     * @return Pairs
71
     */
72
    public function withDecodeValues(bool $decodeValues): self
73
    {
74
        $clone = clone $this;
75
        $clone->decodeValues = $decodeValues;
76
        return $clone;
77
    }
78
79
    /**
80
     * @param null|string $separator
81
     * @return Pairs
82
     */
83
    public function withSeparator(?string $separator): self
84
    {
85
        $clone = clone $this;
86
        $clone->separator = $separator;
87
        return $clone;
88
    }
89
90
    /**
91
     * @return Traversable
92
     */
93
    public function getIterator(): Traversable
94
    {
95
        $separator = $this->separator ?? ini_get('arg_separator.input');
96
97
        if ('' === $separator) {
98
            throw new \RuntimeException("A separator cannot be blank.");
99
        }
100
101
        $pairs = explode($separator, (string) $this->queryString);
102
103
        foreach ($pairs as $pair) {
104
            list($key, $value) = explode('=', $pair);
105
106
            if (true === $this->decodeKeys) {
107
                $key = urldecode($key);
108
            }
109
110
            if (true === $this->decodeValues) {
111
                $value = urldecode($value);
112
            }
113
114
            yield $key => $value;
115
        }
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function __toString(): string
122
    {
123
        return (string) $this->queryString;
124
    }
125
}
126