123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- namespace Symfony\Component\HttpFoundation;
- use Symfony\Component\HttpFoundation\Exception\BadRequestException;
- use Symfony\Component\HttpFoundation\Exception\UnexpectedValueException;
- class ParameterBag implements \IteratorAggregate, \Countable
- {
-
- protected $parameters;
- public function __construct(array $parameters = [])
- {
- $this->parameters = $parameters;
- }
-
- public function all(?string $key = null): array
- {
- if (null === $key) {
- return $this->parameters;
- }
- if (!\is_array($value = $this->parameters[$key] ?? [])) {
- throw new BadRequestException(sprintf('Unexpected value for parameter "%s": expecting "array", got "%s".', $key, get_debug_type($value)));
- }
- return $value;
- }
-
- public function keys(): array
- {
- return array_keys($this->parameters);
- }
-
- public function replace(array $parameters = [])
- {
- $this->parameters = $parameters;
- }
-
- public function add(array $parameters = [])
- {
- $this->parameters = array_replace($this->parameters, $parameters);
- }
- public function get(string $key, mixed $default = null): mixed
- {
- return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
- }
-
- public function set(string $key, mixed $value)
- {
- $this->parameters[$key] = $value;
- }
-
- public function has(string $key): bool
- {
- return \array_key_exists($key, $this->parameters);
- }
-
- public function remove(string $key)
- {
- unset($this->parameters[$key]);
- }
-
- public function getAlpha(string $key, string $default = ''): string
- {
- return preg_replace('/[^[:alpha:]]/', '', $this->getString($key, $default));
- }
-
- public function getAlnum(string $key, string $default = ''): string
- {
- return preg_replace('/[^[:alnum:]]/', '', $this->getString($key, $default));
- }
-
- public function getDigits(string $key, string $default = ''): string
- {
- return preg_replace('/[^[:digit:]]/', '', $this->getString($key, $default));
- }
-
- public function getString(string $key, string $default = ''): string
- {
- $value = $this->get($key, $default);
- if (!\is_scalar($value) && !$value instanceof \Stringable) {
- throw new UnexpectedValueException(sprintf('Parameter value "%s" cannot be converted to "string".', $key));
- }
- return (string) $value;
- }
-
- public function getInt(string $key, int $default = 0): int
- {
-
- return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]) ?: 0;
- }
-
- public function getBoolean(string $key, bool $default = false): bool
- {
- return $this->filter($key, $default, \FILTER_VALIDATE_BOOL, ['flags' => \FILTER_REQUIRE_SCALAR]);
- }
-
- public function getEnum(string $key, string $class, ?\BackedEnum $default = null): ?\BackedEnum
- {
- $value = $this->get($key);
- if (null === $value) {
- return $default;
- }
- try {
- return $class::from($value);
- } catch (\ValueError|\TypeError $e) {
- throw new UnexpectedValueException(sprintf('Parameter "%s" cannot be converted to enum: %s.', $key, $e->getMessage()), $e->getCode(), $e);
- }
- }
-
- public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []): mixed
- {
- $value = $this->get($key, $default);
-
- if (!\is_array($options) && $options) {
- $options = ['flags' => $options];
- }
-
- if (\is_array($value) && !isset($options['flags'])) {
- $options['flags'] = \FILTER_REQUIRE_ARRAY;
- }
- if (\is_object($value) && !$value instanceof \Stringable) {
- throw new UnexpectedValueException(sprintf('Parameter value "%s" cannot be filtered.', $key));
- }
- if ((\FILTER_CALLBACK & $filter) && !(($options['options'] ?? null) instanceof \Closure)) {
- 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)));
- }
- $options['flags'] ??= 0;
- $nullOnFailure = $options['flags'] & \FILTER_NULL_ON_FAILURE;
- $options['flags'] |= \FILTER_NULL_ON_FAILURE;
- $value = filter_var($value, $filter, $options);
- if (null !== $value || $nullOnFailure) {
- return $value;
- }
- $method = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS | \DEBUG_BACKTRACE_PROVIDE_OBJECT, 2)[1];
- $method = ($method['object'] ?? null) === $this ? $method['function'] : 'filter';
- $hint = 'filter' === $method ? 'pass' : 'use method "filter()" with';
- trigger_deprecation('symfony/http-foundation', '6.3', 'Ignoring invalid values when using "%s::%s(\'%s\')" is deprecated and will throw an "%s" in 7.0; '.$hint.' flag "FILTER_NULL_ON_FAILURE" to keep ignoring them.', $this::class, $method, $key, UnexpectedValueException::class);
- return false;
- }
-
- public function getIterator(): \ArrayIterator
- {
- return new \ArrayIterator($this->parameters);
- }
-
- public function count(): int
- {
- return \count($this->parameters);
- }
- }
|