[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/ -> ParameterBag.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpFoundation;
  13  
  14  /**
  15   * ParameterBag is a container for key/value pairs.
  16   *
  17   * @author Fabien Potencier <fabien@symfony.com>
  18   */
  19  class ParameterBag implements \IteratorAggregate, \Countable
  20  {
  21      /**
  22       * Parameter storage.
  23       */
  24      protected $parameters;
  25  
  26      /**
  27       * @param array $parameters An array of parameters
  28       */
  29      public function __construct(array $parameters = array())
  30      {
  31          $this->parameters = $parameters;
  32      }
  33  
  34      /**
  35       * Returns the parameters.
  36       *
  37       * @return array An array of parameters
  38       */
  39      public function all()
  40      {
  41          return $this->parameters;
  42      }
  43  
  44      /**
  45       * Returns the parameter keys.
  46       *
  47       * @return array An array of parameter keys
  48       */
  49      public function keys()
  50      {
  51          return array_keys($this->parameters);
  52      }
  53  
  54      /**
  55       * Replaces the current parameters by a new set.
  56       *
  57       * @param array $parameters An array of parameters
  58       */
  59      public function replace(array $parameters = array())
  60      {
  61          $this->parameters = $parameters;
  62      }
  63  
  64      /**
  65       * Adds parameters.
  66       *
  67       * @param array $parameters An array of parameters
  68       */
  69      public function add(array $parameters = array())
  70      {
  71          $this->parameters = array_replace($this->parameters, $parameters);
  72      }
  73  
  74      /**
  75       * Returns a parameter by name.
  76       *
  77       * Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
  78       *
  79       * @param string $key     The key
  80       * @param mixed  $default The default value if the parameter key does not exist
  81       * @param bool   $deep    If true, a path like foo[bar] will find deeper items
  82       *
  83       * @return mixed
  84       *
  85       * @throws \InvalidArgumentException
  86       */
  87      public function get($key, $default = null, $deep = false)
  88      {
  89          if ($deep) {
  90              @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since Symfony 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
  91          }
  92  
  93          if (!$deep || false === $pos = strpos($key, '[')) {
  94              return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
  95          }
  96  
  97          $root = substr($key, 0, $pos);
  98          if (!array_key_exists($root, $this->parameters)) {
  99              return $default;
 100          }
 101  
 102          $value = $this->parameters[$root];
 103          $currentKey = null;
 104          for ($i = $pos, $c = \strlen($key); $i < $c; ++$i) {
 105              $char = $key[$i];
 106  
 107              if ('[' === $char) {
 108                  if (null !== $currentKey) {
 109                      throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
 110                  }
 111  
 112                  $currentKey = '';
 113              } elseif (']' === $char) {
 114                  if (null === $currentKey) {
 115                      throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
 116                  }
 117  
 118                  if (!\is_array($value) || !array_key_exists($currentKey, $value)) {
 119                      return $default;
 120                  }
 121  
 122                  $value = $value[$currentKey];
 123                  $currentKey = null;
 124              } else {
 125                  if (null === $currentKey) {
 126                      throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
 127                  }
 128  
 129                  $currentKey .= $char;
 130              }
 131          }
 132  
 133          if (null !== $currentKey) {
 134              throw new \InvalidArgumentException('Malformed path. Path must end with "]".');
 135          }
 136  
 137          return $value;
 138      }
 139  
 140      /**
 141       * Sets a parameter by name.
 142       *
 143       * @param string $key   The key
 144       * @param mixed  $value The value
 145       */
 146      public function set($key, $value)
 147      {
 148          $this->parameters[$key] = $value;
 149      }
 150  
 151      /**
 152       * Returns true if the parameter is defined.
 153       *
 154       * @param string $key The key
 155       *
 156       * @return bool true if the parameter exists, false otherwise
 157       */
 158      public function has($key)
 159      {
 160          return array_key_exists($key, $this->parameters);
 161      }
 162  
 163      /**
 164       * Removes a parameter.
 165       *
 166       * @param string $key The key
 167       */
 168      public function remove($key)
 169      {
 170          unset($this->parameters[$key]);
 171      }
 172  
 173      /**
 174       * Returns the alphabetic characters of the parameter value.
 175       *
 176       * @param string $key     The parameter key
 177       * @param string $default The default value if the parameter key does not exist
 178       * @param bool   $deep    If true, a path like foo[bar] will find deeper items
 179       *
 180       * @return string The filtered value
 181       */
 182      public function getAlpha($key, $default = '', $deep = false)
 183      {
 184          return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
 185      }
 186  
 187      /**
 188       * Returns the alphabetic characters and digits of the parameter value.
 189       *
 190       * @param string $key     The parameter key
 191       * @param string $default The default value if the parameter key does not exist
 192       * @param bool   $deep    If true, a path like foo[bar] will find deeper items
 193       *
 194       * @return string The filtered value
 195       */
 196      public function getAlnum($key, $default = '', $deep = false)
 197      {
 198          return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
 199      }
 200  
 201      /**
 202       * Returns the digits of the parameter value.
 203       *
 204       * @param string $key     The parameter key
 205       * @param string $default The default value if the parameter key does not exist
 206       * @param bool   $deep    If true, a path like foo[bar] will find deeper items
 207       *
 208       * @return string The filtered value
 209       */
 210      public function getDigits($key, $default = '', $deep = false)
 211      {
 212          // we need to remove - and + because they're allowed in the filter
 213          return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT, array(), $deep));
 214      }
 215  
 216      /**
 217       * Returns the parameter value converted to integer.
 218       *
 219       * @param string $key     The parameter key
 220       * @param int    $default The default value if the parameter key does not exist
 221       * @param bool   $deep    If true, a path like foo[bar] will find deeper items
 222       *
 223       * @return int The filtered value
 224       */
 225      public function getInt($key, $default = 0, $deep = false)
 226      {
 227          return (int) $this->get($key, $default, $deep);
 228      }
 229  
 230      /**
 231       * Returns the parameter value converted to boolean.
 232       *
 233       * @param string $key     The parameter key
 234       * @param bool   $default The default value if the parameter key does not exist
 235       * @param bool   $deep    If true, a path like foo[bar] will find deeper items
 236       *
 237       * @return bool The filtered value
 238       */
 239      public function getBoolean($key, $default = false, $deep = false)
 240      {
 241          return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
 242      }
 243  
 244      /**
 245       * Filter key.
 246       *
 247       * @param string $key     Key
 248       * @param mixed  $default Default = null
 249       * @param int    $filter  FILTER_* constant
 250       * @param mixed  $options Filter options
 251       * @param bool   $deep    Default = false
 252       *
 253       * @see http://php.net/manual/en/function.filter-var.php
 254       *
 255       * @return mixed
 256       */
 257      public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
 258      {
 259          static $filters = null;
 260  
 261          if (null === $filters) {
 262              foreach (filter_list() as $tmp) {
 263                  $filters[filter_id($tmp)] = 1;
 264              }
 265          }
 266          if (\is_bool($filter) || !isset($filters[$filter]) || \is_array($deep)) {
 267              @trigger_error('Passing the $deep boolean as 3rd argument to the '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0. Remove it altogether as the $deep argument will be removed in 3.0.', E_USER_DEPRECATED);
 268              $tmp = $deep;
 269              $deep = $filter;
 270              $filter = $options;
 271              $options = $tmp;
 272          }
 273  
 274          $value = $this->get($key, $default, $deep);
 275  
 276          // Always turn $options into an array - this allows filter_var option shortcuts.
 277          if (!\is_array($options) && $options) {
 278              $options = array('flags' => $options);
 279          }
 280  
 281          // Add a convenience check for arrays.
 282          if (\is_array($value) && !isset($options['flags'])) {
 283              $options['flags'] = FILTER_REQUIRE_ARRAY;
 284          }
 285  
 286          return filter_var($value, $filter, $options);
 287      }
 288  
 289      /**
 290       * Returns an iterator for parameters.
 291       *
 292       * @return \ArrayIterator An \ArrayIterator instance
 293       */
 294      public function getIterator()
 295      {
 296          return new \ArrayIterator($this->parameters);
 297      }
 298  
 299      /**
 300       * Returns the number of parameters.
 301       *
 302       * @return int The number of parameters
 303       */
 304      public function count()
 305      {
 306          return \count($this->parameters);
 307      }
 308  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1