Completed
Push — master ( 03d9da...2dac9a )
by Tom
13s
created

ApplicationConfig::getKeys()   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 0
1
<?php
2
3
namespace TomPHP\ContainerConfigurator;
4
5
use ArrayAccess;
6
use IteratorAggregate;
7
use TomPHP\ContainerConfigurator\Exception\EntryDoesNotExistException;
8
use TomPHP\ContainerConfigurator\Exception\ReadOnlyException;
9
10
final class ApplicationConfig implements ArrayAccess, IteratorAggregate
11
{
12
    /**
13
     * @var array
14
     */
15
    private $config;
16
17
    /**
18
     * @var string
19
     */
20
    private $separator;
21
22
    /**
23
     * @param array  $config
24
     * @param string $separator
25
     */
26
    public function __construct(array $config, $separator = '.')
27
    {
28
        \Assert\that($separator)->string()->notEmpty();
29
30
        $this->config    = $config;
31
        $this->separator = $separator;
32
    }
33
34
    public function merge(array $config)
35
    {
36
        $this->config = array_replace_recursive($this->config, $config);
37
    }
38
39
    /**
40
     * @param string $separator
41
     *
42
     * @return void
43
     */
44
    public function setSeparator($separator)
45
    {
46
        \Assert\that($separator)->string()->notEmpty();
47
48
        $this->separator = $separator;
49
    }
50
51
    public function getIterator()
52
    {
53
        return new ApplicationConfigIterator($this);
54
    }
55
56
    /**
57
     * @return array<int|string>
58
     */
59
    public function getKeys()
60
    {
61
        return array_keys(iterator_to_array(new ApplicationConfigIterator($this)));
62
    }
63
64
    public function offsetExists($offset)
65
    {
66
        try {
67
            $this->traverseConfig($this->getPath($offset));
68
        } catch (EntryDoesNotExistException $e) {
69
            return false;
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * @throws EntryDoesNotExistException
77
     */
78
    public function offsetGet($offset)
79
    {
80
        return $this->traverseConfig($this->getPath($offset));
81
    }
82
83
    /**
84
     * @throws ReadOnlyException
85
     */
86
    public function offsetSet($offset, $value)
87
    {
88
        throw ReadOnlyException::fromClassName(__CLASS__);
89
    }
90
91
    /**
92
     * @throws ReadOnlyException
93
     */
94
    public function offsetUnset($offset)
95
    {
96
        throw ReadOnlyException::fromClassName(__CLASS__);
97
    }
98
99
    /**
100
     * @return array
101
     */
102
    public function asArray()
103
    {
104
        return $this->config;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getSeparator()
111
    {
112
        return $this->separator;
113
    }
114
115
    private function getPath($offset)
116
    {
117
        return explode($this->separator, $offset);
118
    }
119
120
    /**
121
     * @return mixed
122
     *
123
     * @throws EntryDoesNotExistException
124
     */
125
    private function traverseConfig(array $path)
126
    {
127
        $pointer = &$this->config;
128
129
        foreach ($path as $node) {
130
            if (!is_array($pointer) || !array_key_exists($node, $pointer)) {
131
                throw EntryDoesNotExistException::fromKey(implode($this->separator, $path));
132
            }
133
134
            $pointer = &$pointer[$node];
135
        }
136
137
        return $pointer;
138
    }
139
}
140