-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathQueryString.php
More file actions
495 lines (423 loc) · 17 KB
/
QueryString.php
File metadata and controls
495 lines (423 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Uri;
use BackedEnum;
use League\Uri\Exceptions\SyntaxError;
use League\Uri\KeyValuePair\Converter;
use ReflectionEnum;
use ReflectionException;
use SplObjectStorage;
use Stringable;
use TypeError;
use UnitEnum;
use ValueError;
use function array_is_list;
use function array_key_exists;
use function array_keys;
use function get_debug_type;
use function get_object_vars;
use function http_build_query;
use function implode;
use function is_array;
use function is_object;
use function is_resource;
use function is_scalar;
use function rawurldecode;
use function str_replace;
use function strpos;
use function substr;
use const PHP_QUERY_RFC1738;
use const PHP_QUERY_RFC3986;
/**
* A class to parse the URI query string.
*
* @see https://tools.ietf.org/html/rfc3986#section-3.4
*/
final class QueryString
{
private const PAIR_VALUE_DECODED = 1;
private const PAIR_VALUE_PRESERVED = 2;
private const RECURSION_MARKER = "\0__RECURSION_INTERNAL_MARKER__\0";
/**
* @codeCoverageIgnore
*/
private function __construct()
{
}
/**
* Build a query string from a list of pairs.
*
* @see QueryString::buildFromPairs()
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
*
* @param iterable<array{0:string, 1:mixed}> $pairs
* @param non-empty-string $separator
*
* @throws SyntaxError If the encoding type is invalid
* @throws SyntaxError If a pair is invalid
*/
public static function build(iterable $pairs, string $separator = '&', int $encType = PHP_QUERY_RFC3986, StringCoercionMode $coercionMode = StringCoercionMode::Native): ?string
{
return self::buildFromPairs($pairs, Converter::fromEncodingType($encType)->withSeparator($separator), $coercionMode);
}
/**
* Build a query string from a list of pairs.
*
* The method expects the return value from Query::parse to build
* a valid query string. This method differs from PHP http_build_query as
* it does not modify parameters keys.
*
* If a reserved character is found in a URI component and
* no delimiting role is known for that character, then it must be
* interpreted as representing the data octet corresponding to that
* character's encoding in US-ASCII.
*
* @see https://datatracker.ietf.org/doc/html/rfc3986#section-2.2
*
* @param iterable<array{0:string, 1:mixed}> $pairs
*
* @throws SyntaxError If the encoding type is invalid
* @throws SyntaxError If a pair is invalid
*/
public static function buildFromPairs(iterable $pairs, ?Converter $converter = null, StringCoercionMode $coercionMode = StringCoercionMode::Native): ?string
{
$keyValuePairs = [];
foreach ($pairs as $pair) {
if (!is_array($pair) || [0, 1] !== array_keys($pair)) {
throw new SyntaxError('A pair must be a sequential array starting at `0` and containing two elements.');
}
[$key, $value] = $pair;
$coercionMode->isCoercible($value) || throw new SyntaxError('Converting a type `'.get_debug_type($value).'` into a string is not supported by the '.(StringCoercionMode::Native === $coercionMode ? 'PHP Native' : 'Ecmascript').' coercion mode.');
try {
$key = $coercionMode->coerce($key);
$value = $coercionMode->coerce($value);
} catch (TypeError $typeError) {
throw new SyntaxError('The pair can not be converted to build a query string.', previous: $typeError);
}
$keyValuePairs[] = [(string) Encoder::encodeQueryKeyValue($key), null === $value ? null : Encoder::encodeQueryKeyValue($value)];
}
return ($converter ?? Converter::fromRFC3986())->toValue($keyValuePairs);
}
/**
* Build a query string from an object or an array like http_build_query without discarding values.
* The method differs from http_build_query for the following behavior:
*
* - if a resource is used, a TypeError is thrown.
* - if a recursion is detected a ValueError is thrown
* - the method preserves value with `null` value (http_build_query) skip the key.
* - the method does not handle prefix usage
*
* @param array<array-key, mixed> $data
* @param non-empty-string $separator
*
* @throws TypeError if a resource is found it the input array
* @throws ValueError if a recursion is detected
*/
public static function compose(
array|object $data,
string $separator = '&',
int $encType = PHP_QUERY_RFC1738,
QueryComposeMode $composeMode = QueryComposeMode::Native
): ?string {
if (QueryComposeMode::Native === $composeMode) {
return http_build_query(data: $data, arg_separator: $separator, encoding_type: $encType);
}
$query = self::composeFromValue($data, Converter::fromEncodingType($encType)->withSeparator($separator), $composeMode);
return QueryComposeMode::Safe !== $composeMode ? (string) $query : $query;
}
public static function composeFromValue(
array|object $data,
?Converter $converter = null,
QueryComposeMode $composeMode = QueryComposeMode::Native,
): ?string {
if (QueryComposeMode::EnumLenient === $composeMode && $data instanceof UnitEnum && !$data instanceof BackedEnum) {
return '';
}
QueryComposeMode::Safe !== $composeMode || is_array($data) || throw new TypeError('In safe mode only arrays are supported.');
$converter ??= Converter::fromRFC3986();
$pairs = QueryComposeMode::Native !== $composeMode
? self::composeRecursive($composeMode, $data)
: self::parseFromValue(http_build_query(data: $data, arg_separator: '&'), Converter::fromRFC1738());
return self::buildFromPairs($pairs, $converter);
}
/**
* @param array<array-key, mixed>|object $data
* @param SplObjectStorage<object, null> $seenObjects
*
* @throws TypeError if a resource is found it the input array
* @throws ValueError if a recursion is detected
* @throws ReflectionException if reflection is not possible on the Enum
*
* @return iterable<array{0: array-key, 1: string|int|float|bool|null}>
*/
private static function composeRecursive(
QueryComposeMode $composeMode,
array|object $data,
string|int $prefix = '',
SplObjectStorage $seenObjects = new SplObjectStorage(),
): iterable {
QueryComposeMode::Safe !== $composeMode || is_array($data) || throw new TypeError('In safe mode only arrays are supported.');
in_array($composeMode, [QueryComposeMode::EnumCompatible, QueryComposeMode::EnumLenient], true) || !$data instanceof UnitEnum || throw new TypeError('Argument #1 ($data) must not be an enum, '.((new ReflectionEnum($data::class))->isBacked() ? 'Backed' : 'Pure').' given') ;
if (is_object($data)) {
if ($seenObjects->contains($data)) {
QueryComposeMode::Safe !== $composeMode || throw new ValueError('composition failed; circular reference detected.');
return;
}
$seenObjects->attach($data);
$data = get_object_vars($data);
}
if (self::hasCircularReference($data)) {
QueryComposeMode::Safe !== $composeMode || throw new ValueError('composition failed; circular reference detected.');
return;
}
$stripIndices = QueryComposeMode::Safe === $composeMode && array_is_list($data);
foreach ($data as $name => $value) {
$name = $stripIndices ? '' : $name;
if ('' !== $prefix) {
$name = $prefix.'['.$name.']';
}
if (is_resource($value)) {
QueryComposeMode::Safe !== $composeMode || throw new TypeError('composition failed; a resource has been detected and can not be converted.');
continue;
}
if (is_scalar($value)) {
yield [$name, $value];
continue;
}
if (null === $value) {
if (QueryComposeMode::Safe === $composeMode) {
yield [$name, $value];
}
continue;
}
if ($value instanceof BackedEnum) {
if (QueryComposeMode::Compatible !== $composeMode) {
yield [$name, $value->value];
continue;
}
$value = get_object_vars($value);
}
if ($value instanceof UnitEnum) {
if (QueryComposeMode::EnumLenient === $composeMode) {
continue;
}
QueryComposeMode::Compatible === $composeMode || throw new TypeError('Unbacked enum '.$value::class.' cannot be converted to a string');
$value = get_object_vars($value);
}
if (QueryComposeMode::Safe === $composeMode && is_object($value)) {
throw new ValueError('In conservative mode only arrays, scalar value or null are supported.');
}
yield from self::composeRecursive($composeMode, $value, $name, $seenObjects);
}
}
/**
* Array recursion detection.
* @see https://stackoverflow.com/questions/9042142/detecting-infinite-array-recursion-in-php
*/
private static function hasCircularReference(array &$arr): bool
{
if (isset($arr[self::RECURSION_MARKER])) {
return true;
}
try {
$arr[self::RECURSION_MARKER] = true;
foreach ($arr as $key => &$value) {
if (self::RECURSION_MARKER !== $key && is_array($value) && self::hasCircularReference($value)) {
return true;
}
}
return false;
} finally {
unset($arr[self::RECURSION_MARKER]);
}
}
/**
* Parses the query string.
*
* The result depends on the query parsing mode
*
* @see QueryString::extractFromValue()
*
* @param non-empty-string $separator
*
* @throws SyntaxError
*/
public static function extract(
BackedEnum|Stringable|string|bool|null $query,
string $separator = '&',
int $encType = PHP_QUERY_RFC3986,
QueryExtractMode $extractMode = QueryExtractMode::Unmangled,
): array {
return self::extractFromValue(
$query,
Converter::fromEncodingType($encType)->withSeparator($separator),
$extractMode,
);
}
/**
* Parses the query string.
*
* The result depends on the query parsing mode
*
* @throws SyntaxError
*/
public static function extractFromValue(
BackedEnum|Stringable|string|bool|null $query,
?Converter $converter = null,
QueryExtractMode $extractMode = QueryExtractMode::Unmangled,
): array {
$pairs = ($converter ?? Converter::fromRFC3986())->toPairs($query);
if (QueryExtractMode::Native === $extractMode) {
if ([] === $pairs) {
return [];
}
$data = [];
foreach ($pairs as [$key, $value]) {
$key = str_replace('&', '%26', (string) $key);
$data[] = null === $value ? $key : $key.'='.str_replace('&', '%26', $value);
}
parse_str(implode('&', $data), $result);
return $result;
}
return self::convert(
self::decodePairs($pairs, self::PAIR_VALUE_PRESERVED),
$extractMode
);
}
/**
* Parses a query string into a collection of key/value pairs.
*
* @param non-empty-string $separator
*
* @throws SyntaxError
*
* @return array<int, array{0:string, 1:string|null}>
*/
public static function parse(BackedEnum|Stringable|string|bool|null $query, string $separator = '&', int $encType = PHP_QUERY_RFC3986): array
{
return self::parseFromValue($query, Converter::fromEncodingType($encType)->withSeparator($separator));
}
/**
* Parses a query string into a collection of key/value pairs.
*
* @throws SyntaxError
*
* @return array<int, array{0:string, 1:string|null}>
*/
public static function parseFromValue(BackedEnum|Stringable|string|bool|null $query, ?Converter $converter = null): array
{
return self::decodePairs(
($converter ?? Converter::fromRFC3986())->toPairs($query),
self::PAIR_VALUE_DECODED
);
}
/**
* @param array<non-empty-list<string|null>> $pairs
*
* @return array<int, array{0:string, 1:string|null}>
*/
private static function decodePairs(array $pairs, int $pairValueState): array
{
$decodePair = static function (array $pair, int $pairValueState): array {
[$key, $value] = $pair;
return match ($pairValueState) {
self::PAIR_VALUE_PRESERVED => [(string) Encoder::decodeAll($key), $value],
default => [(string) Encoder::decodeAll($key), Encoder::decodeAll($value)],
};
};
return array_reduce(
$pairs,
fn (array $carry, array $pair) => [...$carry, $decodePair($pair, $pairValueState)],
[]
);
}
/**
* Converts a collection of key/value pairs and returns
* the store PHP variables as elements of an array.
*/
public static function convert(iterable $pairs, QueryExtractMode $extractMode = QueryExtractMode::Unmangled): array
{
$returnedValue = [];
foreach ($pairs as $pair) {
$returnedValue = self::extractPhpVariable($returnedValue, $pair, extractMode: $extractMode);
}
return $returnedValue;
}
/**
* Parses a query pair like parse_str without mangling the results array keys.
*
* <ul>
* <li>empty name are not saved</li>
* <li>If the value from name is duplicated its corresponding value will be overwritten</li>
* <li>if no "[" is detected the value is added to the return array with the name as index</li>
* <li>if no "]" is detected after detecting a "[" the value is added to the return array with the name as index</li>
* <li>if there's a mismatch in bracket usage the remaining part is dropped</li>
* <li>“.” and “ ” are not converted to “_”</li>
* <li>If there is no “]”, then the first “[” is not converted to becomes an “_”</li>
* <li>no whitespace trimming is done on the key value</li>
* </ul>
*
* @see https://php.net/parse_str
* @see https://wiki.php.net/rfc/on_demand_name_mangling
* @see https://github.com/php/php-src/blob/master/ext/standard/tests/strings/parse_str_basic1.phpt
* @see https://github.com/php/php-src/blob/master/ext/standard/tests/strings/parse_str_basic2.phpt
* @see https://github.com/php/php-src/blob/master/ext/standard/tests/strings/parse_str_basic3.phpt
* @see https://github.com/php/php-src/blob/master/ext/standard/tests/strings/parse_str_basic4.phpt
*
* @param array $data the submitted array
* @param array|string $name the pair key
* @param string $value the pair value
*/
private static function extractPhpVariable(
array $data,
array|string $name,
?string $value = '',
QueryExtractMode $extractMode = QueryExtractMode::Unmangled
): array {
if (is_array($name)) {
[$name, $value] = $name;
if (null !== $value || QueryExtractMode::LossLess !== $extractMode) {
$value = rawurldecode((string) $value);
}
}
if ('' === $name) {
return $data;
}
$leftBracketPosition = strpos($name, '[');
if (false === $leftBracketPosition) {
$data[$name] = $value;
return $data;
}
$rightBracketPosition = strpos($name, ']', $leftBracketPosition);
if (false === $rightBracketPosition) {
$data[$name] = $value;
return $data;
}
$key = substr($name, 0, $leftBracketPosition);
if ('' === $key) {
$key = '0';
}
if (!array_key_exists($key, $data) || !is_array($data[$key])) {
$data[$key] = [];
}
$remaining = substr($name, $rightBracketPosition + 1);
if (!str_starts_with($remaining, '[') || !str_contains($remaining, ']')) {
$remaining = '';
}
$name = substr($name, $leftBracketPosition + 1, $rightBracketPosition - $leftBracketPosition - 1).$remaining;
if ('' === $name) {
$data[$key][] = $value;
return $data;
}
$data[$key] = self::extractPhpVariable($data[$key], $name, $value, $extractMode);
return $data;
}
}