4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
15 * ParameterBag is a container for key/value pairs.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class ParameterBag implements \IteratorAggregate, \Countable
24 protected $parameters;
27 * @param array $parameters An array of parameters
29 public function __construct(array $parameters = array())
31 $this->parameters = $parameters;
35 * Returns the parameters.
37 * @return array An array of parameters
41 return $this->parameters;
45 * Returns the parameter keys.
47 * @return array An array of parameter keys
49 public function keys()
51 return array_keys($this->parameters);
55 * Replaces the current parameters by a new set.
57 * @param array $parameters An array of parameters
59 public function replace(array $parameters = array())
61 $this->parameters = $parameters;
67 * @param array $parameters An array of parameters
69 public function add(array $parameters = array())
71 $this->parameters = array_replace($this->parameters, $parameters);
75 * Returns a parameter by name.
77 * @param string $key The key
78 * @param mixed $default The default value if the parameter key does not exist
82 public function get($key, $default = null)
84 return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
88 * Sets a parameter by name.
90 * @param string $key The key
91 * @param mixed $value The value
93 public function set($key, $value)
95 $this->parameters[$key] = $value;
99 * Returns true if the parameter is defined.
101 * @param string $key The key
103 * @return bool true if the parameter exists, false otherwise
105 public function has($key)
107 return array_key_exists($key, $this->parameters);
111 * Removes a parameter.
113 * @param string $key The key
115 public function remove($key)
117 unset($this->parameters[$key]);
121 * Returns the alphabetic characters of the parameter value.
123 * @param string $key The parameter key
124 * @param string $default The default value if the parameter key does not exist
126 * @return string The filtered value
128 public function getAlpha($key, $default = '')
130 return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
134 * Returns the alphabetic characters and digits of the parameter value.
136 * @param string $key The parameter key
137 * @param string $default The default value if the parameter key does not exist
139 * @return string The filtered value
141 public function getAlnum($key, $default = '')
143 return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
147 * Returns the digits of the parameter value.
149 * @param string $key The parameter key
150 * @param string $default The default value if the parameter key does not exist
152 * @return string The filtered value
154 public function getDigits($key, $default = '')
156 // we need to remove - and + because they're allowed in the filter
157 return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT));
161 * Returns the parameter value converted to integer.
163 * @param string $key The parameter key
164 * @param int $default The default value if the parameter key does not exist
166 * @return int The filtered value
168 public function getInt($key, $default = 0)
170 return (int) $this->get($key, $default);
174 * Returns the parameter value converted to boolean.
176 * @param string $key The parameter key
177 * @param mixed $default The default value if the parameter key does not exist
179 * @return bool The filtered value
181 public function getBoolean($key, $default = false)
183 return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN);
189 * @param string $key Key
190 * @param mixed $default Default = null
191 * @param int $filter FILTER_* constant
192 * @param mixed $options Filter options
194 * @see http://php.net/manual/en/function.filter-var.php
198 public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array())
200 $value = $this->get($key, $default);
202 // Always turn $options into an array - this allows filter_var option shortcuts.
203 if (!\is_array($options) && $options) {
204 $options = array('flags' => $options);
207 // Add a convenience check for arrays.
208 if (\is_array($value) && !isset($options['flags'])) {
209 $options['flags'] = FILTER_REQUIRE_ARRAY;
212 return filter_var($value, $filter, $options);
216 * Returns an iterator for parameters.
218 * @return \ArrayIterator An \ArrayIterator instance
220 public function getIterator()
222 return new \ArrayIterator($this->parameters);
226 * Returns the number of parameters.
228 * @return int The number of parameters
230 public function count()
232 return \count($this->parameters);