Completed
Pull Request — master (#37)
by Tom
03:14
created

ApplicationConfig::getSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use ArrayAccess;
6
use IteratorAggregate;
7
use TomPHP\ConfigServiceProvider\Exception\EntryDoesNotExistException;
8
use TomPHP\ConfigServiceProvider\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
        $this->config    = $config;
29
        $this->separator = $separator;
30
    }
31
32
    public function merge(array $config)
33
    {
34
        $this->config = array_replace_recursive($this->config, $config);
35
    }
36
37
    /**
38
     * @param string $separator
39
     *
40
     * @return void
41
     */
42
    public function setSeparator($separator)
43
    {
44
        $this->separator = $separator;
45
    }
46
47
    public function getIterator()
48
    {
49
        return new ApplicationConfigIterator($this);
50
    }
51
52
    /**
53
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<integer|string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
54
     */
55
    public function getKeys()
56
    {
57
        return array_keys(iterator_to_array(new ApplicationConfigIterator($this)));
58
    }
59
60
    public function offsetExists($offset)
61
    {
62
        try {
63
            $this->traverseConfig($this->getPath($offset));
64
        } catch (EntryDoesNotExistException $e) {
65
            return false;
66
        }
67
68
        return true;
69
    }
70
71
    public function offsetGet($offset)
72
    {
73
        return $this->traverseConfig($this->getPath($offset));
74
    }
75
76
    public function offsetSet($offset, $value)
77
    {
78
        throw ReadOnlyException::fromClassName(__CLASS__);
79
    }
80
81
    public function offsetUnset($offset)
82
    {
83
        throw ReadOnlyException::fromClassName(__CLASS__);
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function asArray()
90
    {
91
        return $this->config;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getSeparator()
98
    {
99
        return $this->separator;
100
    }
101
102
    private function getPath($offset)
103
    {
104
        return explode($this->separator, $offset);
105
    }
106
107
    private function traverseConfig(array $path)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
108
    {
109
        $pointer = &$this->config;
110
111
        foreach ($path as $node) {
112
            if (!is_array($pointer) || !array_key_exists($node, $pointer)) {
113
                throw EntryDoesNotExistException::fromKey(implode($this->separator, $path));
114
            }
115
116
            $pointer = &$pointer[$node];
117
        }
118
119
        return $pointer;
120
    }
121
}
122