InputBag.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <saif.gmati@symfony.com>
  16. */
  17. final class InputBag extends ParameterBag
  18. {
  19. /**
  20. * Returns a string input value by name.
  21. *
  22. * @param string|null $default The default value if the input key does not exist
  23. *
  24. * @return string|null
  25. */
  26. public function get(string $key, $default = null)
  27. {
  28. if (null !== $default && !is_scalar($default) && !(\is_object($default) && method_exists($default, '__toString'))) {
  29. 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__);
  30. }
  31. $value = parent::get($key, $this);
  32. if (null !== $value && $this !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
  33. 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__);
  34. }
  35. return $this === $value ? $default : $value;
  36. }
  37. /**
  38. * Returns the inputs.
  39. *
  40. * @param string|null $key The name of the input to return or null to get them all
  41. */
  42. public function all(string $key = null): array
  43. {
  44. if (null === $key) {
  45. return $this->parameters;
  46. }
  47. $value = $this->parameters[$key] ?? [];
  48. if (!\is_array($value)) {
  49. throw new BadRequestException(sprintf('Unexpected value for "%s" input, expecting "array", got "%s".', $key, get_debug_type($value)));
  50. }
  51. return $value;
  52. }
  53. /**
  54. * Replaces the current input values by a new set.
  55. */
  56. public function replace(array $inputs = [])
  57. {
  58. $this->parameters = [];
  59. $this->add($inputs);
  60. }
  61. /**
  62. * Adds input values.
  63. */
  64. public function add(array $inputs = [])
  65. {
  66. foreach ($inputs as $input => $value) {
  67. $this->set($input, $value);
  68. }
  69. }
  70. /**
  71. * Sets an input by name.
  72. *
  73. * @param string|array $value
  74. */
  75. public function set(string $key, $value)
  76. {
  77. if (!is_scalar($value) && !\is_array($value) && !method_exists($value, '__toString')) {
  78. 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__);
  79. }
  80. $this->parameters[$key] = $value;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function filter(string $key, $default = null, int $filter = FILTER_DEFAULT, $options = [])
  86. {
  87. $value = $this->has($key) ? $this->all()[$key] : $default;
  88. // Always turn $options into an array - this allows filter_var option shortcuts.
  89. if (!\is_array($options) && $options) {
  90. $options = ['flags' => $options];
  91. }
  92. if (\is_array($value) && !(($options['flags'] ?? 0) & (FILTER_REQUIRE_ARRAY | FILTER_FORCE_ARRAY))) {
  93. 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__);
  94. if (!isset($options['flags'])) {
  95. $options['flags'] = FILTER_REQUIRE_ARRAY;
  96. }
  97. }
  98. return filter_var($value, $filter, $options);
  99. }
  100. }