Passed
Branch master (7c3f6c)
by Javi
02:38
created

Config::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace itsjavi\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
    public function __construct(array $data = [])
16
    {
17
        $this->import($data);
18
    }
19
20
    /**
21
     * @param string $key
22
     * @param mixed $default
23
     * @return array|mixed|null
24
     */
25
    public function get($key, $default = null)
26
    {
27
        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
    public function set($key, $value)
36
    {
37
        $this->offsetSet($key, $value);
38
39
        return $this;
40
    }
41
42
    /**
43
     * @param string $key
44
     * @param mixed $default
45
     * @return array|mixed|$this|null
46
     */
47 View Code Duplication
    public function __invoke($key = null, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        if (func_num_args() == 0) {
50
            return $this;
51
        }
52
53
        return $this->get($key, $default);
54
    }
55
56
    /**
57
     * @param string $key
58
     * @param mixed $default
59
     * @return array|mixed|null
60
     */
61
    protected function dotGet($key, $default = null)
62
    {
63
        $keys = explode('.', $key);
64
65
        if (!$keys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $keys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
66
            return $default;
67
        }
68
69
        $current = $this->data;
70
71
        foreach ($keys as $k) {
72
            if (is_array($current) && array_key_exists($k, $current)) {
73
                $current = $current[$k];
74
            } elseif (is_object($current) && property_exists($k, $current)) {
75
                $current = $current->$k;
76
            } else {
77
                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
    public function import(array $data)
106
    {
107
        $this->data = $data;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $key
114
     * @return bool
115
     */
116
    public function offsetExists($key)
117
    {
118
        return array_key_exists($key, $this->data);
119
    }
120
121
    /**
122
     * @param string $key
123
     * @return mixed
124
     */
125
    public function offsetGet($key)
126
    {
127
        return $this->data[$key];
128
    }
129
130
    /**
131
     * @param string $key
132
     * @param mixed $value
133
     */
134
    public function offsetSet($key, $value)
135
    {
136
        $this->data[$key] = $value;
137
    }
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