Completed
Push — master ( cdcbb3...004be8 )
by BENOIT
01:09
created

Pairs   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A withQueryString() 0 6 1
A withDecodeKeys() 0 6 1
A withDecodeValues() 0 6 1
A getIterator() 0 18 4
A __toString() 0 4 1
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 QueryString
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
     * Pairs constructor.
27
     */
28
    public function __construct(
29
        QueryString $queryString,
30
        bool $decodeKeys = false,
31
        bool $decodeValues = false,
32
        string $separator = null
0 ignored issues
show
Unused Code introduced by
The parameter $separator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    ) {
34
35
        $this->queryString = $queryString;
36
        $this->decodeKeys = $decodeKeys;
37
        $this->decodeValues = $decodeValues;
38
    }
39
40
    /**
41
     * @param QueryString $queryString
42
     * @return Pairs
43
     */
44
    public function withQueryString(QueryString $queryString): self
45
    {
46
        $clone = clone $this;
47
        $clone->queryString = $queryString;
48
        return $clone;
49
    }
50
51
    /**
52
     * @param bool $decodeKeys
53
     * @return Pairs
54
     */
55
    public function withDecodeKeys(bool $decodeKeys): self
56
    {
57
        $clone = clone $this;
58
        $clone->decodeKeys = $decodeKeys;
59
        return $clone;
60
    }
61
62
    /**
63
     * @param bool $decodeValues
64
     * @return Pairs
65
     */
66
    public function withDecodeValues(bool $decodeValues): self
67
    {
68
        $clone = clone $this;
69
        $clone->decodeValues = $decodeValues;
70
        return $clone;
71
    }
72
73
    /**
74
     * @return Traversable
75
     */
76
    public function getIterator(): Traversable
77
    {
78
        $separator = $this->queryString->getRenderer()->getSeparator() ?? ini_get('arg_separator.input');
79
        $pairs = explode($separator, (string) $this->queryString);
80
        foreach ($pairs as $pair) {
81
            list($key, $value) = explode('=', $pair);
82
83
            if (true === $this->decodeKeys) {
84
                $key = urldecode($key);
85
            }
86
87
            if (true === $this->decodeValues) {
88
                $value = urldecode($value);
89
            }
90
91
            yield $key => $value;
92
        }
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function __toString(): string
99
    {
100
        return (string) $this->queryString;
101
    }
102
}
103