Completed
Push — master ( 53dd23...58e367 )
by BENOIT
01:13
created

Pairs::withSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
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
        $pairs = explode($separator, (string) $this->queryString);
97
        foreach ($pairs as $pair) {
98
            list($key, $value) = explode('=', $pair);
99
100
            if (true === $this->decodeKeys) {
101
                $key = urldecode($key);
102
            }
103
104
            if (true === $this->decodeValues) {
105
                $value = urldecode($value);
106
            }
107
108
            yield $key => $value;
109
        }
110
    }
111
112
    /**
113
     * @return string
114
     */
115
    public function __toString(): string
116
    {
117
        return (string) $this->queryString;
118
    }
119
}
120