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 = array(); |
||
32 | |||
33 | /** |
||
34 | * Constructor method and sets default options, if any |
||
35 | * |
||
36 | * @param array $data |
||
37 | */ |
||
38 | 3 | public function __construct(array $data) |
|
39 | { |
||
40 | 3 | $this->data = array_merge($this->getDefaults(), $data); |
|
41 | 3 | } |
|
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 | 24 | ||
72 | /** |
||
73 | * {@inheritDoc} |
||
74 | 24 | */ |
|
75 | 24 | public function set($key, $value) |
|
76 | 15 | { |
|
77 | 15 | $segs = explode('.', $key); |
|
78 | $root = &$this->data; |
||
79 | 12 | $cacheKey = ''; |
|
80 | 12 | ||
81 | // Look for the key, creating nested keys if needed |
||
82 | 24 | while ($part = array_shift($segs)) { |
|
83 | if ($cacheKey != '') { |
||
84 | $cacheKey .= '.'; |
||
85 | 24 | } |
|
86 | $cacheKey .= $part; |
||
87 | if (!isset($root[$part]) && count($segs)) { |
||
88 | $root[$part] = array(); |
||
89 | } |
||
90 | $root = &$root[$part]; |
||
91 | 18 | ||
92 | //Unset all old nested cache |
||
93 | 18 | if (isset($this->cache[$cacheKey])) { |
|
94 | 18 | unset($this->cache[$cacheKey]); |
|
95 | 18 | } |
|
96 | |||
97 | //Unset all old nested cache in case of array |
||
98 | 18 | if (count($segs) == 0) { |
|
99 | 18 | foreach ($this->cache as $cacheLocalKey => $cacheValue) { |
|
100 | 12 | if (substr($cacheLocalKey, 0, strlen($cacheKey)) === $cacheKey) { |
|
101 | 12 | unset($this->cache[$cacheLocalKey]); |
|
102 | 18 | } |
|
103 | 18 | } |
|
104 | 3 | } |
|
105 | 3 | } |
|
106 | 18 | ||
107 | // Assign value at target node |
||
108 | $this->cache[$key] = $root = $value; |
||
109 | 18 | } |
|
110 | 9 | ||
111 | 9 | /** |
|
112 | * {@inheritDoc} |
||
113 | */ |
||
114 | 18 | public function has($key) |
|
139 | |||
140 | 3 | /** |
|
141 | * {@inheritDoc} |
||
142 | */ |
||
143 | public function all() |
||
147 | |||
148 | /** |
||
149 | * ArrayAccess Methods |
||
150 | */ |
||
151 | |||
152 | /** |
||
153 | * Gets a value using the offset as a key |
||
154 | * |
||
155 | * @param string $offset |
||
156 | 6 | * |
|
157 | * @return mixed |
||
158 | 6 | */ |
|
159 | public function offsetGet($offset) |
||
163 | |||
164 | /** |
||
165 | * Checks if a key exists |
||
166 | * |
||
167 | * @param string $offset |
||
168 | 6 | * |
|
169 | * @return bool |
||
170 | 6 | */ |
|
171 | public function offsetExists($offset) |
||
175 | |||
176 | /** |
||
177 | * Sets a value using the offset as a key |
||
178 | * |
||
179 | * @param string $offset |
||
180 | * @param mixed $value |
||
181 | 3 | * |
|
182 | * @return void |
||
183 | 3 | */ |
|
184 | 3 | public function offsetSet($offset, $value) |
|
188 | |||
189 | /** |
||
190 | * Deletes a key and its value |
||
191 | * |
||
192 | * @param string $offset |
||
193 | 3 | * |
|
194 | * @return void |
||
195 | 3 | */ |
|
196 | 3 | public function offsetUnset($offset) |
|
200 | |||
201 | /** |
||
202 | * Iterator Methods |
||
203 | */ |
||
204 | |||
205 | /** |
||
206 | * Returns the data array element referenced by its internal cursor |
||
207 | * |
||
208 | * @return mixed The element referenced by the data array's internal cursor. |
||
209 | * If the array is empty or there is no element at the cursor, the |
||
210 | 3 | * function returns false. If the array is undefined, the function |
|
211 | * returns null |
||
212 | 3 | */ |
|
213 | public function current() |
||
217 | |||
218 | /** |
||
219 | * Returns the data array index referenced by its internal cursor |
||
220 | * |
||
221 | * @return mixed The index referenced by the data array's internal cursor. |
||
222 | 3 | * If the array is empty or undefined or there is no element at the |
|
223 | * cursor, the function returns null |
||
224 | 3 | */ |
|
225 | public function key() |
||
229 | |||
230 | /** |
||
231 | * Moves the data array's internal cursor forward one element |
||
232 | * |
||
233 | * @return mixed The element referenced by the data array's internal cursor |
||
234 | * after the move is completed. If there are no more elements in the |
||
235 | 3 | * array after the move, the function returns false. If the data array |
|
236 | * is undefined, the function returns null |
||
237 | 3 | */ |
|
238 | public function next() |
||
242 | |||
243 | /** |
||
244 | * Moves the data array's internal cursor to the first element |
||
245 | * |
||
246 | * @return mixed The element referenced by the data array's internal cursor |
||
247 | * after the move is completed. If the data array is empty, the function |
||
248 | 3 | * returns false. If the data array is undefined, the function returns |
|
249 | * null |
||
250 | 3 | */ |
|
251 | public function rewind() |
||
255 | |||
256 | /** |
||
257 | * Tests whether the iterator's current index is valid |
||
258 | 3 | * |
|
259 | * @return bool True if the current index is valid; false otherwise |
||
260 | 3 | */ |
|
261 | public function valid() |
||
265 | } |
||
266 |