Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/ParameterBag.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 * ParameterBag is a container for key/value pairs.
19 *
20 * @author Fabien Potencier <[email protected]>
21 *
22 * @implements \IteratorAggregate<string, mixed>
23 */
24 class ParameterBag implements \IteratorAggregate, \Countable
25 {
26 /**
27 * @param array<string, mixed> $parameters
28 */
29 public function __construct(
30 protected array $parameters = [],
31 ) {
32 }
33
34 /**
35 * Returns the parameters.
36 *
37 * @template TKey of string|null
38 *
39 * @param TKey $key The name of the parameter to return or null to get them all
40 *
41 * @return (TKey is null ? array<string, mixed> : array<mixed>)
42 *
43 * @throws BadRequestException if the value is not an array
44 */
45 public function all(?string $key = null): array
46 {
47 if (null === $key) {
48 return $this->parameters;
49 }
50
51 if (!\is_array($value = $this->parameters[$key] ?? [])) {
52 throw new BadRequestException(\sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
53 }
54
55 return $value;
56 }
57
58 /**
59 * Returns the parameter keys.
60 *
61 * @return list<string>
62 */
63 public function keys(): array
64 {
65 return array_keys($this->parameters);
66 }
67
68 /**
69 * Replaces the current parameters by a new set.
70 *
71 * @param array<string, mixed> $parameters
72 */
73 public function replace(array $parameters = []): void
74 {
75 $this->parameters = $parameters;
76 }
77
78 /**
79 * Adds parameters.
80 *
81 * @param array<string, mixed> $parameters
82 */
83 public function add(array $parameters = []): void
84 {
85 $this->parameters = array_replace($this->parameters, $parameters);
86 }
87
88 public function get(string $key, mixed $default = null): mixed
89 {
90 return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
91 }
92
93 public function set(string $key, mixed $value): void
94 {
95 $this->parameters[$key] = $value;
96 }
97
98 /**
99 * Returns true if the parameter is defined.
100 */
101 public function has(string $key): bool
102 {
103 return \array_key_exists($key, $this->parameters);
104 }
105
106 /**
107 * Removes a parameter.
108 */
109 public function remove(string $key): void
110 {
111 unset($this->parameters[$key]);
112 }
113
114 /**
115 * Returns the alphabetic characters of the parameter value.
116 *
117 * @throws UnexpectedValueException if the value cannot be converted to string
118 */
119 public function getAlpha(string $key, string $default = ''): string
120 {
121 return preg_replace('/[^[:alpha:]]/', '', $this->getString($key, $default));
122 }
123
124 /**
125 * Returns the alphabetic characters and digits of the parameter value.
126 *
127 * @throws UnexpectedValueException if the value cannot be converted to string
128 */
129 public function getAlnum(string $key, string $default = ''): string
130 {
131 return preg_replace('/[^[:alnum:]]/', '', $this->getString($key, $default));
132 }
133
134 /**
135 * Returns the digits of the parameter value.
136 *
137 * @throws UnexpectedValueException if the value cannot be converted to string
138 */
139 public function getDigits(string $key, string $default = ''): string
140 {
141 return preg_replace('/[^[:digit:]]/', '', $this->getString($key, $default));
142 }
143
144 /**
145 * Returns the parameter as string.
146 *
147 * @throws UnexpectedValueException if the value cannot be converted to string
148 */
149 public function getString(string $key, string $default = ''): string
150 {
151 $value = $this->get($key, $default);
152 if (!\is_scalar($value) && !$value instanceof \Stringable) {
153 throw new UnexpectedValueException(\sprintf('Parameter value "%s" cannot be converted to "string".', $key));
154 }
155
156 return (string) $value;
157 }
158
159 /**
160 * Returns the parameter value converted to integer.
161 *
162 * @throws UnexpectedValueException if the value cannot be converted to integer
163 */
164 public function getInt(string $key, int $default = 0): int
165 {
166 return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]);
167 }
168
169 /**
170 * Returns the parameter value converted to boolean.
171 *
172 * @throws UnexpectedValueException if the value cannot be converted to a boolean
173 */
174 public function getBoolean(string $key, bool $default = false): bool
175 {
176 return $this->filter($key, $default, \FILTER_VALIDATE_BOOL, ['flags' => \FILTER_REQUIRE_SCALAR]);
177 }
178
179 /**
180 * Returns the parameter value converted to an enum.
181 *
182 * @template T of \BackedEnum
183 *
184 * @param class-string<T> $class
185 * @param ?T $default
186 *
187 * @return ?T
188 *
189 * @psalm-return ($default is null ? T|null : T)
190 *
191 * @throws UnexpectedValueException if the parameter value cannot be converted to an enum
192 */
193 public function getEnum(string $key, string $class, ?\BackedEnum $default = null): ?\BackedEnum
194 {
195 $value = $this->get($key);
196
197 if (null === $value) {
198 return $default;
199 }
200
201 try {
202 return $class::from($value);
203 } catch (\ValueError|\TypeError $e) {
204 throw new UnexpectedValueException(\sprintf('Parameter "%s" cannot be converted to enum: ', $key).$e->getMessage().'.', $e->getCode(), $e);
205 }
206 }
207
208 /**
209 * Filter key.
210 *
211 * @param int $filter FILTER_* constant
212 * @param int|array{flags?: int, options?: array} $options Flags from FILTER_* constants
213 *
214 * @see https://php.net/filter-var
215 *
216 * @throws UnexpectedValueException if the parameter value is a non-stringable object
217 * @throws UnexpectedValueException if the parameter value is invalid and \FILTER_NULL_ON_FAILURE is not set
218 */
219 public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
220 {
221 $value = $this->get($key, $default);
222
223 // Always turn $options into an array - this allows filter_var option shortcuts.
224 if (!\is_array($options) && $options) {
225 $options = ['flags' => $options];
226 }
227
228 // Add a convenience check for arrays.
229 if (\is_array($value) && !isset($options['flags'])) {
230 $options['flags'] = \FILTER_REQUIRE_ARRAY;
231 }
232
233 if (\is_object($value) && !$value instanceof \Stringable) {
234 throw new UnexpectedValueException(\sprintf('Parameter value "%s" cannot be filtered.', $key));
235 }
236
237 if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
238 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)));
239 }
240
241 $options['flags'] ??= 0;
242 $nullOnFailure = $options['flags'] & \FILTER_NULL_ON_FAILURE;
243 $options['flags'] |= \FILTER_NULL_ON_FAILURE;
244
245 $value = filter_var($value, $filter, $options);
246
247 if (null !== $value || $nullOnFailure) {
248 return $value;
249 }
250
251 throw new \UnexpectedValueException(\sprintf('Parameter value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key));
252 }
253
254 /**
255 * Returns an iterator for parameters.
256 *
257 * @return \ArrayIterator<string, mixed>
258 */
259 public function getIterator(): \ArrayIterator
260 {
261 return new \ArrayIterator($this->parameters);
262 }
263
264 /**
265 * Returns the number of parameters.
266 */
267 public function count(): int
268 {
269 return \count($this->parameters);
270 }
271 }