InputBag.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * @return string|int|float|bool|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-scalar value as 2nd argument to "%s()" is deprecated, pass a scalar 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($key)" instead.', __METHOD__, BadRequestException::class, __CLASS__);
  34. }
  35. return $this === $value ? $default : $value;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function all(string $key = null): array
  41. {
  42. return parent::all($key);
  43. }
  44. /**
  45. * Replaces the current input values by a new set.
  46. */
  47. public function replace(array $inputs = [])
  48. {
  49. $this->parameters = [];
  50. $this->add($inputs);
  51. }
  52. /**
  53. * Adds input values.
  54. */
  55. public function add(array $inputs = [])
  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, $value)
  67. {
  68. if (null !== $value && !is_scalar($value) && !\is_array($value) && !method_exists($value, '__toString')) {
  69. trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a scalar, array, or null instead.', get_debug_type($value), __METHOD__);
  70. }
  71. $this->parameters[$key] = $value;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
  77. {
  78. $value = $this->has($key) ? $this->all()[$key] : $default;
  79. // Always turn $options into an array - this allows filter_var option shortcuts.
  80. if (!\is_array($options) && $options) {
  81. $options = ['flags' => $options];
  82. }
  83. if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) {
  84. 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__);
  85. if (!isset($options['flags'])) {
  86. $options['flags'] = \FILTER_REQUIRE_ARRAY;
  87. }
  88. }
  89. if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
  90. trigger_deprecation('symfony/http-foundation', '5.2', 'Not passing a Closure together with FILTER_CALLBACK to "%s()" is deprecated. Wrap your filter in a closure instead.', __METHOD__);
  91. // 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)));
  92. }
  93. return filter_var($value, $filter, $options);
  94. }
  95. }