|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\HttpFoundation; |
| 13 | + |
| 14 | +/** |
| 15 | + * InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE. |
| 16 | + * |
| 17 | + * @author Saif Eddin Gmati <saif.gmati@symfony.com> |
| 18 | + */ |
| 19 | +final class InputBag extends ParameterBag |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Returns a string input value by name. |
| 23 | + * |
| 24 | + * @param string|null $default The default value if the input key does not exist |
| 25 | + * |
| 26 | + * @return string|null |
| 27 | + */ |
| 28 | + public function get(string $key, $default = null) |
| 29 | + { |
| 30 | + if (null !== $default && !\is_string($default)) { |
| 31 | + @trigger_error(sprintf('Passing a non-string value as 2nd argument to "%s::%s()" is deprecated since Symfony 5.1, pass a string or null instead.', static::class, __METHOD__), E_USER_DEPRECATED); |
| 32 | + } |
| 33 | + |
| 34 | + $value = parent::get($key, $default); |
| 35 | + |
| 36 | + if (null !== $value && $default !== $value && !\is_string($value)) { |
| 37 | + @trigger_error(sprintf('Retrieving a non-string value from "%s::%s()" is deprecated since Symfony 5.1, use "%s::getAll()" instead.', static::class, __METHOD__, static::class), E_USER_DEPRECATED); |
| 38 | + } |
| 39 | + |
| 40 | + return $value; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the headers. |
| 45 | + * |
| 46 | + * @param string|null $key The name of the input to return or null to get them all |
| 47 | + * |
| 48 | + * @return array An array of input |
| 49 | + */ |
| 50 | + public function all(string $key = null): array |
| 51 | + { |
| 52 | + if (null !== $key) { |
| 53 | + $value = $this->parameters[$key] ?? []; |
| 54 | + if (!\is_array($value)) { |
| 55 | + throw new Exception\BadRequestException(sprintf('Unexpected value for %s input.', $key)); |
| 56 | + } |
| 57 | + |
| 58 | + return $value; |
| 59 | + } |
| 60 | + |
| 61 | + return $this->parameters; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Replaces the current input values by a new set. |
| 66 | + */ |
| 67 | + public function replace(array $inputs = []) |
| 68 | + { |
| 69 | + $this->parameters = []; |
| 70 | + $this->add($inputs); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Adds input values. |
| 75 | + */ |
| 76 | + public function add(array $inputs = []) |
| 77 | + { |
| 78 | + foreach ($inputs as $input => $value) { |
| 79 | + $this->set($input, $value); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Sets an input by name. |
| 85 | + * |
| 86 | + * @param string|string[] $value The value |
| 87 | + */ |
| 88 | + public function set(string $key, $value) |
| 89 | + { |
| 90 | + if (!\is_string($value) && !\is_array($value)) { |
| 91 | + @trigger_error(sprintf('Passing "%s" as a 2nd Argument to "%s::%s()" is deprecated since Symfony 5.1, pass a string or an array instead.', \is_object($value) ? \get_class($value) : \gettype($value), static::class, __METHOD__), E_USER_DEPRECATED); |
| 92 | + } |
| 93 | + |
| 94 | + $this->parameters[$key] = $value; |
| 95 | + } |
| 96 | +} |
0 commit comments