Completed
Push — master ( 595e4b...944328 )
by Changwan
07:08
created

Config::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Config;
3
4
use InvalidArgumentException;
5
use Wandu\Config\Contracts\Config as ConfigContract;
6
use Wandu\Config\Contracts\Loader;
7
use Wandu\Config\Exception\NotAllowedMethodException;
8
9
class Config implements ConfigContract
10
{
11
    /** @var array */
12
    protected $items;
13
14
    /**
15
     * @param array $items
16
     */
17 18
    public function __construct(array $items = [])
18
    {
19 18
        $this->items = $items;
20 18
    }
21 18
22
    /**
23
     * @param \Wandu\Config\Contracts\Loader $loader
24
     */
25
    public function pushLoader(Loader $loader)
26 3
    {
27
        $this->merge($loader->load());
28 3
    }
29 2
30
    /**
31 1
     * @param array $appender
32
     */
33
    public function merge(array $appender)
34
    {
35
        $this->items = $this->recursiveMerge($this->items, $appender);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->recursiveMerge($this->items, $appender) of type * is incompatible with the declared type array of property $items.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37 2
38
    /**
39 2
     * @param mixed $origin
40 1
     * @param mixed $appender
41
     * @return mixed
42 1
     */
43
    private function recursiveMerge($origin, $appender)
44
    {
45
        if (is_array($origin)
46
            && array_values($origin) !== $origin
47
            && is_array($appender)
48
            && array_values($appender) !== $appender) {
49
            foreach ($appender as $key => $value) {
50
                if (isset($origin[$key])) {
51
                    $origin[$key] = $this->recursiveMerge($origin[$key], $value);
52
                } else {
53
                    $origin[$key] = $value;
54
                }
55
            }
56
            return $origin;
57
        }
58
        return $appender;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function toArray(): array
65
    {
66
        return $this->items ?? [];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function has($name): bool
73
    {
74
        if ($name === '') {
75
            return true;
76
        }
77
        $names = explode('.', $name);
78
        $dataToReturn = $this->items;
79
        while (count($names)) {
80
            $name = array_shift($names);
81
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
82
                return false;
83
            }
84
            $dataToReturn = $dataToReturn[$name];
85
        }
86
        return true;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function get($name, $default = null)
93
    {
94
        if ($name === '') {
95
            return $this->items;
96
        }
97
        $names = explode('.', $name);
98
        $dataToReturn = $this->items;
99
        while (count($names)) {
100
            $name = array_shift($names);
101
            if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) {
102
                return $default;
103
            }
104
            $dataToReturn = $dataToReturn[$name];
105
        }
106
        return $dataToReturn;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function subset($name): ConfigContract
113
    {
114
        $subset = $this->get($name);
115
        if (!is_array($subset)) {
116
            throw new InvalidArgumentException('subset must be an array.');
117
        }
118
        return new static($subset);
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function offsetExists($offset)
125
    {
126
        return $this->has($offset);
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function offsetGet($offset)
133
    {
134
        if (strpos($offset, '||') !== false) {
135
            list($offset, $default) = explode('||', $offset);
136
            return $this->get($offset, $default);
137
        }
138
        return $this->get($offset);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function offsetSet($offset, $value)
145
    {
146
        throw new NotAllowedMethodException(__FUNCTION__, __CLASS__);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function offsetUnset($offset)
153
    {
154
        throw new NotAllowedMethodException(__FUNCTION__, __CLASS__);
155
    }
156
}
157