ApplicationConfig::traverseConfig()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace TomPHP\ContainerConfigurator;
4
5
use ArrayAccess;
6
use InvalidArgumentException;
7
use IteratorAggregate;
8
use TomPHP\ContainerConfigurator\Exception\EntryDoesNotExistException;
9
use TomPHP\ContainerConfigurator\Exception\ReadOnlyException;
10
11
/**
12
 * @internal
13
 */
14
final class ApplicationConfig implements ArrayAccess, IteratorAggregate
15
{
16
    /**
17
     * @var array
18
     */
19
    private $config;
20
21
    /**
22
     * @var string
23
     */
24
    private $separator;
25
26
    /**
27
     * @param array  $config
28
     * @param string $separator
29
     *
30
     * @throws InvalidArgumentException
31
     */
32
    public function __construct(array $config, $separator = '.')
33
    {
34
        \Assert\that($separator)->string()->notEmpty();
0 ignored issues
show
Deprecated Code introduced by
The function Assert\that() has been deprecated with message: In favour of Assert::that($value, $defaultMessage = null, $defaultPropertyPath = null)

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
35
36
        $this->config    = $config;
37
        $this->separator = $separator;
38
    }
39
40
    public function merge(array $config)
41
    {
42
        $this->config = array_replace_recursive($this->config, $config);
43
    }
44
45
    /**
46
     * @param string $separator
47
     *
48
     * @throws InvalidArgumentException
49
     *
50
     * @return void
51
     */
52
    public function setSeparator($separator)
53
    {
54
        \Assert\that($separator)->string()->notEmpty();
0 ignored issues
show
Deprecated Code introduced by
The function Assert\that() has been deprecated with message: In favour of Assert::that($value, $defaultMessage = null, $defaultPropertyPath = null)

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
55
56
        $this->separator = $separator;
57
    }
58
59
    public function getIterator()
60
    {
61
        return new ApplicationConfigIterator($this);
62
    }
63
64
    /**
65
     * @return array<int|string>
66
     */
67
    public function getKeys()
68
    {
69
        return array_keys(iterator_to_array(new ApplicationConfigIterator($this)));
70
    }
71
72
    public function offsetExists($offset)
73
    {
74
        try {
75
            $this->traverseConfig($this->getPath($offset));
76
        } catch (EntryDoesNotExistException $e) {
77
            return false;
78
        }
79
80
        return true;
81
    }
82
83
    /**
84
     * @param mixed $offset
85
     *
86
     * @throws EntryDoesNotExistException
87
     *
88
     * @return mixed
89
     */
90
    public function offsetGet($offset)
91
    {
92
        return $this->traverseConfig($this->getPath($offset));
93
    }
94
95
    /**
96
     * @param mixed $offset
97
     * @param mixed $value
98
     *
99
     * @throws ReadOnlyException
100
     */
101
    public function offsetSet($offset, $value)
102
    {
103
        throw ReadOnlyException::fromClassName(__CLASS__);
104
    }
105
106
    /**
107
     * @param mixed $offset
108
     *
109
     * @throws ReadOnlyException
110
     */
111
    public function offsetUnset($offset)
112
    {
113
        throw ReadOnlyException::fromClassName(__CLASS__);
114
    }
115
116
    /**
117
     * @return array
118
     */
119
    public function asArray()
120
    {
121
        return $this->config;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getSeparator()
128
    {
129
        return $this->separator;
130
    }
131
132
    /**
133
     * @param string $offset
134
     *
135
     * @return array
136
     */
137
    private function getPath($offset)
138
    {
139
        return explode($this->separator, $offset);
140
    }
141
142
    /**
143
     * @param array $path
144
     *
145
     * @throws EntryDoesNotExistException
146
     *
147
     * @return mixed
148
     */
149
    private function traverseConfig(array $path)
150
    {
151
        $pointer = &$this->config;
152
153
        foreach ($path as $node) {
154
            if (!is_array($pointer) || !array_key_exists($node, $pointer)) {
155
                throw EntryDoesNotExistException::fromKey(implode($this->separator, $path));
156
            }
157
158
            $pointer = &$pointer[$node];
159
        }
160
161
        return $pointer;
162
    }
163
}
164