Completed
Pull Request — master (#81)
by
unknown
05:50
created

AbstractConfig::next()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
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 8
            }
86 18
            $cacheKey .= $part;
87 18
            if (!isset($root[$part]) && count($segs)) {
88 3
                $root[$part] = array();
89 2
            }
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 6
            }
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 7
                        unset($this->cache[$cacheLocalKey]);
102 4
                    }
103 12
                }
104 12
            }
105 12
        }
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 4
        }
133
134
        // Set cache for the given key
135 6
        $this->cache[$key] = $root;
136
137 6
        return true;
138
    }
139
140
    /**
141
     * {@inheritDoc}
142
     */
143 3
    public function all()
144
    {
145 3
        return $this->data;
146
    }
147
148
    /**
149
     * ArrayAccess Methods
150
     */
151
152
    /**
153
     * Gets a value using the offset as a key
154
     *
155
     * @param  string $offset
156
     *
157
     * @return mixed
158
     */
159 6
    public function offsetGet($offset)
160
    {
161 6
        return $this->get($offset);
162
    }
163
164
    /**
165
     * Checks if a key exists
166
     *
167
     * @param  string $offset
168
     *
169
     * @return bool
170
     */
171 6
    public function offsetExists($offset)
172
    {
173 6
        return $this->has($offset);
174
    }
175
176
    /**
177
     * Sets a value using the offset as a key
178
     *
179
     * @param  string $offset
180
     * @param  mixed  $value
181
     *
182
     * @return void
183
     */
184 3
    public function offsetSet($offset, $value)
185
    {
186 3
        $this->set($offset, $value);
187 3
    }
188
189
    /**
190
     * Deletes a key and its value
191
     *
192
     * @param  string $offset
193
     *
194
     * @return void
195
     */
196 3
    public function offsetUnset($offset)
197
    {
198 3
        $this->set($offset, null);
199 3
    }
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
     *     function returns false. If the array is undefined, the function
211
     *     returns null
212
     */
213 3
    public function current()
214
    {
215 3
        return (is_array($this->data) ? current($this->data) : null);
216
    }
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
     *     If the array is empty or undefined or there is no element at the
223
     *     cursor, the function returns null
224
     */
225 3
    public function key()
226
    {
227 3
        return (is_array($this->data) ? key($this->data) : null);
228
    }
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
     *     array after the move, the function returns false. If the data array
236
     *     is undefined, the function returns null
237
     */
238 3
    public function next()
239
    {
240 3
        return (is_array($this->data) ? next($this->data) : null);
241
    }
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
     *     returns false. If the data array is undefined, the function returns
249
     *     null
250
     */
251 3
    public function rewind()
252
    {
253 3
        return (is_array($this->data) ? reset($this->data) : null);
254
    }
255
256
    /**
257
     * Tests whether the iterator's current index is valid
258
     *
259
     * @return bool True if the current index is valid; false otherwise
260
     */
261 3
    public function valid()
262
    {
263 3
        return (is_array($this->data) ? key($this->data) !== null : false);
264
    }
265
}
266