Config   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 46.88%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 0
dl 0
loc 212
ccs 30
cts 64
cp 0.4688
rs 10
c 0
b 0
f 0

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 4 2
A set() 0 6 1
A __invoke() 0 8 2
C dotGet() 0 22 7
A has() 0 4 1
A export() 0 4 1
A import() 0 6 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 6 2
A __get() 0 4 1
A __set() 0 4 1
A __isset() 0 4 1
A __unset() 0 4 1
A getIterator() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A __set_state() 0 4 1
1
<?php
2
3
namespace Flatdown;
4
5
class Config implements \ArrayAccess, \IteratorAggregate, \Serializable
6
{
7
    /**
8
     * @var array
9
     */
10
    private $data;
11
12
    /**
13
     * @param array $data
14
     */
15 15
    public function __construct(array $data = [])
16
    {
17 15
        $this->import($data);
18 15
    }
19
20
    /**
21
     * @param string $key
22
     * @param mixed $default
23
     * @return array|mixed|null
24
     */
25 15
    public function get($key, $default = null)
26
    {
27 15
        return $this->offsetExists($key) ? $this->offsetGet($key) : $this->dotGet($key, $default);
28
    }
29
30
    /**
31
     * @param string $key
32
     * @param mixed $value
33
     * @return $this
34
     */
35 15
    public function set($key, $value)
36
    {
37 15
        $this->offsetSet($key, $value);
38
39 15
        return $this;
40
    }
41
42
    /**
43
     * @param string $key
44
     * @param mixed $default
45
     * @return array|mixed|$this|null
46
     */
47 15
    public function __invoke($key = null, $default = null)
48
    {
49 15
        if (func_num_args() == 0) {
50 15
            return $this;
51
        }
52
53 15
        return $this->get($key, $default);
54
    }
55
56
    /**
57
     * @param string $key
58
     * @param mixed $default
59
     * @return array|mixed|null
60
     */
61 15
    protected function dotGet($key, $default = null)
62
    {
63 15
        $keys = explode('.', $key);
64
65 15
        if (empty($keys)) {
66
            return $default;
67
        }
68
69 15
        $current = $this->data;
70
71 15
        foreach ($keys as $k) {
72 15
            if (is_array($current) && array_key_exists($k, $current)) {
73
                $current = $current[$k];
74 15
            } elseif (is_object($current) && property_exists($k, $current)) {
75
                $current = $current->$k;
76
            } else {
77 15
                return $default;
78
            }
79
        }
80
81
        return $current;
82
    }
83
84
    /**
85
     * @param string $key
86
     * @return bool
87
     */
88
    public function has($key)
89
    {
90
        return $this->offsetExists($key);
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function export()
97
    {
98
        return $this->data;
99
    }
100
101
    /**
102
     * @param array $data
103
     * @return $this
104
     */
105 15
    public function import(array $data)
106
    {
107 15
        $this->data = $data;
108
109 15
        return $this;
110
    }
111
112
    /**
113
     * @param string $key
114
     * @return bool
115
     */
116 15
    public function offsetExists($key)
117
    {
118 15
        return array_key_exists($key, $this->data);
119
    }
120
121
    /**
122
     * @param string $key
123
     * @return mixed
124
     */
125 15
    public function offsetGet($key)
126
    {
127 15
        return $this->data[$key];
128
    }
129
130
    /**
131
     * @param string $key
132
     * @param mixed $value
133
     */
134 15
    public function offsetSet($key, $value)
135
    {
136 15
        $this->data[$key] = $value;
137 15
    }
138
139
    /**
140
     * @param string $key
141
     */
142
    public function offsetUnset($key)
143
    {
144
        if ($this->offsetExists($key)) {
145
            unset($this->data[$key]);
146
        }
147
    }
148
149
    /**
150
     * @param string $key
151
     * @return mixed
152
     */
153
    public function __get($key)
154
    {
155
        return $this->offsetGet($key);
156
    }
157
158
    /**
159
     * @param string $key
160
     * @param mixed $value
161
     */
162
    public function __set($key, $value)
163
    {
164
        $this->offsetSet($key, $value);
165
    }
166
167
    /**
168
     * @param string $key
169
     * @return bool
170
     */
171
    public function __isset($key)
172
    {
173
        return $this->offsetExists($key);
174
    }
175
176
    /**
177
     * @param string $key
178
     */
179
    public function __unset($key)
180
    {
181
        $this->offsetUnset($key);
182
    }
183
184
    /**
185
     * @return \ArrayIterator
186
     */
187
    public function getIterator()
188
    {
189
        return new \ArrayIterator($this->data);
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function serialize()
196
    {
197
        return serialize($this->data);
198
    }
199
200
    /**
201
     * @param string $serialized
202
     */
203
    public function unserialize($serialized)
204
    {
205
        $this->data = unserialize($serialized);
206
    }
207
208
    /**
209
     * @param array $data
210
     * @return static
211
     */
212
    public static function __set_state($data)
213
    {
214
        return new static($data);
215
    }
216
}
217