| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Symfony package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
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 |
|
|
|
16 |
/**
|
|
|
17 |
* InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
|
|
|
18 |
*
|
|
|
19 |
* @author Saif Eddin Gmati <azjezz@protonmail.com>
|
|
|
20 |
*/
|
|
|
21 |
final class InputBag extends ParameterBag
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* Returns a scalar input value by name.
|
|
|
25 |
*
|
|
|
26 |
* @param string|int|float|bool|null $default The default value if the input key does not exist
|
|
|
27 |
*/
|
|
|
28 |
public function get(string $key, mixed $default = null): string|int|float|bool|null
|
|
|
29 |
{
|
|
|
30 |
if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
|
|
|
31 |
throw new \InvalidArgumentException(sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
$value = parent::get($key, $this);
|
|
|
35 |
|
|
|
36 |
if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
|
|
|
37 |
throw new BadRequestException(sprintf('Input value "%s" contains a non-scalar value.', $key));
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
return $this === $value ? $default : $value;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Replaces the current input values by a new set.
|
|
|
45 |
*/
|
|
|
46 |
public function replace(array $inputs = [])
|
|
|
47 |
{
|
|
|
48 |
$this->parameters = [];
|
|
|
49 |
$this->add($inputs);
|
|
|
50 |
}
|
|
|
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 |
/**
|
|
|
63 |
* Sets an input by name.
|
|
|
64 |
*
|
|
|
65 |
* @param string|int|float|bool|array|null $value
|
|
|
66 |
*/
|
|
|
67 |
public function set(string $key, mixed $value)
|
|
|
68 |
{
|
|
|
69 |
if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
|
|
|
70 |
throw new \InvalidArgumentException(sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
$this->parameters[$key] = $value;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
|
|
|
77 |
{
|
|
|
78 |
$value = $this->has($key) ? $this->all()[$key] : $default;
|
|
|
79 |
|
|
|
80 |
// Always turn $options into an array - this allows filter_var option shortcuts.
|
|
|
81 |
if (!\is_array($options) && $options) {
|
|
|
82 |
$options = ['flags' => $options];
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) {
|
|
|
86 |
throw new BadRequestException(sprintf('Input value "%s" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.', $key));
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
|
|
|
90 |
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)));
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
return filter_var($value, $filter, $options);
|
|
|
94 |
}
|
|
|
95 |
}
|