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 * @param string $key The key
82 * @param mixed $default The default value if the parameter key does not exist
86 public function get($key, $default = null)
88 return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
92 * Sets a parameter by name.
94 * @param string $key The key
95 * @param mixed $value The value
97 public function set($key, $value)
99 $this->parameters[$key] = $value;
103 * Returns true if the parameter is defined.
105 * @param string $key The key
107 * @return bool true if the parameter exists, false otherwise
109 public function has($key)
111 return array_key_exists($key, $this->parameters);
115 * Removes a parameter.
117 * @param string $key The key
119 public function remove($key)
121 unset($this->parameters[$key]);
125 * Returns the alphabetic characters of the parameter value.
127 * @param string $key The parameter key
128 * @param string $default The default value if the parameter key does not exist
130 * @return string The filtered value
132 public function getAlpha($key, $default = '')
134 return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
138 * Returns the alphabetic characters and digits of the parameter value.
140 * @param string $key The parameter key
141 * @param string $default The default value if the parameter key does not exist
143 * @return string The filtered value
145 public function getAlnum($key, $default = '')
147 return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
151 * Returns the digits of the parameter value.
153 * @param string $key The parameter key
154 * @param string $default The default value if the parameter key does not exist
156 * @return string The filtered value
158 public function getDigits($key, $default = '')
160 // we need to remove - and + because they're allowed in the filter
161 return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT));
165 * Returns the parameter value converted to integer.
167 * @param string $key The parameter key
168 * @param int $default The default value if the parameter key does not exist
170 * @return int The filtered value
172 public function getInt($key, $default = 0)
174 return (int) $this->get($key, $default);
178 * Returns the parameter value converted to boolean.
180 * @param string $key The parameter key
181 * @param mixed $default The default value if the parameter key does not exist
183 * @return bool The filtered value
185 public function getBoolean($key, $default = false)
187 return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN);
193 * @param string $key Key
194 * @param mixed $default Default = null
195 * @param int $filter FILTER_* constant
196 * @param mixed $options Filter options
198 * @see http://php.net/manual/en/function.filter-var.php
202 public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array())
204 $value = $this->get($key, $default);
206 // Always turn $options into an array - this allows filter_var option shortcuts.
207 if (!is_array($options) && $options) {
208 $options = array('flags' => $options);
211 // Add a convenience check for arrays.
212 if (is_array($value) && !isset($options['flags'])) {
213 $options['flags'] = FILTER_REQUIRE_ARRAY;
216 return filter_var($value, $filter, $options);
220 * Returns an iterator for parameters.
222 * @return \ArrayIterator An \ArrayIterator instance
224 public function getIterator()
226 return new \ArrayIterator($this->parameters);
230 * Returns the number of parameters.
232 * @return int The number of parameters
234 public function count()
236 return count($this->parameters);