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