| 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 |
* ParameterBag is a container for key/value pairs.
|
|
|
18 |
*
|
|
|
19 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
20 |
*
|
|
|
21 |
* @implements \IteratorAggregate<string, mixed>
|
|
|
22 |
*/
|
|
|
23 |
class ParameterBag implements \IteratorAggregate, \Countable
|
|
|
24 |
{
|
|
|
25 |
/**
|
|
|
26 |
* Parameter storage.
|
|
|
27 |
*/
|
|
|
28 |
protected $parameters;
|
|
|
29 |
|
|
|
30 |
public function __construct(array $parameters = [])
|
|
|
31 |
{
|
|
|
32 |
$this->parameters = $parameters;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Returns the parameters.
|
|
|
37 |
*
|
|
|
38 |
* @param string|null $key The name of the parameter to return or null to get them all
|
|
|
39 |
*/
|
|
|
40 |
public function all(string $key = null): array
|
|
|
41 |
{
|
|
|
42 |
if (null === $key) {
|
|
|
43 |
return $this->parameters;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
if (!\is_array($value = $this->parameters[$key] ?? [])) {
|
|
|
47 |
throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
return $value;
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Returns the parameter keys.
|
|
|
55 |
*/
|
|
|
56 |
public function keys(): array
|
|
|
57 |
{
|
|
|
58 |
return array_keys($this->parameters);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Replaces the current parameters by a new set.
|
|
|
63 |
*/
|
|
|
64 |
public function replace(array $parameters = [])
|
|
|
65 |
{
|
|
|
66 |
$this->parameters = $parameters;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Adds parameters.
|
|
|
71 |
*/
|
|
|
72 |
public function add(array $parameters = [])
|
|
|
73 |
{
|
|
|
74 |
$this->parameters = array_replace($this->parameters, $parameters);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
public function get(string $key, mixed $default = null): mixed
|
|
|
78 |
{
|
|
|
79 |
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
public function set(string $key, mixed $value)
|
|
|
83 |
{
|
|
|
84 |
$this->parameters[$key] = $value;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Returns true if the parameter is defined.
|
|
|
89 |
*/
|
|
|
90 |
public function has(string $key): bool
|
|
|
91 |
{
|
|
|
92 |
return \array_key_exists($key, $this->parameters);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Removes a parameter.
|
|
|
97 |
*/
|
|
|
98 |
public function remove(string $key)
|
|
|
99 |
{
|
|
|
100 |
unset($this->parameters[$key]);
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* Returns the alphabetic characters of the parameter value.
|
|
|
105 |
*/
|
|
|
106 |
public function getAlpha(string $key, string $default = ''): string
|
|
|
107 |
{
|
|
|
108 |
return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Returns the alphabetic characters and digits of the parameter value.
|
|
|
113 |
*/
|
|
|
114 |
public function getAlnum(string $key, string $default = ''): string
|
|
|
115 |
{
|
|
|
116 |
return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Returns the digits of the parameter value.
|
|
|
121 |
*/
|
|
|
122 |
public function getDigits(string $key, string $default = ''): string
|
|
|
123 |
{
|
|
|
124 |
// we need to remove - and + because they're allowed in the filter
|
|
|
125 |
return str_replace(['-', '+'], '', $this->filter($key, $default, \FILTER_SANITIZE_NUMBER_INT));
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Returns the parameter value converted to integer.
|
|
|
130 |
*/
|
|
|
131 |
public function getInt(string $key, int $default = 0): int
|
|
|
132 |
{
|
|
|
133 |
return (int) $this->get($key, $default);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
/**
|
|
|
137 |
* Returns the parameter value converted to boolean.
|
|
|
138 |
*/
|
|
|
139 |
public function getBoolean(string $key, bool $default = false): bool
|
|
|
140 |
{
|
|
|
141 |
return $this->filter($key, $default, \FILTER_VALIDATE_BOOL);
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
/**
|
|
|
145 |
* Filter key.
|
|
|
146 |
*
|
|
|
147 |
* @param int $filter FILTER_* constant
|
|
|
148 |
*
|
|
|
149 |
* @see https://php.net/filter-var
|
|
|
150 |
*/
|
|
|
151 |
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
|
|
|
152 |
{
|
|
|
153 |
$value = $this->get($key, $default);
|
|
|
154 |
|
|
|
155 |
// Always turn $options into an array - this allows filter_var option shortcuts.
|
|
|
156 |
if (!\is_array($options) && $options) {
|
|
|
157 |
$options = ['flags' => $options];
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
// Add a convenience check for arrays.
|
|
|
161 |
if (\is_array($value) && !isset($options['flags'])) {
|
|
|
162 |
$options['flags'] = \FILTER_REQUIRE_ARRAY;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
|
|
|
166 |
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)));
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
return filter_var($value, $filter, $options);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* Returns an iterator for parameters.
|
|
|
174 |
*
|
|
|
175 |
* @return \ArrayIterator<string, mixed>
|
|
|
176 |
*/
|
|
|
177 |
public function getIterator(): \ArrayIterator
|
|
|
178 |
{
|
|
|
179 |
return new \ArrayIterator($this->parameters);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Returns the number of parameters.
|
|
|
184 |
*/
|
|
|
185 |
public function count(): int
|
|
|
186 |
{
|
|
|
187 |
return \count($this->parameters);
|
|
|
188 |
}
|
|
|
189 |
}
|