Completed
Push — develop ( 52317d...fc22ae )
by Davide
03:43
created

AbstractConfig   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 13
Bugs 1 Features 5
Metric Value
wmc 18
c 13
b 1
f 5
lcom 1
cbo 0
dl 0
loc 172
ccs 49
cts 49
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDefaults() 0 4 1
B get() 0 24 4
B set() 0 26 6
A has() 0 4 1
A all() 0 4 1
A offsetGet() 0 4 1
A offsetExists() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
3
namespace Noodlehaus;
4
5
use ArrayAccess;
6
7
/**
8
 * Abstract Config class
9
 *
10
 * @package    Config
11
 * @author     Jesus A. Domingo <[email protected]>
12
 * @author     Hassan Khan <[email protected]>
13
 * @link       https://github.com/noodlehaus/config
14
 * @license    MIT
15
 */
16
abstract class AbstractConfig implements ArrayAccess, ConfigInterface
17
{
18
    /**
19
     * Stores the configuration data
20
     *
21
     * @var array|null
22
     */
23
    protected $data = null;
24
25
    /**
26
     * Caches the configuration data
27
     *
28
     * @var array
29
     */
30
    protected $cache = array();
31
32
    /**
33
     * Constructor method and sets default options, if any
34
     *
35
     * @param array $data
36
     */
37 3
    public function __construct(Array $data)
38
    {
39 3
        $this->data = array_merge($this->getDefaults(), $data);
40 3
    }
41
42
    /**
43
     * Override this method in your own subclass to provide an array of default
44
     * options and values
45
     *
46
     * @return array
47
     *
48
     * @codeCoverageIgnore
49
     */
50
    protected function getDefaults()
51
    {
52
        return array();
53
    }
54
55
    /**
56
     * ConfigInterface Methods
57
     */
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 24
    public function get($key, $default = null)
63
    {
64
        // Check if already cached
65 24
        if (isset($this->cache[$key])) {
66 3
            return $this->cache[$key];
67
        }
68
69 24
        $segs = explode('.', $key);
70 24
        $root = $this->data;
71
72
        // nested case
73 24
        foreach ($segs as $part) {
74 24
            if (isset($root[$part])) {
75 15
                $root = $root[$part];
76 15
                continue;
77
            } else {
78 12
                $root = $default;
79 12
                break;
80
            }
81 24
        }
82
83
        // whatever we have is what we needed
84 24
        return ($this->cache[$key] = $root);
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 15
    public function set($key, $value)
91
    {
92 15
        $segs = explode('.', $key);
93 15
        $root = &$this->data;
94 15
        $cacheKey = '';
95
96
        // Look for the key, creating nested keys if needed
97 15
        while ($part = array_shift($segs)) {
98 15
            if($cacheKey != ''){
99 9
                $cacheKey .= '.';
100 9
            }
101 15
            $cacheKey .= $part;
102 15
            if (!isset($root[$part]) && count($segs)) {
103 3
                $root[$part] = array();
104 3
            }
105 15
            $root = &$root[$part];
106
107
            //Unset all old nested cache
108 15
            if(isset($this->cache[$cacheKey])){
109 6
                unset($this->cache[$cacheKey]);
110 6
            }
111 15
        }
112
113
        // Assign value at target node
114 15
        $this->cache[$key] = $root = $value;
115 15
    }
116
117
    /**
118
     * {@inheritDoc}
119
     */
120 6
    public function has($key)
121
    {
122 6
        return !is_null($this->get($key));
123
    }
124
125
    /**
126
     * {@inheritDoc}
127
     */
128 3
    public function all()
129
    {
130 3
        return $this->data;
131
    }
132
133
134
135
    /**
136
     * ArrayAccess Methods
137
     */
138
139
    /**
140
     * Gets a value using the offset as a key
141
     *
142
     * @param  string $offset
143
     *
144
     * @return mixed
145
     */
146 6
    public function offsetGet($offset)
147
    {
148 6
        return $this->get($offset);
149
    }
150
151
    /**
152
     * Checks if a key exists
153
     *
154
     * @param  string $offset
155
     *
156
     * @return bool
157
     */
158 6
    public function offsetExists($offset)
159
    {
160 6
        return $this->has($offset);
161
    }
162
163
    /**
164
     * Sets a value using the offset as a key
165
     *
166
     * @param  string $offset
167
     * @param  mixed  $value
168
     *
169
     * @return void
170
     */
171 3
    public function offsetSet($offset, $value)
172
    {
173 3
        $this->set($offset, $value);
174 3
    }
175
176
    /**
177
     * Deletes a key and its value
178
     *
179
     * @param  string $offset
180
     *
181
     * @return void
182
     */
183 3
    public function offsetUnset($offset)
184
    {
185 3
        $this->set($offset, null);
186 3
    }
187
}
188