class InputBag
InputBag is a container for user input values such as $_GET, $_POST, $_REQUEST, and $_COOKIE.
@author Saif Eddin Gmati <azjezz@protonmail.com>
Hierarchy
- class \Symfony\Component\HttpFoundation\ParameterBag implements \Symfony\Component\HttpFoundation\IteratorAggregate, \Symfony\Component\HttpFoundation\Countable
- class \Symfony\Component\HttpFoundation\InputBag extends \Symfony\Component\HttpFoundation\ParameterBag
Expanded class hierarchy of InputBag
6 files declare their use of InputBag
- FormBuilder.php in core/
lib/ Drupal/ Core/ Form/ FormBuilder.php - NullRouteMatch.php in core/
lib/ Drupal/ Core/ Routing/ NullRouteMatch.php - ParamConversionEnhancer.php in core/
lib/ Drupal/ Core/ Routing/ Enhancer/ ParamConversionEnhancer.php - RouteMatch.php in core/
lib/ Drupal/ Core/ Routing/ RouteMatch.php - UpdateKernel.php in core/
lib/ Drupal/ Core/ Update/ UpdateKernel.php
File
-
vendor/
symfony/ http-foundation/ InputBag.php, line 22
Namespace
Symfony\Component\HttpFoundationView source
final class InputBag extends ParameterBag {
/**
* Returns a scalar input value by name.
*
* @param string|int|float|bool|null $default The default value if the input key does not exist
*/
public function get(string $key, mixed $default = null) : string|int|float|bool|null {
if (null !== $default && !\is_scalar($default) && !$default instanceof \Stringable) {
throw new \InvalidArgumentException(\sprintf('Expected a scalar value as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($default)));
}
$value = parent::get($key, $this);
if (null !== $value && $this !== $value && !\is_scalar($value) && !$value instanceof \Stringable) {
throw new BadRequestException(\sprintf('Input value "%s" contains a non-scalar value.', $key));
}
return $this === $value ? $default : $value;
}
/**
* Replaces the current input values by a new set.
*/
public function replace(array $inputs = []) : void {
$this->parameters = [];
$this->add($inputs);
}
/**
* Adds input values.
*/
public function add(array $inputs = []) : void {
foreach ($inputs as $input => $value) {
$this->set($input, $value);
}
}
/**
* Sets an input by name.
*
* @param string|int|float|bool|array|null $value
*/
public function set(string $key, mixed $value) : void {
if (null !== $value && !\is_scalar($value) && !\is_array($value) && !$value instanceof \Stringable) {
throw new \InvalidArgumentException(\sprintf('Expected a scalar, or an array as a 2nd argument to "%s()", "%s" given.', __METHOD__, get_debug_type($value)));
}
$this->parameters[$key] = $value;
}
/**
* Returns the parameter value converted to an enum.
*
* @template T of \BackedEnum
*
* @param class-string<T> $class
* @param ?T $default
*
* @return ?T
*
* @psalm-return ($default is null ? T|null : T)
*/
public function getEnum(string $key, string $class, ?\BackedEnum $default = null) : ?\BackedEnum {
try {
return parent::getEnum($key, $class, $default);
} catch (UnexpectedValueException $e) {
throw new BadRequestException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* Returns the parameter value converted to string.
*/
public function getString(string $key, string $default = '') : string {
// Shortcuts the parent method because the validation on scalar is already done in get().
return (string) $this->get($key, $default);
}
public function filter(string $key, mixed $default = null, int $filter = \FILTER_DEFAULT, mixed $options = []) : mixed {
$value = $this->has($key) ? $this->all()[$key] : $default;
// Always turn $options into an array - this allows filter_var option shortcuts.
if (!\is_array($options) && $options) {
$options = [
'flags' => $options,
];
}
if (\is_array($value) && !(($options['flags'] ?? 0) & (\FILTER_REQUIRE_ARRAY | \FILTER_FORCE_ARRAY))) {
throw new BadRequestException(\sprintf('Input value "%s" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.', $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;
}
throw new BadRequestException(\sprintf('Input value "%s" is invalid and flag "FILTER_NULL_ON_FAILURE" was not set.', $key));
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
InputBag::add | public | function | Adds input values. | Overrides ParameterBag::add | |
InputBag::filter | public | function | Filter key. | Overrides ParameterBag::filter | |
InputBag::get | public | function | Returns a scalar input value by name. | Overrides ParameterBag::get | |
InputBag::getEnum | public | function | Returns the parameter value converted to an enum. | Overrides ParameterBag::getEnum | |
InputBag::getString | public | function | Returns the parameter value converted to string. | Overrides ParameterBag::getString | |
InputBag::replace | public | function | Replaces the current input values by a new set. | Overrides ParameterBag::replace | |
InputBag::set | public | function | Sets an input by name. | Overrides ParameterBag::set | |
ParameterBag::all | public | function | Returns the parameters. | ||
ParameterBag::count | public | function | Returns the number of parameters. | ||
ParameterBag::getAlnum | public | function | Returns the alphabetic characters and digits of the parameter value. | ||
ParameterBag::getAlpha | public | function | Returns the alphabetic characters of the parameter value. | ||
ParameterBag::getBoolean | public | function | Returns the parameter value converted to boolean. | ||
ParameterBag::getDigits | public | function | Returns the digits of the parameter value. | ||
ParameterBag::getInt | public | function | Returns the parameter value converted to integer. | ||
ParameterBag::getIterator | public | function | Returns an iterator for parameters. | ||
ParameterBag::has | public | function | Returns true if the parameter is defined. | ||
ParameterBag::keys | public | function | Returns the parameter keys. | ||
ParameterBag::remove | public | function | Removes a parameter. | ||
ParameterBag::__construct | public | function | 2 |