vendor/symfony/src/Symfony/Component/HttpFoundation/ParameterBag.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     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  * @api
       
    20  */
       
    21 class ParameterBag
       
    22 {
       
    23     protected $parameters;
       
    24 
       
    25     /**
       
    26      * Constructor.
       
    27      *
       
    28      * @param array $parameters An array of parameters
       
    29      *
       
    30      * @api
       
    31      */
       
    32     public function __construct(array $parameters = array())
       
    33     {
       
    34         $this->parameters = $parameters;
       
    35     }
       
    36 
       
    37     /**
       
    38      * Returns the parameters.
       
    39      *
       
    40      * @return array An array of parameters
       
    41      *
       
    42      * @api
       
    43      */
       
    44     public function all()
       
    45     {
       
    46         return $this->parameters;
       
    47     }
       
    48 
       
    49     /**
       
    50      * Returns the parameter keys.
       
    51      *
       
    52      * @return array An array of parameter keys
       
    53      *
       
    54      * @api
       
    55      */
       
    56     public function keys()
       
    57     {
       
    58         return array_keys($this->parameters);
       
    59     }
       
    60 
       
    61     /**
       
    62      * Replaces the current parameters by a new set.
       
    63      *
       
    64      * @param array $parameters An array of parameters
       
    65      *
       
    66      * @api
       
    67      */
       
    68     public function replace(array $parameters = array())
       
    69     {
       
    70         $this->parameters = $parameters;
       
    71     }
       
    72 
       
    73     /**
       
    74      * Adds parameters.
       
    75      *
       
    76      * @param array $parameters An array of parameters
       
    77      *
       
    78      * @api
       
    79      */
       
    80     public function add(array $parameters = array())
       
    81     {
       
    82         $this->parameters = array_replace($this->parameters, $parameters);
       
    83     }
       
    84 
       
    85     /**
       
    86      * Returns a parameter by name.
       
    87      *
       
    88      * @param string  $path    The key
       
    89      * @param mixed   $default The default value
       
    90      * @param boolean $deep
       
    91      *
       
    92      * @api
       
    93      */
       
    94     public function get($path, $default = null, $deep = false)
       
    95     {
       
    96         if (!$deep || false === $pos = strpos($path, '[')) {
       
    97             return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
       
    98         }
       
    99 
       
   100         $root = substr($path, 0, $pos);
       
   101         if (!array_key_exists($root, $this->parameters)) {
       
   102             return $default;
       
   103         }
       
   104 
       
   105         $value = $this->parameters[$root];
       
   106         $currentKey = null;
       
   107         for ($i=$pos,$c=strlen($path); $i<$c; $i++) {
       
   108             $char = $path[$i];
       
   109 
       
   110             if ('[' === $char) {
       
   111                 if (null !== $currentKey) {
       
   112                     throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
       
   113                 }
       
   114 
       
   115                 $currentKey = '';
       
   116             } else if (']' === $char) {
       
   117                 if (null === $currentKey) {
       
   118                     throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
       
   119                 }
       
   120 
       
   121                 if (!is_array($value) || !array_key_exists($currentKey, $value)) {
       
   122                     return $default;
       
   123                 }
       
   124 
       
   125                 $value = $value[$currentKey];
       
   126                 $currentKey = null;
       
   127             } else {
       
   128                 if (null === $currentKey) {
       
   129                     throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
       
   130                 }
       
   131 
       
   132                 $currentKey .= $char;
       
   133             }
       
   134         }
       
   135 
       
   136         if (null !== $currentKey) {
       
   137             throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
       
   138         }
       
   139 
       
   140         return $value;
       
   141     }
       
   142 
       
   143     /**
       
   144      * Sets a parameter by name.
       
   145      *
       
   146      * @param string $key   The key
       
   147      * @param mixed  $value The value
       
   148      *
       
   149      * @api
       
   150      */
       
   151     public function set($key, $value)
       
   152     {
       
   153         $this->parameters[$key] = $value;
       
   154     }
       
   155 
       
   156     /**
       
   157      * Returns true if the parameter is defined.
       
   158      *
       
   159      * @param string $key The key
       
   160      *
       
   161      * @return Boolean true if the parameter exists, false otherwise
       
   162      *
       
   163      * @api
       
   164      */
       
   165     public function has($key)
       
   166     {
       
   167         return array_key_exists($key, $this->parameters);
       
   168     }
       
   169 
       
   170     /**
       
   171      * Removes a parameter.
       
   172      *
       
   173      * @param string $key The key
       
   174      *
       
   175      * @api
       
   176      */
       
   177     public function remove($key)
       
   178     {
       
   179         unset($this->parameters[$key]);
       
   180     }
       
   181 
       
   182     /**
       
   183      * Returns the alphabetic characters of the parameter value.
       
   184      *
       
   185      * @param string  $key     The parameter key
       
   186      * @param mixed   $default The default value
       
   187      * @param boolean $deep
       
   188      *
       
   189      * @return string The filtered value
       
   190      *
       
   191      * @api
       
   192      */
       
   193     public function getAlpha($key, $default = '', $deep = false)
       
   194     {
       
   195         return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
       
   196     }
       
   197 
       
   198     /**
       
   199      * Returns the alphabetic characters and digits of the parameter value.
       
   200      *
       
   201      * @param string  $key     The parameter key
       
   202      * @param mixed   $default The default value
       
   203      * @param boolean $deep
       
   204      *
       
   205      * @return string The filtered value
       
   206      *
       
   207      * @api
       
   208      */
       
   209     public function getAlnum($key, $default = '', $deep = false)
       
   210     {
       
   211         return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
       
   212     }
       
   213 
       
   214     /**
       
   215      * Returns the digits of the parameter value.
       
   216      *
       
   217      * @param string  $key     The parameter key
       
   218      * @param mixed   $default The default value
       
   219      * @param boolean $deep
       
   220      *
       
   221      * @return string The filtered value
       
   222      *
       
   223      * @api
       
   224      */
       
   225     public function getDigits($key, $default = '', $deep = false)
       
   226     {
       
   227         return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default, $deep));
       
   228     }
       
   229 
       
   230     /**
       
   231      * Returns the parameter value converted to integer.
       
   232      *
       
   233      * @param string  $key     The parameter key
       
   234      * @param mixed   $default The default value
       
   235      * @param boolean $deep
       
   236      *
       
   237      * @return string The filtered value
       
   238      *
       
   239      * @api
       
   240      */
       
   241     public function getInt($key, $default = 0, $deep = false)
       
   242     {
       
   243         return (int) $this->get($key, $default, $deep);
       
   244     }
       
   245 }