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
26 protected $parameters;
31 * @param array $parameters An array of parameters
33 public function __construct(array $parameters = array())
35 $this->parameters = $parameters;
39 * Returns the parameters.
41 * @return array An array of parameters
45 return $this->parameters;
49 * Returns the parameter keys.
51 * @return array An array of parameter keys
53 public function keys()
55 return array_keys($this->parameters);
59 * Replaces the current parameters by a new set.
61 * @param array $parameters An array of parameters
63 public function replace(array $parameters = array())
65 $this->parameters = $parameters;
71 * @param array $parameters An array of parameters
73 public function add(array $parameters = array())
75 $this->parameters = array_replace($this->parameters, $parameters);
79 * Returns a parameter by name.
81 * Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
83 * @param string $key The key
84 * @param mixed $default The default value if the parameter key does not exist
85 * @param bool $deep If true, a path like foo[bar] will find deeper items
89 * @throws \InvalidArgumentException
91 public function get($key, $default = null, $deep = false)
94 @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
97 if (!$deep || false === $pos = strpos($key, '[')) {
98 return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
101 $root = substr($key, 0, $pos);
102 if (!array_key_exists($root, $this->parameters)) {
106 $value = $this->parameters[$root];
108 for ($i = $pos, $c = strlen($key); $i < $c; ++$i) {
112 if (null !== $currentKey) {
113 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
117 } elseif (']' === $char) {
118 if (null === $currentKey) {
119 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
122 if (!is_array($value) || !array_key_exists($currentKey, $value)) {
126 $value = $value[$currentKey];
129 if (null === $currentKey) {
130 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
133 $currentKey .= $char;
137 if (null !== $currentKey) {
138 throw new \InvalidArgumentException('Malformed path. Path must end with "]".');
145 * Sets a parameter by name.
147 * @param string $key The key
148 * @param mixed $value The value
150 public function set($key, $value)
152 $this->parameters[$key] = $value;
156 * Returns true if the parameter is defined.
158 * @param string $key The key
160 * @return bool true if the parameter exists, false otherwise
162 public function has($key)
164 return array_key_exists($key, $this->parameters);
168 * Removes a parameter.
170 * @param string $key The key
172 public function remove($key)
174 unset($this->parameters[$key]);
178 * Returns the alphabetic characters of the parameter value.
180 * @param string $key The parameter key
181 * @param string $default The default value if the parameter key does not exist
182 * @param bool $deep If true, a path like foo[bar] will find deeper items
184 * @return string The filtered value
186 public function getAlpha($key, $default = '', $deep = false)
188 return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
192 * Returns the alphabetic characters and digits of the parameter value.
194 * @param string $key The parameter key
195 * @param string $default The default value if the parameter key does not exist
196 * @param bool $deep If true, a path like foo[bar] will find deeper items
198 * @return string The filtered value
200 public function getAlnum($key, $default = '', $deep = false)
202 return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
206 * Returns the digits of the parameter value.
208 * @param string $key The parameter key
209 * @param string $default The default value if the parameter key does not exist
210 * @param bool $deep If true, a path like foo[bar] will find deeper items
212 * @return string The filtered value
214 public function getDigits($key, $default = '', $deep = false)
216 // we need to remove - and + because they're allowed in the filter
217 return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT, array(), $deep));
221 * Returns the parameter value converted to integer.
223 * @param string $key The parameter key
224 * @param int $default The default value if the parameter key does not exist
225 * @param bool $deep If true, a path like foo[bar] will find deeper items
227 * @return int The filtered value
229 public function getInt($key, $default = 0, $deep = false)
231 return (int) $this->get($key, $default, $deep);
235 * Returns the parameter value converted to boolean.
237 * @param string $key The parameter key
238 * @param mixed $default The default value if the parameter key does not exist
239 * @param bool $deep If true, a path like foo[bar] will find deeper items
241 * @return bool The filtered value
243 public function getBoolean($key, $default = false, $deep = false)
245 return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
251 * @param string $key Key
252 * @param mixed $default Default = null
253 * @param int $filter FILTER_* constant
254 * @param mixed $options Filter options
255 * @param bool $deep Default = false
257 * @see http://php.net/manual/en/function.filter-var.php
261 public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
263 static $filters = null;
265 if (null === $filters) {
266 foreach (filter_list() as $tmp) {
267 $filters[filter_id($tmp)] = 1;
270 if (is_bool($filter) || !isset($filters[$filter]) || is_array($deep)) {
271 @trigger_error('Passing the $deep boolean as 3rd argument to the '.__METHOD__.' method is deprecated since version 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);
278 $value = $this->get($key, $default, $deep);
280 // Always turn $options into an array - this allows filter_var option shortcuts.
281 if (!is_array($options) && $options) {
282 $options = array('flags' => $options);
285 // Add a convenience check for arrays.
286 if (is_array($value) && !isset($options['flags'])) {
287 $options['flags'] = FILTER_REQUIRE_ARRAY;
290 return filter_var($value, $filter, $options);
294 * Returns an iterator for parameters.
296 * @return \ArrayIterator An \ArrayIterator instance
298 public function getIterator()
300 return new \ArrayIterator($this->parameters);
304 * Returns the number of parameters.
306 * @return int The number of parameters
308 public function count()
310 return count($this->parameters);