| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
use Symfony\Component\HttpFoundation\Exception\BadRequestException; |
| 15 |
use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
final class InputBag extends ParameterBag |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public function get(string $key, mixed $default = null): string|int|float|bool|null |
| 38 |
{ |
| 39 |
if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) { |
| 40 |
throw new \InvalidArgumentException(\sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default))); |
| 41 |
} |
| 42 |
|
| 43 |
$value = parent::get($key, $this); |
| 44 |
|
| 45 |
if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) { |
| 46 |
throw new BadRequestException(\sprintf('Input value "%s" contains a non-scalar value.', $key)); |
| 47 |
} |
| 48 |
|
| 49 |
return $this === $value ? $default : $value; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function replace(array $inputs = []): void |
| 56 |
{ |
| 57 |
$this->parameters = []; |
| 58 |
$this->add($inputs); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function add(array $inputs = []): void |
| 65 |
{ |
| 66 |
foreach ($inputs as $input => $value) { |
| 67 |
$this->set($input, $value); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function set(string $key, mixed $value): void |
| 77 |
{ |
| 78 |
if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) { |
| 79 |
throw new \InvalidArgumentException(\sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value))); |
| 80 |
} |
| 81 |
|
| 82 |
$this->parameters[$key] = $value; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function getEnum(string $key, string $class, ?\BackedEnum $default = null): ?\BackedEnum |
| 100 |
{ |
| 101 |
try { |
| 102 |
return parent::getEnum($key, $class, $default); |
| 103 |
} catch (UnexpectedValueException $e) { |
| 104 |
throw new BadRequestException($e->getMessage(), $e->getCode(), $e); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function getString(string $key, string $default = ''): string |
| 114 |
{ |
| 115 |
|
| 116 |
return (string) $this->get($key, $default); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed |
| 124 |
{ |
| 125 |
$value = $this->has($key) ? $this->all()[$key] : $default; |
| 126 |
|
| 127 |
|
| 128 |
if (!\is_array($options) && $options) { |
| 129 |
$options = ['flags' => $options]; |
| 130 |
} |
| 131 |
|
| 132 |
if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) { |
| 133 |
throw new BadRequestException(\sprintf('Input value "%s" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.', $key)); |
| 134 |
} |
| 135 |
|
| 136 |
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) { |
| 137 |
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))); |
| 138 |
} |
| 139 |
|
| 140 |
$options['flags'] ??= 0; |
| 141 |
$nullOnFailure = $options['flags'] & \FILTER_NULL_ON_FAILURE; |
| 142 |
$options['flags'] |= \FILTER_NULL_ON_FAILURE; |
| 143 |
|
| 144 |
$value = filter_var($value, $filter, $options); |
| 145 |
|
| 146 |
if (null !== $value || $nullOnFailure) { |
| 147 |
return $value; |
| 148 |
} |
| 149 |
|
| 150 |
throw new BadRequestException(\sprintf('Input value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key)); |
| 151 |
} |
| 152 |
} |