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 string |
17
|
|
|
*/ |
18
|
|
|
private $separator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
private $decodeKeys; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $decodeValues; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Pairs constructor. |
32
|
|
|
*/ |
33
|
|
|
public function __construct( |
34
|
|
|
QueryString $queryString, |
35
|
|
|
string $separator = null, |
36
|
|
|
bool $decodeKeys = false, |
37
|
|
|
bool $decodeValues = false |
38
|
|
|
) { |
39
|
|
|
|
40
|
|
|
$this->queryString = $queryString; |
41
|
|
|
$this->separator = $separator; |
42
|
|
|
$this->decodeKeys = $decodeKeys; |
43
|
|
|
$this->decodeValues = $decodeValues; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param QueryString $queryString |
48
|
|
|
* @return Pairs |
49
|
|
|
*/ |
50
|
|
|
public function withQueryString(QueryString $queryString): self |
51
|
|
|
{ |
52
|
|
|
$clone = clone $this; |
53
|
|
|
$clone->queryString = $queryString; |
54
|
|
|
return $clone; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param null|string $separator |
59
|
|
|
* @return Pairs |
60
|
|
|
*/ |
61
|
|
|
public function withSeparator(?string $separator): self |
62
|
|
|
{ |
63
|
|
|
$clone = clone $this; |
64
|
|
|
$clone->separator = $separator; |
65
|
|
|
return $clone; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param bool $decodeKeys |
70
|
|
|
* @return Pairs |
71
|
|
|
*/ |
72
|
|
|
public function withDecodeKeys(bool $decodeKeys): self |
73
|
|
|
{ |
74
|
|
|
$clone = clone $this; |
75
|
|
|
$clone->decodeKeys = $decodeKeys; |
76
|
|
|
return $clone; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param bool $decodeValues |
81
|
|
|
* @return Pairs |
82
|
|
|
*/ |
83
|
|
|
public function withDecodeValues(bool $decodeValues): self |
84
|
|
|
{ |
85
|
|
|
$clone = clone $this; |
86
|
|
|
$clone->decodeValues = $decodeValues; |
87
|
|
|
return $clone; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Traversable |
92
|
|
|
*/ |
93
|
|
|
public function getIterator(): Traversable |
94
|
|
|
{ |
95
|
|
|
$separator = $this->separator ?? $this->queryString->getRenderer()->getSeparator() ?? 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
|
|
|
|