2010-04-08 10:15:19 +01:00
< ? php
/*
* This file is part of the Symfony package .
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-04-08 10:15:19 +01:00
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\HttpFoundation ;
2010-04-08 10:15:19 +01:00
/**
2010-05-03 10:40:23 +01:00
* ParameterBag is a container for key / value pairs .
2010-04-08 10:15:19 +01:00
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2010-04-08 10:15:19 +01:00
*/
2012-03-06 15:07:49 +00:00
class ParameterBag implements \IteratorAggregate , \Countable
2010-04-08 10:15:19 +01:00
{
2011-11-02 15:42:51 +00:00
/**
2011-11-03 05:37:03 +00:00
* Parameter storage .
2011-11-02 15:42:51 +00:00
*/
2010-05-06 12:25:53 +01:00
protected $parameters ;
/**
* @ param array $parameters An array of parameters
*/
public function __construct ( array $parameters = array ())
2010-05-03 10:40:23 +01:00
{
2011-01-30 14:07:02 +00:00
$this -> parameters = $parameters ;
2010-05-03 10:40:23 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Returns the parameters .
*
* @ return array An array of parameters
*/
public function all ()
2010-05-03 10:40:23 +01:00
{
2010-05-06 12:25:53 +01:00
return $this -> parameters ;
2010-05-03 10:40:23 +01:00
}
2010-08-26 09:56:46 +01:00
/**
* Returns the parameter keys .
*
* @ return array An array of parameter keys
*/
public function keys ()
{
return array_keys ( $this -> parameters );
}
2010-05-06 12:25:53 +01:00
/**
* Replaces the current parameters by a new set .
*
* @ param array $parameters An array of parameters
*/
public function replace ( array $parameters = array ())
{
$this -> parameters = $parameters ;
}
2010-06-23 20:42:41 +01:00
/**
* Adds parameters .
*
* @ param array $parameters An array of parameters
*/
public function add ( array $parameters = array ())
{
$this -> parameters = array_replace ( $this -> parameters , $parameters );
}
2010-05-06 12:25:53 +01:00
/**
* Returns a parameter by name .
*
2015-04-03 17:13:34 +01:00
* Note : Finding deep items is deprecated since version 2.8 , to be removed in 3.0 .
*
* @ param string $key The key
2014-11-30 13:33:44 +00:00
* @ param mixed $default The default value if the parameter key does not exist
* @ param bool $deep If true , a path like foo [ bar ] will find deeper items
2011-07-20 09:06:02 +01:00
*
2012-06-25 17:21:41 +01:00
* @ return mixed
*
2012-12-16 12:02:54 +00:00
* @ throws \InvalidArgumentException
2010-05-06 12:25:53 +01:00
*/
2015-04-03 17:13:34 +01:00
public function get ( $key , $default = null , $deep = false )
2010-05-06 12:25:53 +01:00
{
2015-11-27 10:11:22 +00:00
if ( $deep ) {
2017-12-31 04:48:26 +00:00
@ 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 );
2015-04-03 17:13:34 +01:00
}
if ( ! $deep || false === $pos = strpos ( $key , '[' )) {
return array_key_exists ( $key , $this -> parameters ) ? $this -> parameters [ $key ] : $default ;
2011-04-05 09:17:31 +01:00
}
2015-04-03 17:13:34 +01:00
$root = substr ( $key , 0 , $pos );
2011-04-05 09:17:31 +01:00
if ( ! array_key_exists ( $root , $this -> parameters )) {
return $default ;
}
$value = $this -> parameters [ $root ];
$currentKey = null ;
2015-04-03 17:13:34 +01:00
for ( $i = $pos , $c = strlen ( $key ); $i < $c ; ++ $i ) {
$char = $key [ $i ];
2011-04-05 09:17:31 +01:00
if ( '[' === $char ) {
if ( null !== $currentKey ) {
throw new \InvalidArgumentException ( sprintf ( 'Malformed path. Unexpected "[" at position %d.' , $i ));
}
$currentKey = '' ;
2011-12-18 13:42:59 +00:00
} elseif ( ']' === $char ) {
2011-04-05 09:17:31 +01:00
if ( null === $currentKey ) {
throw new \InvalidArgumentException ( sprintf ( 'Malformed path. Unexpected "]" at position %d.' , $i ));
}
if ( ! is_array ( $value ) || ! array_key_exists ( $currentKey , $value )) {
return $default ;
}
$value = $value [ $currentKey ];
$currentKey = null ;
} else {
if ( null === $currentKey ) {
throw new \InvalidArgumentException ( sprintf ( 'Malformed path. Unexpected "%s" at position %d.' , $char , $i ));
}
$currentKey .= $char ;
}
}
if ( null !== $currentKey ) {
2017-04-04 19:32:29 +01:00
throw new \InvalidArgumentException ( 'Malformed path. Path must end with "]".' );
2011-04-05 09:17:31 +01:00
}
return $value ;
}
2010-05-06 12:25:53 +01:00
/**
* Sets a parameter by name .
*
* @ param string $key The key
* @ param mixed $value The value
*/
2010-06-23 15:24:24 +01:00
public function set ( $key , $value )
2010-05-06 12:25:53 +01:00
{
$this -> parameters [ $key ] = $value ;
}
/**
* Returns true if the parameter is defined .
*
* @ param string $key The key
*
2014-11-30 13:33:44 +00:00
* @ return bool true if the parameter exists , false otherwise
2010-05-06 12:25:53 +01:00
*/
public function has ( $key )
{
return array_key_exists ( $key , $this -> parameters );
}
/**
2010-11-23 08:42:19 +00:00
* Removes a parameter .
2010-05-06 12:25:53 +01:00
*
* @ param string $key The key
*/
2010-11-23 08:42:19 +00:00
public function remove ( $key )
2010-05-06 12:25:53 +01:00
{
unset ( $this -> parameters [ $key ]);
}
/**
* Returns the alphabetic characters of the parameter value .
*
2014-11-30 13:33:44 +00:00
* @ param string $key The parameter key
2015-10-28 22:41:55 +00:00
* @ param string $default The default value if the parameter key does not exist
2014-11-30 13:33:44 +00:00
* @ param bool $deep If true , a path like foo [ bar ] will find deeper items
2010-05-06 12:25:53 +01:00
*
* @ return string The filtered value
*/
2011-05-10 10:16:25 +01:00
public function getAlpha ( $key , $default = '' , $deep = false )
2010-05-06 12:25:53 +01:00
{
2011-05-10 10:16:25 +01:00
return preg_replace ( '/[^[:alpha:]]/' , '' , $this -> get ( $key , $default , $deep ));
2010-05-06 12:25:53 +01:00
}
/**
* Returns the alphabetic characters and digits of the parameter value .
*
2014-11-30 13:33:44 +00:00
* @ param string $key The parameter key
2015-10-28 22:41:55 +00:00
* @ param string $default The default value if the parameter key does not exist
2014-11-30 13:33:44 +00:00
* @ param bool $deep If true , a path like foo [ bar ] will find deeper items
2010-05-06 12:25:53 +01:00
*
* @ return string The filtered value
*/
2011-05-10 10:16:25 +01:00
public function getAlnum ( $key , $default = '' , $deep = false )
2010-05-06 12:25:53 +01:00
{
2011-05-10 10:16:25 +01:00
return preg_replace ( '/[^[:alnum:]]/' , '' , $this -> get ( $key , $default , $deep ));
2010-05-06 12:25:53 +01:00
}
/**
* Returns the digits of the parameter value .
*
2014-11-30 13:33:44 +00:00
* @ param string $key The parameter key
2015-10-28 22:41:55 +00:00
* @ param string $default The default value if the parameter key does not exist
2014-11-30 13:33:44 +00:00
* @ param bool $deep If true , a path like foo [ bar ] will find deeper items
2010-05-06 12:25:53 +01:00
*
* @ return string The filtered value
*/
2011-05-10 10:16:25 +01:00
public function getDigits ( $key , $default = '' , $deep = false )
2010-05-06 12:25:53 +01:00
{
2012-02-21 17:08:57 +00:00
// we need to remove - and + because they're allowed in the filter
2015-11-27 10:11:22 +00:00
return str_replace ( array ( '-' , '+' ), '' , $this -> filter ( $key , $default , FILTER_SANITIZE_NUMBER_INT , array (), $deep ));
2010-05-06 12:25:53 +01:00
}
/**
* Returns the parameter value converted to integer .
*
2014-11-30 13:33:44 +00:00
* @ param string $key The parameter key
2015-10-28 22:41:55 +00:00
* @ param int $default The default value if the parameter key does not exist
2014-11-30 13:33:44 +00:00
* @ param bool $deep If true , a path like foo [ bar ] will find deeper items
2010-05-06 12:25:53 +01:00
*
2014-11-30 13:33:44 +00:00
* @ return int The filtered value
2010-05-06 12:25:53 +01:00
*/
2011-05-10 10:16:25 +01:00
public function getInt ( $key , $default = 0 , $deep = false )
2010-05-06 12:25:53 +01:00
{
2011-05-10 10:16:25 +01:00
return ( int ) $this -> get ( $key , $default , $deep );
2010-05-06 12:25:53 +01:00
}
2011-10-29 11:05:45 +01:00
2014-06-10 03:24:55 +01:00
/**
* Returns the parameter value converted to boolean .
*
2014-12-05 20:11:30 +00:00
* @ param string $key The parameter key
* @ param mixed $default The default value if the parameter key does not exist
* @ param bool $deep If true , a path like foo [ bar ] will find deeper items
2014-06-10 03:24:55 +01:00
*
2014-12-05 20:11:30 +00:00
* @ return bool The filtered value
2014-06-10 03:24:55 +01:00
*/
public function getBoolean ( $key , $default = false , $deep = false )
{
2015-11-27 10:11:22 +00:00
return $this -> filter ( $key , $default , FILTER_VALIDATE_BOOLEAN , array (), $deep );
2014-06-10 03:24:55 +01:00
}
2011-09-25 23:20:08 +01:00
/**
* Filter key .
2011-10-29 11:05:45 +01:00
*
2016-06-28 06:50:50 +01:00
* @ param string $key Key
* @ param mixed $default Default = null
* @ param int $filter FILTER_ * constant
* @ param mixed $options Filter options
2016-06-29 06:29:29 +01:00
* @ param bool $deep Default = false
2011-10-29 11:05:45 +01:00
*
2011-09-27 15:29:32 +01:00
* @ see http :// php . net / manual / en / function . filter - var . php
2011-09-25 23:20:08 +01:00
*
* @ return mixed
*/
2015-11-27 10:11:22 +00:00
public function filter ( $key , $default = null , $filter = FILTER_DEFAULT , $options = array (), $deep = false )
2011-09-25 23:20:08 +01:00
{
2015-11-27 10:11:22 +00:00
static $filters = null ;
if ( null === $filters ) {
foreach ( filter_list () as $tmp ) {
$filters [ filter_id ( $tmp )] = 1 ;
}
}
if ( is_bool ( $filter ) || ! isset ( $filters [ $filter ]) || is_array ( $deep )) {
2017-12-31 04:48:26 +00:00
@ 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 );
2015-11-27 10:11:22 +00:00
$tmp = $deep ;
$deep = $filter ;
$filter = $options ;
$options = $tmp ;
}
2011-09-25 23:20:08 +01:00
$value = $this -> get ( $key , $default , $deep );
2011-10-29 11:05:45 +01:00
2011-09-27 15:29:32 +01:00
// Always turn $options into an array - this allows filter_var option shortcuts.
if ( ! is_array ( $options ) && $options ) {
$options = array ( 'flags' => $options );
}
2011-10-29 11:05:45 +01:00
2011-09-27 15:29:32 +01:00
// Add a convenience check for arrays.
if ( is_array ( $value ) && ! isset ( $options [ 'flags' ])) {
2011-09-25 23:20:08 +01:00
$options [ 'flags' ] = FILTER_REQUIRE_ARRAY ;
}
2011-10-29 11:05:45 +01:00
2011-09-25 23:20:08 +01:00
return filter_var ( $value , $filter , $options );
}
2012-03-06 15:07:49 +00:00
/**
2012-03-23 12:49:00 +00:00
* Returns an iterator for parameters .
2012-03-06 15:07:49 +00:00
*
2012-03-23 12:49:00 +00:00
* @ return \ArrayIterator An \ArrayIterator instance
2012-03-06 15:07:49 +00:00
*/
public function getIterator ()
{
return new \ArrayIterator ( $this -> parameters );
}
/**
2012-03-23 12:49:00 +00:00
* Returns the number of parameters .
2012-03-06 15:07:49 +00:00
*
2012-03-23 12:49:00 +00:00
* @ return int The number of parameters
2012-03-06 15:07:49 +00:00
*/
public function count ()
{
return count ( $this -> parameters );
}
2010-04-08 10:15:19 +01:00
}