| 3 |
liveuser |
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 <saif.gmati@symfony.com>
|
|
|
20 |
*/
|
|
|
21 |
final class InputBag extends ParameterBag
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* Returns a string input value by name.
|
|
|
25 |
*
|
|
|
26 |
* @param string|null $default The default value if the input key does not exist
|
|
|
27 |
*
|
|
|
28 |
* @return string|null
|
|
|
29 |
*/
|
|
|
30 |
public function get(string $key, $default = null)
|
|
|
31 |
{
|
|
|
32 |
if (null !== $default && !is_scalar($default) && !(\is_object($default) && method_exists($default, '__toString'))) {
|
|
|
33 |
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead.', __METHOD__);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
$value = parent::get($key, $this);
|
|
|
37 |
|
|
|
38 |
if (null !== $value && $this !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
|
|
|
39 |
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__);
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
return $this === $value ? $default : $value;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* {@inheritdoc}
|
|
|
47 |
*/
|
|
|
48 |
public function all(string $key = null): array
|
|
|
49 |
{
|
|
|
50 |
return parent::all($key);
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Replaces the current input values by a new set.
|
|
|
55 |
*/
|
|
|
56 |
public function replace(array $inputs = [])
|
|
|
57 |
{
|
|
|
58 |
$this->parameters = [];
|
|
|
59 |
$this->add($inputs);
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Adds input values.
|
|
|
64 |
*/
|
|
|
65 |
public function add(array $inputs = [])
|
|
|
66 |
{
|
|
|
67 |
foreach ($inputs as $input => $value) {
|
|
|
68 |
$this->set($input, $value);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Sets an input by name.
|
|
|
74 |
*
|
|
|
75 |
* @param string|array|null $value
|
|
|
76 |
*/
|
|
|
77 |
public function set(string $key, $value)
|
|
|
78 |
{
|
|
|
79 |
if (null !== $value && !is_scalar($value) && !\is_array($value) && !method_exists($value, '__toString')) {
|
|
|
80 |
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead.', get_debug_type($value), __METHOD__);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
$this->parameters[$key] = $value;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* {@inheritdoc}
|
|
|
88 |
*/
|
|
|
89 |
public function filter(string $key, $default = null, int $filter = \FILTER_DEFAULT, $options = [])
|
|
|
90 |
{
|
|
|
91 |
$value = $this->has($key) ? $this->all()[$key] : $default;
|
|
|
92 |
|
|
|
93 |
// Always turn $options into an array - this allows filter_var option shortcuts.
|
|
|
94 |
if (!\is_array($options) && $options) {
|
|
|
95 |
$options = ['flags' => $options];
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) {
|
|
|
99 |
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__);
|
|
|
100 |
|
|
|
101 |
if (!isset($options['flags'])) {
|
|
|
102 |
$options['flags'] = \FILTER_REQUIRE_ARRAY;
|
|
|
103 |
}
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return filter_var($value, $filter, $options);
|
|
|
107 |
}
|
|
|
108 |
}
|