Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/InputBag.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
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 * InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
19 *
20 * @template TInput of string|int|float|bool|null
21 *
22 * @author Saif Eddin Gmati <[email protected]>
23 */
24 final class InputBag extends ParameterBag
25 {
26 /**
27 * Returns a scalar input value by name.
28 *
29 * @template TDefault of string|int|float|bool|null
30 *
31 * @param TDefault $default The default value if the input key does not exist
32 *
33 * @return TDefault|TInput
34 *
35 * @throws BadRequestException if the input contains a non-scalar value
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 * Replaces the current input values by a new set.
54 */
55 public function replace(array $inputs = []): void
56 {
57 $this->parameters = [];
58 $this->add($inputs);
59 }
60
61 /**
62 * Adds input values.
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 * Sets an input by name.
73 *
74 * @param string|int|float|bool|array|null $value
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 * Returns the parameter value converted to an enum.
87 *
88 * @template T of \BackedEnum
89 *
90 * @param class-string<T> $class
91 * @param ?T $default
92 *
93 * @return ?T
94 *
95 * @psalm-return ($default is null ? T|null : T)
96 *
97 * @throws BadRequestException if the input cannot be converted to an enum
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 * Returns the parameter value converted to string.
110 *
111 * @throws BadRequestException if the input contains a non-scalar value
112 */
113 public function getString(string $key, string $default = ''): string
114 {
115 // Shortcuts the parent method because the validation on scalar is already done in get().
116 return (string) $this->get($key, $default);
117 }
118
119 /**
120 * @throws BadRequestException if the input value is an array and \FILTER_REQUIRE_ARRAY or \FILTER_FORCE_ARRAY is not set
121 * @throws BadRequestException if the input value is invalid and \FILTER_NULL_ON_FAILURE is not set
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 // Always turn $options into an array - this allows filter_var option shortcuts.
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 }