InputBag.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;
  13. /**
  14. * InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
  15. *
  16. * @template TInput of string|int|float|bool|null
  17. *
  18. * @author Saif Eddin Gmati <azjezz@protonmail.com>
  19. */
  20. final class InputBag extends ParameterBag
  21. {
  22. /**
  23. * Returns a scalar input value by name.
  24. *
  25. * @template TDefault of string|int|float|bool|null
  26. *
  27. * @param TDefault $default The default value if the input key does not exist
  28. *
  29. * @return TDefault|TInput
  30. *
  31. * @throws BadRequestException if the input contains a non-scalar value
  32. */
  33. public function get(string $key, mixed $default = null): string|int|float|bool|null
  34. {
  35. if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
  36. throw new \InvalidArgumentException(\sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
  37. }
  38. $value = parent::get($key, $this);
  39. if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
  40. throw new BadRequestException(\sprintf('Input value "%s" contains a non-scalar value.', $key));
  41. }
  42. return $this === $value ? $default : $value;
  43. }
  44. /**
  45. * Replaces the current input values by a new set.
  46. */
  47. public function replace(array $inputs = []): void
  48. {
  49. $this->parameters = [];
  50. $this->add($inputs);
  51. }
  52. /**
  53. * Adds input values.
  54. */
  55. public function add(array $inputs = []): void
  56. {
  57. foreach ($inputs as $input => $value) {
  58. $this->set($input, $value);
  59. }
  60. }
  61. /**
  62. * Sets an input by name.
  63. *
  64. * @param string|int|float|bool|array|null $value
  65. */
  66. public function set(string $key, mixed $value): void
  67. {
  68. if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
  69. throw new \InvalidArgumentException(\sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
  70. }
  71. $this->parameters[$key] = $value;
  72. }
  73. /**
  74. * Returns the parameter value converted to an enum.
  75. *
  76. * @template T of \BackedEnum
  77. *
  78. * @param class-string<T> $class
  79. * @param ?T $default
  80. *
  81. * @return ?T
  82. *
  83. * @psalm-return ($default is null ? T|null : T)
  84. *
  85. * @throws BadRequestException if the input cannot be converted to an enum
  86. */
  87. public function getEnum(string $key, string $class, ?\BackedEnum $default = null): ?\BackedEnum
  88. {
  89. try {
  90. return parent::getEnum($key, $class, $default);
  91. } catch (UnexpectedValueException $e) {
  92. throw new BadRequestException($e->getMessage(), $e->getCode(), $e);
  93. }
  94. }
  95. /**
  96. * Returns the parameter value converted to string.
  97. *
  98. * @throws BadRequestException if the input contains a non-scalar value
  99. */
  100. public function getString(string $key, string $default = ''): string
  101. {
  102. // Shortcuts the parent method because the validation on scalar is already done in get().
  103. return (string) $this->get($key, $default);
  104. }
  105. /**
  106. * @throws BadRequestException if the input value is an array and \FILTER_REQUIRE_ARRAY or \FILTER_FORCE_ARRAY is not set
  107. * @throws BadRequestException if the input value is invalid and \FILTER_NULL_ON_FAILURE is not set
  108. */
  109. public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
  110. {
  111. $value = $this->has($key) ? $this->all()[$key] : $default;
  112. // Always turn $options into an array - this allows filter_var option shortcuts.
  113. if (!\is_array($options) && $options) {
  114. $options = ['flags' => $options];
  115. }
  116. if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) {
  117. throw new BadRequestException(\sprintf('Input value "%s" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.', $key));
  118. }
  119. if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
  120. 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)));
  121. }
  122. $options['flags'] ??= 0;
  123. $nullOnFailure = $options['flags'] & \FILTER_NULL_ON_FAILURE;
  124. $options['flags'] |= \FILTER_NULL_ON_FAILURE;
  125. $value = filter_var($value, $filter, $options);
  126. if (null !== $value || $nullOnFailure) {
  127. return $value;
  128. }
  129. throw new BadRequestException(\sprintf('Input value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key));
  130. }
  131. }