vendor/symfony/http-foundation/ParameterBag.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpFoundation;
  11. /**
  12.  * ParameterBag is a container for key/value pairs.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class ParameterBag implements \IteratorAggregate\Countable
  17. {
  18.     /**
  19.      * Parameter storage.
  20.      */
  21.     protected $parameters;
  22.     public function __construct(array $parameters = [])
  23.     {
  24.         $this->parameters $parameters;
  25.     }
  26.     /**
  27.      * Returns the parameters.
  28.      *
  29.      * @return array An array of parameters
  30.      */
  31.     public function all()
  32.     {
  33.         return $this->parameters;
  34.     }
  35.     /**
  36.      * Returns the parameter keys.
  37.      *
  38.      * @return array An array of parameter keys
  39.      */
  40.     public function keys()
  41.     {
  42.         return array_keys($this->parameters);
  43.     }
  44.     /**
  45.      * Replaces the current parameters by a new set.
  46.      */
  47.     public function replace(array $parameters = [])
  48.     {
  49.         $this->parameters $parameters;
  50.     }
  51.     /**
  52.      * Adds parameters.
  53.      */
  54.     public function add(array $parameters = [])
  55.     {
  56.         $this->parameters array_replace($this->parameters$parameters);
  57.     }
  58.     /**
  59.      * Returns a parameter by name.
  60.      *
  61.      * @param mixed $default The default value if the parameter key does not exist
  62.      *
  63.      * @return mixed
  64.      */
  65.     public function get(string $key$default null)
  66.     {
  67.         return \array_key_exists($key$this->parameters) ? $this->parameters[$key] : $default;
  68.     }
  69.     /**
  70.      * Sets a parameter by name.
  71.      *
  72.      * @param mixed $value The value
  73.      */
  74.     public function set(string $key$value)
  75.     {
  76.         $this->parameters[$key] = $value;
  77.     }
  78.     /**
  79.      * Returns true if the parameter is defined.
  80.      *
  81.      * @return bool true if the parameter exists, false otherwise
  82.      */
  83.     public function has(string $key)
  84.     {
  85.         return \array_key_exists($key$this->parameters);
  86.     }
  87.     /**
  88.      * Removes a parameter.
  89.      */
  90.     public function remove(string $key)
  91.     {
  92.         unset($this->parameters[$key]);
  93.     }
  94.     /**
  95.      * Returns the alphabetic characters of the parameter value.
  96.      *
  97.      * @return string The filtered value
  98.      */
  99.     public function getAlpha(string $keystring $default '')
  100.     {
  101.         return preg_replace('/[^[:alpha:]]/'''$this->get($key$default));
  102.     }
  103.     /**
  104.      * Returns the alphabetic characters and digits of the parameter value.
  105.      *
  106.      * @return string The filtered value
  107.      */
  108.     public function getAlnum(string $keystring $default '')
  109.     {
  110.         return preg_replace('/[^[:alnum:]]/'''$this->get($key$default));
  111.     }
  112.     /**
  113.      * Returns the digits of the parameter value.
  114.      *
  115.      * @return string The filtered value
  116.      */
  117.     public function getDigits(string $keystring $default '')
  118.     {
  119.         // we need to remove - and + because they're allowed in the filter
  120.         return str_replace(['-''+'], ''$this->filter($key$defaultFILTER_SANITIZE_NUMBER_INT));
  121.     }
  122.     /**
  123.      * Returns the parameter value converted to integer.
  124.      *
  125.      * @return int The filtered value
  126.      */
  127.     public function getInt(string $keyint $default 0)
  128.     {
  129.         return (int) $this->get($key$default);
  130.     }
  131.     /**
  132.      * Returns the parameter value converted to boolean.
  133.      *
  134.      * @return bool The filtered value
  135.      */
  136.     public function getBoolean(string $keybool $default false)
  137.     {
  138.         return $this->filter($key$defaultFILTER_VALIDATE_BOOLEAN);
  139.     }
  140.     /**
  141.      * Filter key.
  142.      *
  143.      * @param mixed $default Default = null
  144.      * @param int   $filter  FILTER_* constant
  145.      * @param mixed $options Filter options
  146.      *
  147.      * @see https://php.net/filter-var
  148.      *
  149.      * @return mixed
  150.      */
  151.     public function filter(string $key$default nullint $filter FILTER_DEFAULT$options = [])
  152.     {
  153.         $value $this->get($key$default);
  154.         // Always turn $options into an array - this allows filter_var option shortcuts.
  155.         if (!\is_array($options) && $options) {
  156.             $options = ['flags' => $options];
  157.         }
  158.         // Add a convenience check for arrays.
  159.         if (\is_array($value) && !isset($options['flags'])) {
  160.             $options['flags'] = FILTER_REQUIRE_ARRAY;
  161.         }
  162.         return filter_var($value$filter$options);
  163.     }
  164.     /**
  165.      * Returns an iterator for parameters.
  166.      *
  167.      * @return \ArrayIterator An \ArrayIterator instance
  168.      */
  169.     public function getIterator()
  170.     {
  171.         return new \ArrayIterator($this->parameters);
  172.     }
  173.     /**
  174.      * Returns the number of parameters.
  175.      *
  176.      * @return int The number of parameters
  177.      */
  178.     public function count()
  179.     {
  180.         return \count($this->parameters);
  181.     }
  182. }