|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Uri; |
| 13 | + |
| 14 | +use Symfony\Component\Uri\Exception\InvalidQueryStringException; |
| 15 | + |
| 16 | +/** |
| 17 | + * @experimental |
| 18 | + * |
| 19 | + * @author Alexandre Daubois <[email protected]> |
| 20 | + */ |
| 21 | +final class QueryString implements \Stringable |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var array<string, string|string[]> |
| 25 | + */ |
| 26 | + private array $parameters = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Parses a URI. |
| 30 | + * |
| 31 | + * Unlike `parse_str()`, this method does not overwrite duplicate keys but instead |
| 32 | + * returns an array of all values for each key: |
| 33 | + * |
| 34 | + * QueryString::parse('foo=1&foo=2&bar=3'); // stored as ['foo' => ['1', '2'], 'bar' => '3'] |
| 35 | + * |
| 36 | + * `+` are supported in parameter keys and not replaced by an underscore: |
| 37 | + * |
| 38 | + * QueryString::parse('foo+bar=1'); // stored as ['foo bar' => '1'] |
| 39 | + * |
| 40 | + * `.` and `_` are supported distinct in parameter keys: |
| 41 | + * |
| 42 | + * QueryString::parse('foo.bar=1'); // stored as ['foo.bar' => '1'] |
| 43 | + * QueryString::parse('foo_bar=1'); // stored as ['foo_bar' => '1'] |
| 44 | + */ |
| 45 | + public static function parse(string $query): QueryString |
| 46 | + { |
| 47 | + $parts = \explode('&', $query); |
| 48 | + $queryString = new self(); |
| 49 | + |
| 50 | + foreach ($parts as $part) { |
| 51 | + if ('' === $part) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + $part = \explode('=', $part, 2); |
| 56 | + $key = \urldecode($part[0]); |
| 57 | + // keys without value will be stored as empty strings, as "parse_str()" does |
| 58 | + $value = isset($part[1]) ? \urldecode($part[1]) : ''; |
| 59 | + |
| 60 | + // take care of nested arrays |
| 61 | + if (preg_match_all('/\[(.*?)\]/', $key, $matches)) { |
| 62 | + $nestedKeys = $matches[1]; |
| 63 | + // nest the value inside the extracted keys |
| 64 | + $value = array_reduce(array_reverse($nestedKeys), static function ($carry, $key) { |
| 65 | + return [$key => $carry]; |
| 66 | + }, $value); |
| 67 | + |
| 68 | + $key = strstr($key, '[', true); |
| 69 | + } |
| 70 | + |
| 71 | + if ($queryString->has($key)) { |
| 72 | + $queryString->set($key, \array_merge((array) $queryString->get($key), (array) $value)); |
| 73 | + } else { |
| 74 | + $queryString->set($key, $value); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $queryString; |
| 79 | + } |
| 80 | + |
| 81 | + public function has(string $key): bool |
| 82 | + { |
| 83 | + return \array_key_exists($key, $this->parameters); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @return null|string|string[] |
| 88 | + */ |
| 89 | + public function get(string $key): string|array|null |
| 90 | + { |
| 91 | + return $this->parameters[$key] ?? null; |
| 92 | + } |
| 93 | + |
| 94 | + public function set(string $key, array|string|null $value): self |
| 95 | + { |
| 96 | + $this->parameters[$key] = $value; |
| 97 | + |
| 98 | + return $this; |
| 99 | + } |
| 100 | + |
| 101 | + public function remove(string $key): self |
| 102 | + { |
| 103 | + unset($this->parameters[$key]); |
| 104 | + |
| 105 | + return $this; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return array<string, string|string[]> |
| 110 | + */ |
| 111 | + public function all(): array |
| 112 | + { |
| 113 | + return $this->parameters; |
| 114 | + } |
| 115 | + |
| 116 | + public function __toString(): string |
| 117 | + { |
| 118 | + $parts = []; |
| 119 | + foreach ($this->parameters as $key => $values) { |
| 120 | + foreach ((array) $values as $value) { |
| 121 | + $parts[] = \urlencode($key).'='.urlencode($value); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return \implode('&', $parts); |
| 126 | + } |
| 127 | +} |
0 commit comments