Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/http-foundation/ParameterBag.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 * @param string $key The key | |
82 * @param mixed $default The default value if the parameter key does not exist | |
83 * | |
84 * @return mixed | |
85 */ | |
86 public function get($key, $default = null) | |
87 { | |
88 return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; | |
89 } | |
90 | |
91 /** | |
92 * Sets a parameter by name. | |
93 * | |
94 * @param string $key The key | |
95 * @param mixed $value The value | |
96 */ | |
97 public function set($key, $value) | |
98 { | |
99 $this->parameters[$key] = $value; | |
100 } | |
101 | |
102 /** | |
103 * Returns true if the parameter is defined. | |
104 * | |
105 * @param string $key The key | |
106 * | |
107 * @return bool true if the parameter exists, false otherwise | |
108 */ | |
109 public function has($key) | |
110 { | |
111 return array_key_exists($key, $this->parameters); | |
112 } | |
113 | |
114 /** | |
115 * Removes a parameter. | |
116 * | |
117 * @param string $key The key | |
118 */ | |
119 public function remove($key) | |
120 { | |
121 unset($this->parameters[$key]); | |
122 } | |
123 | |
124 /** | |
125 * Returns the alphabetic characters of the parameter value. | |
126 * | |
127 * @param string $key The parameter key | |
128 * @param string $default The default value if the parameter key does not exist | |
129 * | |
130 * @return string The filtered value | |
131 */ | |
132 public function getAlpha($key, $default = '') | |
133 { | |
134 return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default)); | |
135 } | |
136 | |
137 /** | |
138 * Returns the alphabetic characters and digits of the parameter value. | |
139 * | |
140 * @param string $key The parameter key | |
141 * @param string $default The default value if the parameter key does not exist | |
142 * | |
143 * @return string The filtered value | |
144 */ | |
145 public function getAlnum($key, $default = '') | |
146 { | |
147 return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default)); | |
148 } | |
149 | |
150 /** | |
151 * Returns the digits of the parameter value. | |
152 * | |
153 * @param string $key The parameter key | |
154 * @param string $default The default value if the parameter key does not exist | |
155 * | |
156 * @return string The filtered value | |
157 */ | |
158 public function getDigits($key, $default = '') | |
159 { | |
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)); | |
162 } | |
163 | |
164 /** | |
165 * Returns the parameter value converted to integer. | |
166 * | |
167 * @param string $key The parameter key | |
168 * @param int $default The default value if the parameter key does not exist | |
169 * | |
170 * @return int The filtered value | |
171 */ | |
172 public function getInt($key, $default = 0) | |
173 { | |
174 return (int) $this->get($key, $default); | |
175 } | |
176 | |
177 /** | |
178 * Returns the parameter value converted to boolean. | |
179 * | |
180 * @param string $key The parameter key | |
181 * @param mixed $default The default value if the parameter key does not exist | |
182 * | |
183 * @return bool The filtered value | |
184 */ | |
185 public function getBoolean($key, $default = false) | |
186 { | |
187 return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN); | |
188 } | |
189 | |
190 /** | |
191 * Filter key. | |
192 * | |
193 * @param string $key Key | |
194 * @param mixed $default Default = null | |
195 * @param int $filter FILTER_* constant | |
196 * @param mixed $options Filter options | |
197 * | |
198 * @see http://php.net/manual/en/function.filter-var.php | |
199 * | |
200 * @return mixed | |
201 */ | |
202 public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array()) | |
203 { | |
204 $value = $this->get($key, $default); | |
205 | |
206 // Always turn $options into an array - this allows filter_var option shortcuts. | |
207 if (!is_array($options) && $options) { | |
208 $options = array('flags' => $options); | |
209 } | |
210 | |
211 // Add a convenience check for arrays. | |
212 if (is_array($value) && !isset($options['flags'])) { | |
213 $options['flags'] = FILTER_REQUIRE_ARRAY; | |
214 } | |
215 | |
216 return filter_var($value, $filter, $options); | |
217 } | |
218 | |
219 /** | |
220 * Returns an iterator for parameters. | |
221 * | |
222 * @return \ArrayIterator An \ArrayIterator instance | |
223 */ | |
224 public function getIterator() | |
225 { | |
226 return new \ArrayIterator($this->parameters); | |
227 } | |
228 | |
229 /** | |
230 * Returns the number of parameters. | |
231 * | |
232 * @return int The number of parameters | |
233 */ | |
234 public function count() | |
235 { | |
236 return count($this->parameters); | |
237 } | |
238 } |