e3be0a9b8b22aae583e6a6351cca2b313649eef4
[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     protected $parameters;
25
26     /**
27      * @param array $parameters An array of parameters
28      */
29     public function __construct(array $parameters = array())
30     {
31         $this->parameters = $parameters;
32     }
33
34     /**
35      * Returns the parameters.
36      *
37      * @return array An array of parameters
38      */
39     public function all()
40     {
41         return $this->parameters;
42     }
43
44     /**
45      * Returns the parameter keys.
46      *
47      * @return array An array of parameter keys
48      */
49     public function keys()
50     {
51         return array_keys($this->parameters);
52     }
53
54     /**
55      * Replaces the current parameters by a new set.
56      *
57      * @param array $parameters An array of parameters
58      */
59     public function replace(array $parameters = array())
60     {
61         $this->parameters = $parameters;
62     }
63
64     /**
65      * Adds parameters.
66      *
67      * @param array $parameters An array of parameters
68      */
69     public function add(array $parameters = array())
70     {
71         $this->parameters = array_replace($this->parameters, $parameters);
72     }
73
74     /**
75      * Returns a parameter by name.
76      *
77      * @param string $key     The key
78      * @param mixed  $default The default value if the parameter key does not exist
79      *
80      * @return mixed
81      */
82     public function get($key, $default = null)
83     {
84         return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
85     }
86
87     /**
88      * Sets a parameter by name.
89      *
90      * @param string $key   The key
91      * @param mixed  $value The value
92      */
93     public function set($key, $value)
94     {
95         $this->parameters[$key] = $value;
96     }
97
98     /**
99      * Returns true if the parameter is defined.
100      *
101      * @param string $key The key
102      *
103      * @return bool true if the parameter exists, false otherwise
104      */
105     public function has($key)
106     {
107         return array_key_exists($key, $this->parameters);
108     }
109
110     /**
111      * Removes a parameter.
112      *
113      * @param string $key The key
114      */
115     public function remove($key)
116     {
117         unset($this->parameters[$key]);
118     }
119
120     /**
121      * Returns the alphabetic characters of the parameter value.
122      *
123      * @param string $key     The parameter key
124      * @param string $default The default value if the parameter key does not exist
125      *
126      * @return string The filtered value
127      */
128     public function getAlpha($key, $default = '')
129     {
130         return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default));
131     }
132
133     /**
134      * Returns the alphabetic characters and digits of the parameter value.
135      *
136      * @param string $key     The parameter key
137      * @param string $default The default value if the parameter key does not exist
138      *
139      * @return string The filtered value
140      */
141     public function getAlnum($key, $default = '')
142     {
143         return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default));
144     }
145
146     /**
147      * Returns the digits of the parameter value.
148      *
149      * @param string $key     The parameter key
150      * @param string $default The default value if the parameter key does not exist
151      *
152      * @return string The filtered value
153      */
154     public function getDigits($key, $default = '')
155     {
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));
158     }
159
160     /**
161      * Returns the parameter value converted to integer.
162      *
163      * @param string $key     The parameter key
164      * @param int    $default The default value if the parameter key does not exist
165      *
166      * @return int The filtered value
167      */
168     public function getInt($key, $default = 0)
169     {
170         return (int) $this->get($key, $default);
171     }
172
173     /**
174      * Returns the parameter value converted to boolean.
175      *
176      * @param string $key     The parameter key
177      * @param mixed  $default The default value if the parameter key does not exist
178      *
179      * @return bool The filtered value
180      */
181     public function getBoolean($key, $default = false)
182     {
183         return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN);
184     }
185
186     /**
187      * Filter key.
188      *
189      * @param string $key     Key
190      * @param mixed  $default Default = null
191      * @param int    $filter  FILTER_* constant
192      * @param mixed  $options Filter options
193      *
194      * @see http://php.net/manual/en/function.filter-var.php
195      *
196      * @return mixed
197      */
198     public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array())
199     {
200         $value = $this->get($key, $default);
201
202         // Always turn $options into an array - this allows filter_var option shortcuts.
203         if (!\is_array($options) && $options) {
204             $options = array('flags' => $options);
205         }
206
207         // Add a convenience check for arrays.
208         if (\is_array($value) && !isset($options['flags'])) {
209             $options['flags'] = FILTER_REQUIRE_ARRAY;
210         }
211
212         return filter_var($value, $filter, $options);
213     }
214
215     /**
216      * Returns an iterator for parameters.
217      *
218      * @return \ArrayIterator An \ArrayIterator instance
219      */
220     public function getIterator()
221     {
222         return new \ArrayIterator($this->parameters);
223     }
224
225     /**
226      * Returns the number of parameters.
227      *
228      * @return int The number of parameters
229      */
230     public function count()
231     {
232         return \count($this->parameters);
233     }
234 }