|
| 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 | +use Symfony\Component\HttpFoundation\Exception\BadRequestException; |
| 15 | + |
| 16 | +/** |
| 17 | + * InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE. |
| 18 | + * |
| 19 | + * @author Saif Eddin Gmati <saif.gmati@symfony.com> |
| 20 | + */ |
| 21 | +final class InputBag extends ParameterBag |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Returns a string input value by name. |
| 25 | + * |
| 26 | + * @param string|null $default The default value if the input key does not exist |
| 27 | + * |
| 28 | + * @return string|null |
| 29 | + */ |
| 30 | + public function get(string $key, $default = null) |
| 31 | + { |
| 32 | + if (null !== $default && !is_scalar($default) && !method_exists($default, '__toString')) { |
| 33 | + trigger_deprecation('symfony/http-foundation', '5.1', 'Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead.', __METHOD__); |
| 34 | + } |
| 35 | + |
| 36 | + $value = parent::get($key, $this); |
| 37 | + |
| 38 | + if (null !== $value && $this !== $value && !is_scalar($value) && !method_exists($value, '__toString')) { |
| 39 | + trigger_deprecation('symfony/http-foundation', '5.1', 'Retrieving a non-string value from "%s()" is deprecated, and will throw a "%s" exception in Symfony 6.0, use "%s::all()" instead.', __METHOD__, BadRequestException::class, __CLASS__); |
| 40 | + } |
| 41 | + |
| 42 | + return $this === $value ? $default : $value; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the inputs. |
| 47 | + * |
| 48 | + * @param string|null $key The name of the input to return or null to get them all |
| 49 | + */ |
| 50 | + public function all(string $key = null): array |
| 51 | + { |
| 52 | + if (null === $key) { |
| 53 | + return $this->parameters; |
| 54 | + } |
| 55 | + |
| 56 | + $value = $this->parameters[$key] ?? []; |
| 57 | + if (!\is_array($value)) { |
| 58 | + throw new BadRequestException(sprintf('Unexpected value for "%s" input, expecting "array", got "%s".', $key, get_debug_type($value))); |
| 59 | + } |
| 60 | + |
| 61 | + return $value; |
| 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|array $value |
| 87 | + */ |
| 88 | + public function set(string $key, $value) |
| 89 | + { |
| 90 | + if (!is_scalar($value) && !method_exists($value, '__toString') && !\is_array($value)) { |
| 91 | + trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string or an array instead.', get_debug_type($value), __METHOD__); |
| 92 | + } |
| 93 | + |
| 94 | + $this->parameters[$key] = $value; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritdoc} |
| 99 | + */ |
| 100 | + public function filter(string $key, $default = null, int $filter = FILTER_DEFAULT, $options = []) |
| 101 | + { |
| 102 | + $value = $this->has($key) ? $this->all()[$key] : $default; |
| 103 | + |
| 104 | + // Always turn $options into an array - this allows filter_var option shortcuts. |
| 105 | + if (!\is_array($options) && $options) { |
| 106 | + $options = ['flags' => $options]; |
| 107 | + } |
| 108 | + |
| 109 | + if (\is_array($value) && !(($options['flags'] ?? 0) & (FILTER_REQUIRE_ARRAY | FILTER_FORCE_ARRAY))) { |
| 110 | + trigger_deprecation('symfony/http-foundation', '5.1', 'Filtering an array value with "%s()" without passing the FILTER_REQUIRE_ARRAY or FILTER_FORCE_ARRAY flag is deprecated', __METHOD__); |
| 111 | + |
| 112 | + if (!isset($options['flags'])) { |
| 113 | + $options['flags'] = FILTER_REQUIRE_ARRAY; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return filter_var($value, $filter, $options); |
| 118 | + } |
| 119 | +} |
0 commit comments