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>
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-08-26 09:56:46 +01:00
|
|
|
*/
|
|
|
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-06-23 20:42:41 +01:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2011-05-10 19:24:58 +01:00
|
|
|
* @param string $path The key
|
2011-05-10 10:16:25 +01:00
|
|
|
* @param mixed $default The default value
|
|
|
|
* @param boolean $deep
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-05-10 10:16:25 +01:00
|
|
|
public function get($path, $default = null, $deep = false)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-05-10 10:16:25 +01:00
|
|
|
if (!$deep || false === $pos = strpos($path, '[')) {
|
|
|
|
return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
|
2011-04-05 09:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$root = substr($path, 0, $pos);
|
|
|
|
if (!array_key_exists($root, $this->parameters)) {
|
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
|
|
|
$value = $this->parameters[$root];
|
|
|
|
$currentKey = null;
|
|
|
|
for ($i=$pos,$c=strlen($path); $i<$c; $i++) {
|
|
|
|
$char = $path[$i];
|
|
|
|
|
|
|
|
if ('[' === $char) {
|
|
|
|
if (null !== $currentKey) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
|
|
|
|
}
|
|
|
|
|
|
|
|
$currentKey = '';
|
|
|
|
} else if (']' === $char) {
|
|
|
|
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) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2011-05-10 10:16:25 +01:00
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
* @param boolean $deep
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The filtered value
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2011-05-10 10:16:25 +01:00
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
* @param boolean $deep
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The filtered value
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
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.
|
|
|
|
*
|
2011-05-10 10:16:25 +01:00
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
* @param boolean $deep
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The filtered value
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-05-10 10:16:25 +01:00
|
|
|
public function getDigits($key, $default = '', $deep = false)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
2011-05-10 10:16:25 +01:00
|
|
|
return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default, $deep));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the parameter value converted to integer.
|
|
|
|
*
|
2011-05-10 10:16:25 +01:00
|
|
|
* @param string $key The parameter key
|
|
|
|
* @param mixed $default The default value
|
|
|
|
* @param boolean $deep
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return string The filtered value
|
2011-07-20 09:06:02 +01:00
|
|
|
*
|
|
|
|
* @api
|
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-09-25 23:20:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter key.
|
|
|
|
*
|
|
|
|
* @param string $key Key.
|
|
|
|
* @param mixed $default Default = null.
|
|
|
|
* @param boolean $deep Default = false.
|
|
|
|
* @param integer $filter FILTER_* constant.
|
2011-09-27 10:35:51 +01:00
|
|
|
* @param array $options Filter options - can be an array of options or FILTER_* constant.
|
2011-09-25 23:20:08 +01:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, array $options=array())
|
|
|
|
{
|
|
|
|
$value = $this->get($key, $default, $deep);
|
|
|
|
if (is_array($value)) {
|
|
|
|
$options['flags'] = FILTER_REQUIRE_ARRAY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter_var($value, $filter, $options);
|
|
|
|
}
|
2010-04-08 10:15:19 +01:00
|
|
|
}
|