2022-08-19 19:48:37 +08:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\HttpFoundation ;
use Symfony\Component\HttpFoundation\Exception\BadRequestException ;
/**
* InputBag is a container for user input values such as $_GET , $_POST , $_REQUEST , and $_COOKIE .
*
* @ author Saif Eddin Gmati < azjezz @ protonmail . com >
*/
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
*/
2022-11-28 19:11:12 +08:00
public function get ( string $key , mixed $default = null ) : string | int | float | bool | null
2022-08-19 19:48:37 +08:00
{
2022-11-28 19:11:12 +08:00
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 )));
2022-08-19 19:48:37 +08:00
}
$value = parent :: get ( $key , $this );
2022-11-28 19:11:12 +08:00
if ( null !== $value && $this !== $value && ! \is_scalar ( $value ) && ! $value instanceof \Stringable ) {
throw new BadRequestException ( sprintf ( 'Input value "%s" contains a non-scalar value.' , $key ));
2022-08-19 19:48:37 +08:00
}
return $this === $value ? $default : $value ;
}
/**
* Replaces the current input values by a new set .
*/
public function replace ( array $inputs = [])
{
$this -> parameters = [];
$this -> add ( $inputs );
}
/**
* Adds input values .
*/
public function add ( array $inputs = [])
{
foreach ( $inputs as $input => $value ) {
$this -> set ( $input , $value );
}
}
/**
* Sets an input by name .
*
* @ param string | int | float | bool | array | null $value
*/
2022-11-28 19:11:12 +08:00
public function set ( string $key , mixed $value )
2022-08-19 19:48:37 +08:00
{
2022-11-28 19:11:12 +08:00
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 )));
2022-08-19 19:48:37 +08:00
}
$this -> parameters [ $key ] = $value ;
}
/**
* { @ inheritdoc }
*/
2022-11-28 19:11:12 +08:00
public function filter ( string $key , mixed $default = null , int $filter = \FILTER_DEFAULT , mixed $options = []) : mixed
2022-08-19 19:48:37 +08:00
{
$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 ))) {
2022-11-28 19:11:12 +08:00
throw new BadRequestException ( sprintf ( 'Input value "%s" contains an array, but "FILTER_REQUIRE_ARRAY" or "FILTER_FORCE_ARRAY" flags were not set.' , $key ));
2022-08-19 19:48:37 +08:00
}
if (( \FILTER_CALLBACK & $filter ) && ! (( $options [ 'options' ] ? ? null ) instanceof \Closure )) {
2022-11-28 19:11:12 +08:00
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 )));
2022-08-19 19:48:37 +08:00
}
return filter_var ( $value , $filter , $options );
}
}