| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class ParameterBag implements \IteratorAggregate, \Countable |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( |
| 30 |
protected array $parameters = [], |
| 31 |
) { |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 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 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function keys(): array |
| 64 |
{ |
| 65 |
return array_keys($this->parameters); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function replace(array $parameters = []): void |
| 74 |
{ |
| 75 |
$this->parameters = $parameters; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 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 |
|
| 100 |
|
| 101 |
public function has(string $key): bool |
| 102 |
{ |
| 103 |
return \array_key_exists($key, $this->parameters); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function remove(string $key): void |
| 110 |
{ |
| 111 |
unset($this->parameters[$key]); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getAlpha(string $key, string $default = ''): string |
| 120 |
{ |
| 121 |
return preg_replace('/[^[:alpha:]]/', '', $this->getString($key, $default)); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function getAlnum(string $key, string $default = ''): string |
| 130 |
{ |
| 131 |
return preg_replace('/[^[:alnum:]]/', '', $this->getString($key, $default)); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getDigits(string $key, string $default = ''): string |
| 140 |
{ |
| 141 |
return preg_replace('/[^[:digit:]]/', '', $this->getString($key, $default)); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 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 |
|
| 161 |
|
| 162 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 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 |
|
| 224 |
if (!\is_array($options) && $options) { |
| 225 |
$options = ['flags' => $options]; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 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 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function getIterator(): \ArrayIterator |
| 260 |
{ |
| 261 |
return new \ArrayIterator($this->parameters); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public function count(): int |
| 268 |
{ |
| 269 |
return \count($this->parameters); |
| 270 |
} |
| 271 |
} |