1 | <?php |
||
17 | abstract class AbstractConfig implements ArrayAccess, ConfigInterface, Iterator |
||
18 | { |
||
19 | /** |
||
20 | * Stores the configuration data |
||
21 | * |
||
22 | * @var array|null |
||
23 | */ |
||
24 | protected $data = null; |
||
25 | |||
26 | /** |
||
27 | * Caches the configuration data |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $cache = []; |
||
32 | |||
33 | /** |
||
34 | * Constructor method and sets default options, if any |
||
35 | * |
||
36 | * @param array $data |
||
37 | */ |
||
38 | 3 | public function __construct(array $data) |
|
42 | |||
43 | /** |
||
44 | * Override this method in your own subclass to provide an array of default |
||
45 | * options and values |
||
46 | * |
||
47 | * @return array |
||
48 | * |
||
49 | * @codeCoverageIgnore |
||
50 | */ |
||
51 | protected function getDefaults() |
||
55 | |||
56 | /** |
||
57 | * ConfigInterface Methods |
||
58 | */ |
||
59 | |||
60 | /** |
||
61 | * {@inheritDoc} |
||
62 | */ |
||
63 | 24 | public function get($key, $default = null) |
|
71 | |||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | */ |
||
75 | 18 | public function set($key, $value) |
|
110 | |||
111 | /** |
||
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 9 | public function remove($key) |
|
115 | { |
||
116 | $segs = explode('.', $key); |
||
117 | 9 | $root = &$this->data; |
|
118 | 3 | ||
119 | foreach(array_slice($segs, 0, -1) as $seg) { |
||
120 | $root = &$root[$seg]; |
||
121 | 9 | } |
|
122 | 9 | ||
123 | unset($root[array_pop($segs)]); |
||
124 | unset($this->cache[$key]); |
||
125 | 9 | } |
|
126 | 9 | ||
127 | 9 | /** |
|
128 | 9 | * {@inheritDoc} |
|
129 | */ |
||
130 | 6 | public function has($key) |
|
155 | 3 | ||
156 | /** |
||
157 | 3 | * Merge config from another instance |
|
158 | * |
||
159 | * @param ConfigInterface $config |
||
160 | * @return ConfigInterface |
||
161 | */ |
||
162 | public function merge(ConfigInterface $config) |
||
167 | |||
168 | /** |
||
169 | * {@inheritDoc} |
||
170 | */ |
||
171 | 6 | public function all() |
|
175 | |||
176 | /** |
||
177 | * ArrayAccess Methods |
||
178 | */ |
||
179 | |||
180 | /** |
||
181 | * Gets a value using the offset as a key |
||
182 | * |
||
183 | 6 | * @param string $offset |
|
184 | * |
||
185 | 6 | * @return mixed |
|
186 | */ |
||
187 | public function offsetGet($offset) |
||
191 | |||
192 | /** |
||
193 | * Checks if a key exists |
||
194 | * |
||
195 | * @param string $offset |
||
196 | 3 | * |
|
197 | * @return bool |
||
198 | 3 | */ |
|
199 | 3 | public function offsetExists($offset) |
|
203 | |||
204 | /** |
||
205 | * Sets a value using the offset as a key |
||
206 | * |
||
207 | * @param string $offset |
||
208 | 3 | * @param mixed $value |
|
209 | * |
||
210 | 3 | * @return void |
|
211 | 3 | */ |
|
212 | public function offsetSet($offset, $value) |
||
216 | |||
217 | /** |
||
218 | * Deletes a key and its value |
||
219 | * |
||
220 | * @param string $offset |
||
221 | * |
||
222 | * @return void |
||
223 | */ |
||
224 | public function offsetUnset($offset) |
||
228 | |||
229 | /** |
||
230 | * Iterator Methods |
||
231 | */ |
||
232 | |||
233 | /** |
||
234 | * Returns the data array element referenced by its internal cursor |
||
235 | * |
||
236 | * @return mixed The element referenced by the data array's internal cursor. |
||
237 | 3 | * If the array is empty or there is no element at the cursor, the |
|
238 | * function returns false. If the array is undefined, the function |
||
239 | 3 | * returns null |
|
240 | */ |
||
241 | public function current() |
||
245 | |||
246 | /** |
||
247 | * Returns the data array index referenced by its internal cursor |
||
248 | * |
||
249 | * @return mixed The index referenced by the data array's internal cursor. |
||
250 | 3 | * If the array is empty or undefined or there is no element at the |
|
251 | * cursor, the function returns null |
||
252 | 3 | */ |
|
253 | public function key() |
||
257 | |||
258 | /** |
||
259 | * Moves the data array's internal cursor forward one element |
||
260 | * |
||
261 | * @return mixed The element referenced by the data array's internal cursor |
||
262 | * after the move is completed. If there are no more elements in the |
||
263 | 3 | * array after the move, the function returns false. If the data array |
|
264 | * is undefined, the function returns null |
||
265 | 3 | */ |
|
266 | public function next() |
||
270 | |||
271 | /** |
||
272 | * Moves the data array's internal cursor to the first element |
||
273 | 3 | * |
|
274 | * @return mixed The element referenced by the data array's internal cursor |
||
275 | 3 | * after the move is completed. If the data array is empty, the function |
|
276 | * returns false. If the data array is undefined, the function returns |
||
277 | * null |
||
278 | */ |
||
279 | public function rewind() |
||
283 | |||
284 | /** |
||
285 | 3 | * Tests whether the iterator's current index is valid |
|
286 | * |
||
287 | 3 | * @return bool True if the current index is valid; false otherwise |
|
288 | 3 | */ |
|
289 | public function valid() |
||
293 | } |
||
294 |