Completed
Pull Request — master (#96)
by Raffael
08:14
created

AbstractConfig::merge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Noodlehaus;
4
5
use ArrayAccess;
6
use Iterator;
7
8
/**
9
 * Abstract Config class
10
 *
11
 * @package    Config
12
 * @author     Jesus A. Domingo <[email protected]>
13
 * @author     Hassan Khan <[email protected]>
14
 * @link       https://github.com/noodlehaus/config
15
 * @license    MIT
16
 */
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()
52
    {
53
        return array();
54
    }
55
56
    /**
57
     * ConfigInterface Methods
58
     */
59
60
    /**
61
     * {@inheritDoc}
62
     */
63 24
    public function get($key, $default = null)
64
    {
65 24
        if ($this->has($key)) {
66 12
            return $this->cache[$key];
67
        }
68
69 12
        return $default;
70
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 18
    public function set($key, $value)
76
    {
77 18
        $segs = explode('.', $key);
78 18
        $root = &$this->data;
79 18
        $cacheKey = '';
80
81
        // Look for the key, creating nested keys if needed
82 18
        while ($part = array_shift($segs)) {
83 18
            if ($cacheKey != '') {
84 12
                $cacheKey .= '.';
85 12
            }
86 18
            $cacheKey .= $part;
87 18
            if (!isset($root[$part]) && count($segs)) {
88 3
                $root[$part] = array();
89 3
            }
90 18
            $root = &$root[$part];
91
92
            //Unset all old nested cache
93 18
            if (isset($this->cache[$cacheKey])) {
94 9
                unset($this->cache[$cacheKey]);
95 9
            }
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 9
                    if (substr($cacheLocalKey, 0, strlen($cacheKey)) === $cacheKey) {
101 6
                        unset($this->cache[$cacheLocalKey]);
102 6
                    }
103 18
                }
104 18
            }
105 18
        }
106
107
        // Assign value at target node
108 18
        $this->cache[$key] = $root = $value;
109 18
    }
110
111
    /**
112
     * {@inheritDoc}
113
     */
114 6
    public function has($key)
115
    {
116
        // Check if already cached
117 6
        if (isset($this->cache[$key])) {
118
            return true;
119
        }
120
121 6
        $segments = explode('.', $key);
122 6
        $root = $this->data;
123
124
        // nested case
125 6
        foreach ($segments as $segment) {
126 6
            if (array_key_exists($segment, $root)) {
127 6
                $root = $root[$segment];
128 6
                continue;
129
            } else {
130 6
                return false;
131
            }
132 6
        }
133
134
        // Set cache for the given key
135 6
        $this->cache[$key] = $root;
136
137 6
        return true;
138
    }
139
140
    /**
141
     * Merge config from another instance
142
     *
143 3
     * @param ConfigInterface $config
144
     * @return ConfigInterface
145 3
     */
146
    public function merge(ConfigInterface $config)
147
    {
148
        $this->data = array_replace_recursive($this->data, $config->all());
149
        return $this;
150
    }
151
152
    /**
153
     * {@inheritDoc}
154
     */
155
    public function all()
156
    {
157
        return $this->data;
158
    }
159 6
160
    /**
161 6
     * ArrayAccess Methods
162
     */
163
164
    /**
165
     * Gets a value using the offset as a key
166
     *
167
     * @param  string $offset
168
     *
169
     * @return mixed
170
     */
171 6
    public function offsetGet($offset)
172
    {
173 6
        return $this->get($offset);
174
    }
175
176
    /**
177
     * Checks if a key exists
178
     *
179
     * @param  string $offset
180
     *
181
     * @return bool
182
     */
183
    public function offsetExists($offset)
184 3
    {
185
        return $this->has($offset);
186 3
    }
187 3
188
    /**
189
     * Sets a value using the offset as a key
190
     *
191
     * @param  string $offset
192
     * @param  mixed  $value
193
     *
194
     * @return void
195
     */
196 3
    public function offsetSet($offset, $value)
197
    {
198 3
        $this->set($offset, $value);
199 3
    }
200
201
    /**
202
     * Deletes a key and its value
203
     *
204
     * @param  string $offset
205
     *
206
     * @return void
207
     */
208
    public function offsetUnset($offset)
209
    {
210
        $this->set($offset, null);
211
    }
212
213 3
    /**
214
     * Iterator Methods
215 3
     */
216
217
    /**
218
     * Returns the data array element referenced by its internal cursor
219
     *
220
     * @return mixed The element referenced by the data array's internal cursor.
221
     *     If the array is empty or there is no element at the cursor, the
222
     *     function returns false. If the array is undefined, the function
223
     *     returns null
224
     */
225 3
    public function current()
226
    {
227 3
        return (is_array($this->data) ? current($this->data) : null);
228
    }
229
230
    /**
231
     * Returns the data array index referenced by its internal cursor
232
     *
233
     * @return mixed The index referenced by the data array's internal cursor.
234
     *     If the array is empty or undefined or there is no element at the
235
     *     cursor, the function returns null
236
     */
237
    public function key()
238 3
    {
239
        return (is_array($this->data) ? key($this->data) : null);
240 3
    }
241
242
    /**
243
     * Moves the data array's internal cursor forward one element
244
     *
245
     * @return mixed The element referenced by the data array's internal cursor
246
     *     after the move is completed. If there are no more elements in the
247
     *     array after the move, the function returns false. If the data array
248
     *     is undefined, the function returns null
249
     */
250
    public function next()
251 3
    {
252
        return (is_array($this->data) ? next($this->data) : null);
253 3
    }
254
255
    /**
256
     * Moves the data array's internal cursor to the first element
257
     *
258
     * @return mixed The element referenced by the data array's internal cursor
259
     *     after the move is completed. If the data array is empty, the function
260
     *     returns false. If the data array is undefined, the function returns
261 3
     *     null
262
     */
263 3
    public function rewind()
264
    {
265
        return (is_array($this->data) ? reset($this->data) : null);
266
    }
267
268
    /**
269
     * Tests whether the iterator's current index is valid
270
     *
271
     * @return bool True if the current index is valid; false otherwise
272
     */
273 3
    public function valid()
274
    {
275 3
        return (is_array($this->data) ? key($this->data) !== null : false);
276 3
    }
277
278
    /**
279
     * Remove a value using the offset as a key
280
     *
281
     * @param  string $key
282
     *
283
     * @return void
284
     */
285
    public function remove($key)
286
    {
287
        $this->offsetUnset($key);
288
    }
289
}
290