InputBag.php 5.0 KB

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