1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
9
|
|
|
* @version 4.1.1 |
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
11
|
|
|
*/ |
12
|
|
|
namespace League\Uri\Components; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use League\Uri\Interfaces\Query as QueryInterface; |
16
|
|
|
use League\Uri\QueryParser; |
17
|
|
|
use League\Uri\Types\ImmutableCollectionTrait; |
18
|
|
|
use League\Uri\Types\ImmutableComponentTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Value object representing a URI query component. |
22
|
|
|
* |
23
|
|
|
* @package League.uri |
24
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
*/ |
27
|
|
|
class Query implements QueryInterface |
28
|
|
|
{ |
29
|
|
|
use ImmutableComponentTrait; |
30
|
|
|
|
31
|
|
|
use ImmutableCollectionTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Key/pair separator character |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected static $separator = '&'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* a new instance |
42
|
|
|
* |
43
|
|
|
* @param string $data |
44
|
|
|
*/ |
45
|
754 |
|
public function __construct($data = null) |
46
|
|
|
{ |
47
|
754 |
|
if (null !== $data) { |
48
|
607 |
|
$this->data = $this->validate($data); |
49
|
404 |
|
} |
50
|
754 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* sanitize the submitted data |
54
|
|
|
* |
55
|
|
|
* @param string $str |
56
|
|
|
* |
57
|
|
|
* @throws InvalidArgumentException If reserved characters are used |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
607 |
|
protected function validate($str) |
62
|
|
|
{ |
63
|
607 |
|
$str = $this->validateString($str); |
64
|
607 |
|
if (strpos($str, '#') !== false) { |
65
|
3 |
|
throw new InvalidArgumentException('the query string must not contain a URI fragment'); |
66
|
|
|
} |
67
|
|
|
|
68
|
607 |
|
return (new QueryParser())->parse($str, static::$separator, false); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* return a new Query instance from an Array or a traversable object |
73
|
|
|
* |
74
|
|
|
* @param \Traversable|array $data |
75
|
|
|
* |
76
|
|
|
* @return static |
77
|
|
|
*/ |
78
|
102 |
|
public static function createFromArray($data) |
79
|
|
|
{ |
80
|
102 |
|
$query = (new QueryParser())->build( |
81
|
102 |
|
static::validateIterator($data), |
82
|
96 |
|
static::$separator, |
83
|
32 |
|
PHP_QUERY_RFC3986 |
84
|
64 |
|
); |
85
|
|
|
|
86
|
96 |
|
return new static($query); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
*/ |
92
|
2 |
|
public function __debugInfo() |
93
|
|
|
{ |
94
|
2 |
|
return ['query' => $this->__toString()]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
*/ |
100
|
12 |
|
public static function __set_state(array $properties) |
101
|
|
|
{ |
102
|
12 |
|
return static::createFromArray($properties['data']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the instance string representation; If the |
107
|
|
|
* instance is not defined an empty string is returned |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
661 |
|
public function __toString() |
112
|
|
|
{ |
113
|
661 |
|
return (new QueryParser())->build($this->data, static::$separator, PHP_QUERY_RFC3986); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns the instance string representation |
118
|
|
|
* with its optional URI delimiters |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
611 |
|
public function getUriComponent() |
123
|
|
|
{ |
124
|
611 |
|
$query = $this->__toString(); |
125
|
611 |
|
if ('' !== $query) { |
126
|
443 |
|
$query = QueryInterface::DELIMITER.$query; |
127
|
295 |
|
} |
128
|
|
|
|
129
|
611 |
|
return $query; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Retrieves a single query parameter. |
134
|
|
|
* |
135
|
|
|
* Retrieves a single query parameter. If the parameter has not been set, |
136
|
|
|
* returns the default value provided. |
137
|
|
|
* |
138
|
|
|
* @param string $offset the parameter name |
139
|
|
|
* @param mixed $default Default value to return if the parameter does not exist. |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
9 |
|
public function getValue($offset, $default = null) |
144
|
|
|
{ |
145
|
9 |
|
$offset = rawurldecode($offset); |
146
|
9 |
|
if (isset($this->data[$offset])) { |
147
|
3 |
|
return $this->data[$offset]; |
148
|
|
|
} |
149
|
|
|
|
150
|
6 |
|
return $default; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns an instance merge with the specified query |
155
|
|
|
* |
156
|
|
|
* This method MUST retain the state of the current instance, and return |
157
|
|
|
* an instance that contains the modified query |
158
|
|
|
* |
159
|
|
|
* @param Query|string $query the data to be merged query can be |
160
|
|
|
* - another Interfaces\Query object |
161
|
|
|
* - a string or a Stringable object |
162
|
|
|
* |
163
|
|
|
* @return static |
164
|
|
|
*/ |
165
|
24 |
|
public function merge($query) |
166
|
|
|
{ |
167
|
24 |
|
if (!$query instanceof QueryInterface) { |
168
|
6 |
|
$query = static::createFromArray($this->validate($query)); |
169
|
4 |
|
} |
170
|
|
|
|
171
|
24 |
|
if ($this->sameValueAs($query)) { |
172
|
3 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
21 |
|
return static::createFromArray(array_merge($this->toArray(), $query->toArray())); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Sort the query string by offset, maintaining offset to data correlations. |
180
|
|
|
* |
181
|
|
|
* This method MUST retain the state of the current instance, and return |
182
|
|
|
* an instance that contains the modified query |
183
|
|
|
* |
184
|
|
|
* @param callable|int $sort a PHP sort flag constant or a comparaison function |
185
|
|
|
* which must return an integer less than, equal to, |
186
|
|
|
* or greater than zero if the first argument is |
187
|
|
|
* considered to be respectively less than, equal to, |
188
|
|
|
* or greater than the second. |
189
|
|
|
* |
190
|
|
|
* @return static |
191
|
|
|
*/ |
192
|
45 |
|
public function ksort($sort = SORT_REGULAR) |
193
|
|
|
{ |
194
|
45 |
|
$func = is_callable($sort) ? 'uksort' : 'ksort'; |
195
|
45 |
|
$data = $this->data; |
196
|
45 |
|
$func($data, $sort); |
197
|
45 |
|
if ($data === $this->data) { |
198
|
33 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
12 |
|
return static::createFromArray($data); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Return a new instance when needed |
206
|
|
|
* |
207
|
|
|
* @param array $data |
208
|
|
|
* |
209
|
|
|
* @return static |
210
|
|
|
*/ |
211
|
42 |
|
protected function newCollectionInstance(array $data) |
212
|
|
|
{ |
213
|
42 |
|
if ($data == $this->data) { |
214
|
15 |
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
27 |
|
return static::createFromArray($data); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|