InputBag.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Exception\BadRequestException;
  12. /**
  13. * InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
  14. *
  15. * @author Saif Eddin Gmati <azjezz@protonmail.com>
  16. */
  17. final class InputBag extends ParameterBag
  18. {
  19. /**
  20. * Returns a scalar input value by name.
  21. *
  22. * @param string|int|float|bool|null $default The default value if the input key does not exist
  23. */
  24. public function get(string $key, mixed $default = null): string|int|float|bool|null
  25. {
  26. if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
  27. throw new \InvalidArgumentException(sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
  28. }
  29. $value = parent::get($key, $this);
  30. if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
  31. throw new BadRequestException(sprintf('Input value "%s" contains a non-scalar value.', $key));
  32. }
  33. return $this === $value ? $default : $value;
  34. }
  35. /**
  36. * Replaces the current input values by a new set.
  37. */
  38. public function replace(array $inputs = [])
  39. {
  40. $this->parameters = [];
  41. $this->add($inputs);
  42. }
  43. /**
  44. * Adds input values.
  45. */
  46. public function add(array $inputs = [])
  47. {
  48. foreach ($inputs as $input => $value) {
  49. $this->set($input, $value);
  50. }
  51. }
  52. /**
  53. * Sets an input by name.
  54. *
  55. * @param string|int|float|bool|array|null $value
  56. */
  57. public function set(string $key, mixed $value)
  58. {
  59. if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
  60. throw new \InvalidArgumentException(sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
  61. }
  62. $this->parameters[$key] = $value;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
  68. {
  69. $value = $this->has($key) ? $this->all()[$key] : $default;
  70. // Always turn $options into an array - this allows filter_var option shortcuts.
  71. if (!\is_array($options) && $options) {
  72. $options = ['flags' => $options];
  73. }
  74. if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) {
  75. throw new BadRequestException(sprintf('Input value "%s" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.', $key));
  76. }
  77. if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
  78. throw new \InvalidArgumentException(sprintf('A Closure must be passed to "%s()" when FILTER_CALLBACK is used, "%s" given.', __METHOD__, get_debug_type($options['options'] ?? null)));
  79. }
  80. return filter_var($value, $filter, $options);
  81. }
  82. }