Completed
Pull Request — master (#42)
by Tom
02:16
created

ApplicationConfig::offsetSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
        \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
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...
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
    public function offsetGet($offset)
76
    {
77
        return $this->traverseConfig($this->getPath($offset));
78
    }
79
80
    public function offsetSet($offset, $value)
81
    {
82
        throw ReadOnlyException::fromClassName(__CLASS__);
83
    }
84
85
    public function offsetUnset($offset)
86
    {
87
        throw ReadOnlyException::fromClassName(__CLASS__);
88
    }
89
90
    /**
91
     * @return array
92
     */
93
    public function asArray()
94
    {
95
        return $this->config;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getSeparator()
102
    {
103
        return $this->separator;
104
    }
105
106
    private function getPath($offset)
107
    {
108
        return explode($this->separator, $offset);
109
    }
110
111
    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...
112
    {
113
        $pointer = &$this->config;
114
115
        foreach ($path as $node) {
116
            if (!is_array($pointer) || !array_key_exists($node, $pointer)) {
117
                throw EntryDoesNotExistException::fromKey(implode($this->separator, $path));
118
            }
119
120
            $pointer = &$pointer[$node];
121
        }
122
123
        return $pointer;
124
    }
125
}
126