Completed
Push — master ( 6fdfa0...9b62c3 )
by James Ekow Abaka
01:15
created

Config::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace ntentan\config;
4
5
class Config implements \ArrayAccess
6
{
7
8
    private $config;
9
    private $context = 'default';
10
11
    /**
12
     * Reads the configurations stored at a given path.
13
     * 
14
     * Path could either point to a directory or a specific file. In the case of
15
     * a file, the contents of the file are read into the config object. However,
16
     * in the case of a directory, the contents of all the files in the directory 
17
     * are read into the config file with configurations from each file prefixed with
18
     * the file name.
19
     *
20
     * @param string $path
21
     * @return Config
22
     */
23 4
    public function readPath($path)
24
    {
25 4
        if (is_dir($path)) {
26 3
            $dir = new Directory($path);
27 3
            $this->config = ['default' => $this->expand($dir->parse())];
28 3
            foreach ($dir->getContexts() as $context) {
29 2
                $this->config[$context] = array_merge(
30 3
                        $this->config['default'], $this->expand($dir->parse($context))
31
                );
32
            }
33 1
        } else if (is_file($path)) {
34 1
            $this->config[$this->context] = $this->expand(File::read($path));
35
        } else {
36
            throw new \ntentan\utils\exceptions\FileNotFoundException($path);
37
        }
38 4
        return $this;
39
    }
40
41
    public function isKeySet($key)
42
    {
43
        return isset($this->config[$this->context][$key]);
44
    }
45
46 6
    public function get($key, $default = null)
47
    {
48 6
        return isset($this->config[$this->context][$key]) ? $this->config[$this->context][$key] : $default;
49
    }
50
51 1
    public function setContext($context)
52
    {
53 1
        $this->context = $context;
54 1
    }
55
56 3
    public function set($key, $value)
57
    {
58 3
        $keys = explode('.', $key);
59 3
        $this->config[$this->context] = $this->setValue($keys, $value, $this->config[$this->context]);
60 3
        $this->config[$this->context][$key] = $value;
61 3
        if (is_array($value)) {
62 1
            $this->config[$this->context] += $this->expand($value, $key);
63
        }
64 3
    }
65
66 3
    private function setValue($keys, $value, $config)
67
    {
68 3
        if (!empty($keys)) {
69 3
            $key = array_shift($keys);
70 3
            $config[$key] = $this->setValue($keys, $value, isset($config[$key]) ? $config[$key] : []);
71 3
            return $config;
72
        } else {
73 3
            return $value;
74
        }
75
    }
76
77 5
    private function expand($array, $prefix = null)
78
    {
79 5
        $config = [];
80 5
        if (!is_array($array))
81 5
            return $config;
82 5
        $dottedPrefix = $prefix ? "$prefix." : "";
83 5
        foreach ($array as $key => $value) {
84 5
            $newPrefix = $dottedPrefix . $key;
85 5
            $config[$newPrefix] = $value;
86 5
            $config += $this->expand($value, $newPrefix);
87
        }
88 5
        if ($prefix)
89 4
            $config[$prefix] = $array;
90 5
        return $config;
91
    }
92
93
    public function offsetExists($offset): bool
94
    {
95
        return isset($this->config[$this->context][$offset]);
96
    }
97
98
    public function offsetGet($offset)
99
    {
100
        return $this->config[$this->context][$offset];
101
    }
102
103
    public function offsetSet($offset, $value): void
104
    {
105
        $this->set($offset, $value);
106
    }
107
108
    public function offsetUnset($offset): void
109
    {
110
        // Do nothing for now ...
111
    }
112
113
}
114