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
|
|
|
*/
|
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-12-18 13:33:54 +00:00
|
|
|
*
|
2011-11-02 15:42:51 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
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.
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param string $path The 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
|
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
|
|
|
|
*
|
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;
|
2012-04-18 16:10:56 +01:00
|
|
|
for ($i = $pos, $c = strlen($path); $i < $c; $i++) {
|
2011-04-05 09:17:31 +01:00
|
|
|
$char = $path[$i];
|
|
|
|
|
|
|
|
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) {
|
|
|
|
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
|
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return bool 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.
|
|
|
|
*
|
2014-11-30 13:33:44 +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
|
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.
|
|
|
|
*
|
2014-11-30 13:33:44 +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
|
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.
|
|
|
|
*
|
2014-11-30 13:33:44 +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
|
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
|
|
|
{
|
2012-02-21 17:08:57 +00:00
|
|
|
// we need to remove - and + because they're allowed in the filter
|
2012-03-02 20:43:08 +00:00
|
|
|
return str_replace(array('-', '+'), '', $this->filter($key, $default, $deep, FILTER_SANITIZE_NUMBER_INT));
|
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
|
|
|
|
* @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
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @return int 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-10-29 11:05:45 +01:00
|
|
|
|
2011-09-25 23:20:08 +01:00
|
|
|
/**
|
|
|
|
* Filter key.
|
2011-10-29 11:05:45 +01:00
|
|
|
*
|
2014-11-30 13:33:44 +00:00
|
|
|
* @param string $key Key.
|
|
|
|
* @param mixed $default Default = null.
|
|
|
|
* @param bool $deep Default = false.
|
|
|
|
* @param int $filter FILTER_* constant.
|
|
|
|
* @param mixed $options Filter options.
|
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
|
|
|
|
*/
|
2014-09-21 19:53:12 +01:00
|
|
|
public function filter($key, $default = null, $deep = false, $filter = FILTER_DEFAULT, $options = array())
|
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
|
|
|
}
|