2010-04-08 10:15:19 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
*
|
2010-10-17 12:45:15 +01:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
2010-04-08 10:15:19 +01:00
|
|
|
*/
|
2010-05-03 10:40:23 +01:00
|
|
|
class ParameterBag
|
2010-04-08 10:15:19 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $parameters;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param string $key The key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*/
|
|
|
|
public function get($key, $default = null)
|
|
|
|
{
|
|
|
|
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* @return Boolean true if the parameter exists, false otherwise
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @return string The filtered value
|
|
|
|
*/
|
|
|
|
public function getAlpha($key, $default = '')
|
|
|
|
{
|
|
|
|
return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the alphabetic characters and digits of the parameter value.
|
|
|
|
*
|
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @return string The filtered value
|
|
|
|
*/
|
|
|
|
public function getAlnum($key, $default = '')
|
|
|
|
{
|
|
|
|
return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the digits of the parameter value.
|
|
|
|
*
|
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @return string The filtered value
|
|
|
|
*/
|
|
|
|
public function getDigits($key, $default = '')
|
|
|
|
{
|
|
|
|
return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parameter value converted to integer.
|
|
|
|
*
|
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
*
|
|
|
|
* @return string The filtered value
|
|
|
|
*/
|
|
|
|
public function getInt($key, $default = 0)
|
|
|
|
{
|
|
|
|
return (int) $this->get($key, $default);
|
|
|
|
}
|
2010-04-08 10:15:19 +01:00
|
|
|
}
|