1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Pomm's Foundation package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Foundation; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\Exception\FoundationException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ParameterHolder |
16
|
|
|
* |
17
|
|
|
* @package Foundation |
18
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
19
|
|
|
* @author Grégoire HUBERT <[email protected]> |
20
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
21
|
|
|
*/ |
22
|
|
|
class ParameterHolder implements \ArrayAccess, \IteratorAggregate, \Countable |
23
|
|
|
{ |
24
|
|
|
protected $parameters; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* __construct() |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* @param array $parameters (optional) |
31
|
|
|
*/ |
32
|
|
|
public function __construct(array $parameters = []) |
33
|
|
|
{ |
34
|
|
|
$this->parameters = $parameters; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* setParameter |
39
|
|
|
* |
40
|
|
|
* Set a parameter. |
41
|
|
|
* |
42
|
|
|
* @access public |
43
|
|
|
* @param string $name |
44
|
|
|
* @param string|array $value |
45
|
|
|
* @return ParameterHolder $this |
46
|
|
|
*/ |
47
|
|
|
public function setParameter($name, $value) |
48
|
|
|
{ |
49
|
|
|
$this->parameters[$name] = $value; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* hasParameter |
56
|
|
|
* |
57
|
|
|
* check if the given parameter exists. |
58
|
|
|
* |
59
|
|
|
* @access public |
60
|
|
|
* @param string $name |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function hasParameter($name) |
64
|
|
|
{ |
65
|
|
|
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* getParameter |
70
|
|
|
* |
71
|
|
|
* Returns the parameter "name" or "default" if not set. |
72
|
|
|
* |
73
|
|
|
* @access public |
74
|
|
|
* @param string $name |
75
|
|
|
* @param string $default Optional default value if name not set. |
76
|
|
|
* @return string|array Parameter's value or default. |
77
|
|
|
*/ |
78
|
|
|
public function getParameter($name, $default = null) |
79
|
|
|
{ |
80
|
|
|
return $this->hasParameter($name) ? $this->parameters[$name] : $default; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* mustHave() |
85
|
|
|
* |
86
|
|
|
* Throw an exception if a param is not set |
87
|
|
|
* |
88
|
|
|
* @access public |
89
|
|
|
* @throws FoundationException |
90
|
|
|
* @param string $name the parameter's name |
91
|
|
|
* @return ParameterHolder $this |
92
|
|
|
*/ |
93
|
|
|
public function mustHave($name) |
94
|
|
|
{ |
95
|
|
|
if (!$this->hasParameter($name)) { |
96
|
|
|
throw new FoundationException(sprintf('The parameter "%s" is mandatory.', $name)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* setDefaultValue() |
104
|
|
|
* |
105
|
|
|
* Sets a default value if the param $name is not set |
106
|
|
|
* |
107
|
|
|
* @access public |
108
|
|
|
* @param string $name the parameter's name |
109
|
|
|
* @param mixed $value the default value |
110
|
|
|
* @return ParameterHolder $this |
111
|
|
|
*/ |
112
|
|
|
public function setDefaultValue($name, $value) |
113
|
|
|
{ |
114
|
|
|
if (!$this->hasParameter($name)) { |
115
|
|
|
$this->setParameter($name, $value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* mustBeOneOf() |
123
|
|
|
* |
124
|
|
|
* Check if the given parameter is one of the values passed as argument. If |
125
|
|
|
* not, an exception is thrown. |
126
|
|
|
* |
127
|
|
|
* @access public |
128
|
|
|
* @throws FoundationException |
129
|
|
|
* @param string $name the parameter's name |
130
|
|
|
* @param array $values |
131
|
|
|
* @return ParameterHolder $this |
132
|
|
|
*/ |
133
|
|
|
public function mustBeOneOf($name, array $values) |
134
|
|
|
{ |
135
|
|
|
if (!in_array($this[$name], $values)) { |
136
|
|
|
throw new FoundationException(sprintf('The parameters "%s" must be one of [%s].', $name, implode(', ', $values))); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* unsetParameter() |
144
|
|
|
* |
145
|
|
|
* @access public |
146
|
|
|
* @param string $name |
147
|
|
|
* @return ParameterHolder $this |
148
|
|
|
*/ |
149
|
|
|
public function unsetParameter($name) |
150
|
|
|
{ |
151
|
|
|
unset($this->parameters[$name]); |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* offsetExists() |
158
|
|
|
* |
159
|
|
|
* @see ArrayAccess |
160
|
|
|
*/ |
161
|
|
|
public function offsetExists($name) |
162
|
|
|
{ |
163
|
|
|
return $this->hasParameter($name); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* offsetGet() |
168
|
|
|
* |
169
|
|
|
* @see ArrayAccess |
170
|
|
|
*/ |
171
|
|
|
public function offsetGet($name) |
172
|
|
|
{ |
173
|
|
|
return $this->getParameter($name); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* offsetSet() |
178
|
|
|
* |
179
|
|
|
* @see ArrayAccess |
180
|
|
|
*/ |
181
|
|
|
public function offsetSet($name, $value) |
182
|
|
|
{ |
183
|
|
|
$this->setParameter($name, $value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* offsetUnset() |
188
|
|
|
* |
189
|
|
|
* @see ArrayAccess |
190
|
|
|
*/ |
191
|
|
|
public function offsetUnset($name) |
192
|
|
|
{ |
193
|
|
|
$this->unsetParameter($name); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* |
198
|
|
|
* @see \Countable |
199
|
|
|
*/ |
200
|
|
|
public function count() |
201
|
|
|
{ |
202
|
|
|
return count($this->parameters); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* getIterator() |
207
|
|
|
* |
208
|
|
|
* @see \IteratorAggregate |
209
|
|
|
*/ |
210
|
|
|
public function getIterator() |
211
|
|
|
{ |
212
|
|
|
return new \ArrayIterator($this->parameters); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|