|
28 | 28 | use function array_keys; |
29 | 29 | use function get_object_vars; |
30 | 30 | use function http_build_query; |
| 31 | +use function implode; |
31 | 32 | use function is_array; |
32 | 33 | use function is_object; |
33 | 34 | use function is_resource; |
34 | 35 | use function is_scalar; |
35 | 36 | use function rawurldecode; |
| 37 | +use function str_replace; |
36 | 38 | use function strpos; |
37 | 39 | use function substr; |
38 | 40 |
|
@@ -258,39 +260,62 @@ private static function isRecursive(array &$arr): bool |
258 | 260 | } |
259 | 261 |
|
260 | 262 | /** |
261 | | - * Parses the query string like parse_str without mangling the results. |
| 263 | + * Parses the query string. |
| 264 | + * |
| 265 | + * The result depends on the query parsing mode |
262 | 266 | * |
263 | 267 | * @see QueryString::extractFromValue() |
264 | | - * @see http://php.net/parse_str |
265 | | - * @see https://wiki.php.net/rfc/on_demand_name_mangling |
266 | 268 | * |
267 | 269 | * @param non-empty-string $separator |
268 | 270 | * |
269 | 271 | * @throws SyntaxError |
270 | 272 | */ |
271 | | - public static function extract(Stringable|string|bool|null $query, string $separator = '&', int $encType = PHP_QUERY_RFC3986): array |
272 | | - { |
273 | | - return self::extractFromValue($query, Converter::fromEncodingType($encType)->withSeparator($separator)); |
| 273 | + public static function extract( |
| 274 | + Stringable|string|bool|null $query, |
| 275 | + string $separator = '&', |
| 276 | + int $encType = PHP_QUERY_RFC3986, |
| 277 | + QueryParsingMode $queryParsingMode = QueryParsingMode::Unmangled, |
| 278 | + ): array { |
| 279 | + return self::extractFromValue( |
| 280 | + $query, |
| 281 | + Converter::fromEncodingType($encType)->withSeparator($separator), |
| 282 | + $queryParsingMode, |
| 283 | + ); |
274 | 284 | } |
275 | 285 |
|
276 | 286 | /** |
277 | | - * Parses the query string like parse_str without mangling the results. |
| 287 | + * Parses the query string. |
278 | 288 | * |
279 | | - * The result is similar as PHP parse_str when used with its |
280 | | - * second argument with the difference that variable names are |
281 | | - * not mangled. |
282 | | - * |
283 | | - * @see http://php.net/parse_str |
284 | | - * @see https://wiki.php.net/rfc/on_demand_name_mangling |
| 289 | + * The result depends on the query parsing mode |
285 | 290 | * |
286 | 291 | * @throws SyntaxError |
287 | 292 | */ |
288 | | - public static function extractFromValue(Stringable|string|bool|null $query, ?Converter $converter = null): array |
289 | | - { |
290 | | - return self::convert(self::decodePairs( |
291 | | - ($converter ?? Converter::fromRFC3986())->toPairs($query), |
292 | | - self::PAIR_VALUE_PRESERVED |
293 | | - )); |
| 293 | + public static function extractFromValue( |
| 294 | + Stringable|string|bool|null $query, |
| 295 | + ?Converter $converter = null, |
| 296 | + QueryParsingMode $queryParsingMode = QueryParsingMode::Unmangled, |
| 297 | + ): array { |
| 298 | + $pairs = ($converter ?? Converter::fromRFC3986())->toPairs($query); |
| 299 | + if (QueryParsingMode::Native === $queryParsingMode) { |
| 300 | + if ([] === $pairs) { |
| 301 | + return []; |
| 302 | + } |
| 303 | + |
| 304 | + $data = []; |
| 305 | + foreach ($pairs as [$key, $value]) { |
| 306 | + $key = str_replace('&', '%26', (string) $key); |
| 307 | + $data[] = null === $value ? $key : $key.'='.str_replace('&', '%26', $value); |
| 308 | + } |
| 309 | + |
| 310 | + parse_str(implode('&', $data), $result); |
| 311 | + |
| 312 | + return $result; |
| 313 | + } |
| 314 | + |
| 315 | + return self::convert( |
| 316 | + self::decodePairs($pairs, self::PAIR_VALUE_PRESERVED), |
| 317 | + $queryParsingMode |
| 318 | + ); |
294 | 319 | } |
295 | 320 |
|
296 | 321 | /** |
@@ -349,11 +374,11 @@ private static function decodePairs(array $pairs, int $pairValueState): array |
349 | 374 | * Converts a collection of key/value pairs and returns |
350 | 375 | * the store PHP variables as elements of an array. |
351 | 376 | */ |
352 | | - public static function convert(iterable $pairs): array |
| 377 | + public static function convert(iterable $pairs, QueryParsingMode $queryParsingMode = QueryParsingMode::Unmangled): array |
353 | 378 | { |
354 | 379 | $returnedValue = []; |
355 | 380 | foreach ($pairs as $pair) { |
356 | | - $returnedValue = self::extractPhpVariable($returnedValue, $pair); |
| 381 | + $returnedValue = self::extractPhpVariable($returnedValue, $pair, queryParsingMode: $queryParsingMode); |
357 | 382 | } |
358 | 383 |
|
359 | 384 | return $returnedValue; |
@@ -384,11 +409,17 @@ public static function convert(iterable $pairs): array |
384 | 409 | * @param array|string $name the pair key |
385 | 410 | * @param string $value the pair value |
386 | 411 | */ |
387 | | - private static function extractPhpVariable(array $data, array|string $name, string $value = ''): array |
388 | | - { |
| 412 | + private static function extractPhpVariable( |
| 413 | + array $data, |
| 414 | + array|string $name, |
| 415 | + ?string $value = '', |
| 416 | + QueryParsingMode $queryParsingMode = QueryParsingMode::Unmangled |
| 417 | + ): array { |
389 | 418 | if (is_array($name)) { |
390 | 419 | [$name, $value] = $name; |
391 | | - $value = rawurldecode((string) $value); |
| 420 | + if (null !== $value || QueryParsingMode::PreserveNull !== $queryParsingMode) { |
| 421 | + $value = rawurldecode((string) $value); |
| 422 | + } |
392 | 423 | } |
393 | 424 |
|
394 | 425 | if ('' === $name) { |
@@ -430,7 +461,7 @@ private static function extractPhpVariable(array $data, array|string $name, stri |
430 | 461 | return $data; |
431 | 462 | } |
432 | 463 |
|
433 | | - $data[$key] = self::extractPhpVariable($data[$key], $name, $value); |
| 464 | + $data[$key] = self::extractPhpVariable($data[$key], $name, $value, $queryParsingMode); |
434 | 465 |
|
435 | 466 | return $data; |
436 | 467 | } |
|
0 commit comments