Completed
Push — master ( f57a06...06ac50 )
by Hassan
12:53
created

AbstractConfig::has()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 25
ccs 13
cts 13
cp 1
rs 8.5806
cc 4
eloc 13
nc 4
nop 1
crap 4
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
        if ($this->has($key)) {
66 24
            return $this->cache[$key];
67 3
        }
68
69
        return $default;
70 24
    }
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)
115 18
    {
116 9
        // Check if already cached
117 6
        if (isset($this->cache[$key])) {
118 6
            return true;
119 18
        }
120 18
121 18
        $segments = explode('.', $key);
122
        $root = $this->data;
123
124 18
        // nested case
125 18
        foreach ($segments as $segment) {
126
            if (array_key_exists($segment, $root)) {
127
                $root = $root[$segment];
128
                continue;
129
            } else {
130 6
                return false;
131
            }
132 6
        }
133
134
        // Set cache for the given key
135
        $this->cache[$key] = $root;
136
137
        return true;
138 3
    }
139
140 3
    /**
141
     * {@inheritDoc}
142
     */
143
    public function all()
144
    {
145
        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 6
     *
157
     * @return mixed
158 6
     */
159
    public function offsetGet($offset)
160
    {
161
        return $this->get($offset);
162
    }
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)
172
    {
173
        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 3
     *
182
     * @return void
183 3
     */
184 3
    public function offsetSet($offset, $value)
185
    {
186
        $this->set($offset, $value);
187
    }
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)
197
    {
198
        $this->set($offset, null);
199
    }
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()
214
    {
215
        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 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()
226
    {
227
        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 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()
239
    {
240
        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 3
     *     returns false. If the data array is undefined, the function returns
249
     *     null
250 3
     */
251
    public function rewind()
252
    {
253
        return (is_array($this->data) ? reset($this->data) : null);
254
    }
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()
262
    {
263
        return (is_array($this->data) ? key($this->data) !== null : false);
264
    }
265
}
266