/
sfParameterHolder.class.php
217 lines (194 loc) · 5.28 KB
/
sfParameterHolder.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr <sean@code-box.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfParameterHolder provides a base class for managing parameters.
*
* Parameters, in this case, are used to extend classes with additional data
* that requires no additional logic to manage.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Sean Kerr <sean@code-box.org>
*/
class sfParameterHolder implements Serializable
{
protected $parameters = [];
/**
* The constructor for sfParameterHolder.
*/
public function __construct()
{
}
/**
* Serializes the current instance for PHP 7.4+.
*
* @return array
*/
public function __serialize()
{
return $this->parameters;
}
/**
* Unserializes a sfParameterHolder instance for PHP 7.4+.
* [CVE-2024-28861] Check type of returned data to avoid deserialization vulnerabilities.
*
* @param array $data
*/
public function __unserialize($data)
{
if (!is_array($data)) {
$this->parameters = [];
return;
}
$this->parameters = $data;
}
/**
* Clears all parameters associated with this request.
*/
public function clear()
{
$this->parameters = [];
}
/**
* Retrieves a parameter.
*
* @param string $name A parameter name
* @param mixed $default A default parameter value
*
* @return mixed A parameter value, if the parameter exists, otherwise null
*/
public function &get($name, $default = null)
{
if (array_key_exists($name, $this->parameters)) {
$value = &$this->parameters[$name];
} else {
$value = $default;
}
return $value;
}
/**
* Retrieves an array of parameter names.
*
* @return array An indexed array of parameter names
*/
public function getNames()
{
return array_keys($this->parameters);
}
/**
* Retrieves an array of parameters.
*
* @return array An associative array of parameters
*/
public function &getAll()
{
return $this->parameters;
}
/**
* Indicates whether or not a parameter exists.
*
* @param string $name A parameter name
*
* @return bool true, if the parameter exists, otherwise false
*/
public function has($name)
{
return array_key_exists($name, $this->parameters);
}
/**
* Remove a parameter.
*
* @param string $name A parameter name
* @param mixed $default A default parameter value
*
* @return string A parameter value, if the parameter was removed, otherwise null
*/
public function remove($name, $default = null)
{
$retval = $default;
if (array_key_exists($name, $this->parameters)) {
$retval = $this->parameters[$name];
unset($this->parameters[$name]);
}
return $retval;
}
/**
* Sets a parameter.
*
* If a parameter with the name already exists the value will be overridden.
*
* @param string $name A parameter name
* @param mixed $value A parameter value
*/
public function set($name, $value)
{
$this->parameters[$name] = $value;
}
/**
* Sets a parameter by reference.
*
* If a parameter with the name already exists the value will be overridden.
*
* @param string $name A parameter name
* @param mixed $value A reference to a parameter value
*/
public function setByRef($name, &$value)
{
$this->parameters[$name] = &$value;
}
/**
* Sets an array of parameters.
*
* If an existing parameter name matches any of the keys in the supplied
* array, the associated value will be overridden.
*
* @param array $parameters An associative array of parameters and their associated values
*/
public function add($parameters)
{
if (null === $parameters) {
return;
}
foreach ($parameters as $key => $value) {
$this->parameters[$key] = $value;
}
}
/**
* Sets an array of parameters by reference.
*
* If an existing parameter name matches any of the keys in the supplied
* array, the associated value will be overridden.
*
* @param array $parameters An associative array of parameters and references to their associated values
*/
public function addByRef(&$parameters)
{
foreach ($parameters as $key => &$value) {
$this->parameters[$key] = &$value;
}
}
/**
* Serializes the current instance.
*
* @return string Objects instance
*/
public function serialize()
{
return serialize($this->__serialize());
}
/**
* Unserializes a sfParameterHolder instance.
*
* @param string $serialized A serialized sfParameterHolder instance
*/
public function unserialize($serialized)
{
$this->__unserialize(unserialize($serialized));
}
}