ef13494ee00b1f524f6e975c059ed3aa26535612
[yaffs-website] / vendor / symfony / http-foundation / ParameterBag.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation;
13
14 /**
15  * ParameterBag is a container for key/value pairs.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class ParameterBag implements \IteratorAggregate, \Countable
20 {
21     /**
22      * Parameter storage.
23      *
24      * @var array
25      */
26     protected $parameters;
27
28     /**
29      * Constructor.
30      *
31      * @param array $parameters An array of parameters
32      */
33     public function __construct(array $parameters = array())
34     {
35         $this->parameters = $parameters;
36     }
37
38     /**
39      * Returns the parameters.
40      *
41      * @return array An array of parameters
42      */
43     public function all()
44     {
45         return $this->parameters;
46     }
47
48     /**
49      * Returns the parameter keys.
50      *
51      * @return array An array of parameter keys
52      */
53     public function keys()
54     {
55         return array_keys($this->parameters);
56     }
57
58     /**
59      * Replaces the current parameters by a new set.
60      *
61      * @param array $parameters An array of parameters
62      */
63     public function replace(array $parameters = array())
64     {
65         $this->parameters = $parameters;
66     }
67
68     /**
69      * Adds parameters.
70      *
71      * @param array $parameters An array of parameters
72      */
73     public function add(array $parameters = array())
74     {
75         $this->parameters = array_replace($this->parameters, $parameters);
76     }
77
78     /**
79      * Returns a parameter by name.
80      *
81      * Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
82      *
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
86      *
87      * @return mixed
88      *
89      * @throws \InvalidArgumentException
90      */
91     public function get($key, $default = null, $deep = false)
92     {
93         if ($deep) {
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);
95         }
96
97         if (!$deep || false === $pos = strpos($key, '[')) {
98             return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
99         }
100
101         $root = substr($key, 0, $pos);
102         if (!array_key_exists($root, $this->parameters)) {
103             return $default;
104         }
105
106         $value = $this->parameters[$root];
107         $currentKey = null;
108         for ($i = $pos, $c = strlen($key); $i < $c; ++$i) {
109             $char = $key[$i];
110
111             if ('[' === $char) {
112                 if (null !== $currentKey) {
113                     throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
114                 }
115
116                 $currentKey = '';
117             } elseif (']' === $char) {
118                 if (null === $currentKey) {
119                     throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
120                 }
121
122                 if (!is_array($value) || !array_key_exists($currentKey, $value)) {
123                     return $default;
124                 }
125
126                 $value = $value[$currentKey];
127                 $currentKey = null;
128             } else {
129                 if (null === $currentKey) {
130                     throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
131                 }
132
133                 $currentKey .= $char;
134             }
135         }
136
137         if (null !== $currentKey) {
138             throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
139         }
140
141         return $value;
142     }
143
144     /**
145      * Sets a parameter by name.
146      *
147      * @param string $key   The key
148      * @param mixed  $value The value
149      */
150     public function set($key, $value)
151     {
152         $this->parameters[$key] = $value;
153     }
154
155     /**
156      * Returns true if the parameter is defined.
157      *
158      * @param string $key The key
159      *
160      * @return bool true if the parameter exists, false otherwise
161      */
162     public function has($key)
163     {
164         return array_key_exists($key, $this->parameters);
165     }
166
167     /**
168      * Removes a parameter.
169      *
170      * @param string $key The key
171      */
172     public function remove($key)
173     {
174         unset($this->parameters[$key]);
175     }
176
177     /**
178      * Returns the alphabetic characters of the parameter value.
179      *
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
183      *
184      * @return string The filtered value
185      */
186     public function getAlpha($key, $default = '', $deep = false)
187     {
188         return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
189     }
190
191     /**
192      * Returns the alphabetic characters and digits of the parameter value.
193      *
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
197      *
198      * @return string The filtered value
199      */
200     public function getAlnum($key, $default = '', $deep = false)
201     {
202         return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
203     }
204
205     /**
206      * Returns the digits of the parameter value.
207      *
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
211      *
212      * @return string The filtered value
213      */
214     public function getDigits($key, $default = '', $deep = false)
215     {
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));
218     }
219
220     /**
221      * Returns the parameter value converted to integer.
222      *
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
226      *
227      * @return int The filtered value
228      */
229     public function getInt($key, $default = 0, $deep = false)
230     {
231         return (int) $this->get($key, $default, $deep);
232     }
233
234     /**
235      * Returns the parameter value converted to boolean.
236      *
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
240      *
241      * @return bool The filtered value
242      */
243     public function getBoolean($key, $default = false, $deep = false)
244     {
245         return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
246     }
247
248     /**
249      * Filter key.
250      *
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
256      *
257      * @see http://php.net/manual/en/function.filter-var.php
258      *
259      * @return mixed
260      */
261     public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
262     {
263         static $filters = null;
264
265         if (null === $filters) {
266             foreach (filter_list() as $tmp) {
267                 $filters[filter_id($tmp)] = 1;
268             }
269         }
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);
272             $tmp = $deep;
273             $deep = $filter;
274             $filter = $options;
275             $options = $tmp;
276         }
277
278         $value = $this->get($key, $default, $deep);
279
280         // Always turn $options into an array - this allows filter_var option shortcuts.
281         if (!is_array($options) && $options) {
282             $options = array('flags' => $options);
283         }
284
285         // Add a convenience check for arrays.
286         if (is_array($value) && !isset($options['flags'])) {
287             $options['flags'] = FILTER_REQUIRE_ARRAY;
288         }
289
290         return filter_var($value, $filter, $options);
291     }
292
293     /**
294      * Returns an iterator for parameters.
295      *
296      * @return \ArrayIterator An \ArrayIterator instance
297      */
298     public function getIterator()
299     {
300         return new \ArrayIterator($this->parameters);
301     }
302
303     /**
304      * Returns the number of parameters.
305      *
306      * @return int The number of parameters
307      */
308     public function count()
309     {
310         return count($this->parameters);
311     }
312 }