composer package updates
[openemr.git] / vendor / symfony / http-foundation / ParameterBag.php
blobb524c27e526e42d66658504b6f8824fc03e48a5b
1 <?php
3 /*
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;
14 /**
15 * ParameterBag is a container for key/value pairs.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class ParameterBag implements \IteratorAggregate, \Countable
21 /**
22 * Parameter storage.
24 protected $parameters;
26 /**
27 * @param array $parameters An array of parameters
29 public function __construct(array $parameters = array())
31 $this->parameters = $parameters;
34 /**
35 * Returns the parameters.
37 * @return array An array of parameters
39 public function all()
41 return $this->parameters;
44 /**
45 * Returns the parameter keys.
47 * @return array An array of parameter keys
49 public function keys()
51 return array_keys($this->parameters);
54 /**
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;
64 /**
65 * Adds parameters.
67 * @param array $parameters An array of parameters
69 public function add(array $parameters = array())
71 $this->parameters = array_replace($this->parameters, $parameters);
74 /**
75 * Returns a parameter by name.
77 * Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
79 * @param string $key The key
80 * @param mixed $default The default value if the parameter key does not exist
81 * @param bool $deep If true, a path like foo[bar] will find deeper items
83 * @return mixed
85 * @throws \InvalidArgumentException
87 public function get($key, $default = null, $deep = false)
89 if ($deep) {
90 @trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since Symfony 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
93 if (!$deep || false === $pos = strpos($key, '[')) {
94 return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
97 $root = substr($key, 0, $pos);
98 if (!array_key_exists($root, $this->parameters)) {
99 return $default;
102 $value = $this->parameters[$root];
103 $currentKey = null;
104 for ($i = $pos, $c = strlen($key); $i < $c; ++$i) {
105 $char = $key[$i];
107 if ('[' === $char) {
108 if (null !== $currentKey) {
109 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
112 $currentKey = '';
113 } elseif (']' === $char) {
114 if (null === $currentKey) {
115 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
118 if (!is_array($value) || !array_key_exists($currentKey, $value)) {
119 return $default;
122 $value = $value[$currentKey];
123 $currentKey = null;
124 } else {
125 if (null === $currentKey) {
126 throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
129 $currentKey .= $char;
133 if (null !== $currentKey) {
134 throw new \InvalidArgumentException('Malformed path. Path must end with "]".');
137 return $value;
141 * Sets a parameter by name.
143 * @param string $key The key
144 * @param mixed $value The value
146 public function set($key, $value)
148 $this->parameters[$key] = $value;
152 * Returns true if the parameter is defined.
154 * @param string $key The key
156 * @return bool true if the parameter exists, false otherwise
158 public function has($key)
160 return array_key_exists($key, $this->parameters);
164 * Removes a parameter.
166 * @param string $key The key
168 public function remove($key)
170 unset($this->parameters[$key]);
174 * Returns the alphabetic characters of the parameter value.
176 * @param string $key The parameter key
177 * @param string $default The default value if the parameter key does not exist
178 * @param bool $deep If true, a path like foo[bar] will find deeper items
180 * @return string The filtered value
182 public function getAlpha($key, $default = '', $deep = false)
184 return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
188 * Returns the alphabetic characters and digits of the parameter value.
190 * @param string $key The parameter key
191 * @param string $default The default value if the parameter key does not exist
192 * @param bool $deep If true, a path like foo[bar] will find deeper items
194 * @return string The filtered value
196 public function getAlnum($key, $default = '', $deep = false)
198 return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
202 * Returns the digits of the parameter value.
204 * @param string $key The parameter key
205 * @param string $default The default value if the parameter key does not exist
206 * @param bool $deep If true, a path like foo[bar] will find deeper items
208 * @return string The filtered value
210 public function getDigits($key, $default = '', $deep = false)
212 // we need to remove - and + because they're allowed in the filter
213 return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT, array(), $deep));
217 * Returns the parameter value converted to integer.
219 * @param string $key The parameter key
220 * @param int $default The default value if the parameter key does not exist
221 * @param bool $deep If true, a path like foo[bar] will find deeper items
223 * @return int The filtered value
225 public function getInt($key, $default = 0, $deep = false)
227 return (int) $this->get($key, $default, $deep);
231 * Returns the parameter value converted to boolean.
233 * @param string $key The parameter key
234 * @param mixed $default The default value if the parameter key does not exist
235 * @param bool $deep If true, a path like foo[bar] will find deeper items
237 * @return bool The filtered value
239 public function getBoolean($key, $default = false, $deep = false)
241 return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
245 * Filter key.
247 * @param string $key Key
248 * @param mixed $default Default = null
249 * @param int $filter FILTER_* constant
250 * @param mixed $options Filter options
251 * @param bool $deep Default = false
253 * @see http://php.net/manual/en/function.filter-var.php
255 * @return mixed
257 public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
259 static $filters = null;
261 if (null === $filters) {
262 foreach (filter_list() as $tmp) {
263 $filters[filter_id($tmp)] = 1;
266 if (is_bool($filter) || !isset($filters[$filter]) || is_array($deep)) {
267 @trigger_error('Passing the $deep boolean as 3rd argument to the '.__METHOD__.' method is deprecated since Symfony 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);
268 $tmp = $deep;
269 $deep = $filter;
270 $filter = $options;
271 $options = $tmp;
274 $value = $this->get($key, $default, $deep);
276 // Always turn $options into an array - this allows filter_var option shortcuts.
277 if (!is_array($options) && $options) {
278 $options = array('flags' => $options);
281 // Add a convenience check for arrays.
282 if (is_array($value) && !isset($options['flags'])) {
283 $options['flags'] = FILTER_REQUIRE_ARRAY;
286 return filter_var($value, $filter, $options);
290 * Returns an iterator for parameters.
292 * @return \ArrayIterator An \ArrayIterator instance
294 public function getIterator()
296 return new \ArrayIterator($this->parameters);
300 * Returns the number of parameters.
302 * @return int The number of parameters
304 public function count()
306 return count($this->parameters);