Completed
Push — master ( 5b8acd...bb6be8 )
by James Ekow Abaka
01:44
created

Config::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace ntentan\config;
4
5
class Config 
6
{
7
8
    private $config;
9
    private $context = 'default';
10
    
11
    public function readPath($path, $namespace = null) {
12
        if (is_dir($path)) {
13
            $dir = new Directory($path);
14
            $this->config = ['default' => $this->expand($dir->parse(), $namespace)];
15
            foreach ($dir->getContexts() as $context) {
16
                $this->config[$context] = array_merge(
17
                        $this->config['default'], $this->expand($dir->parse($context), $namespace)
18
                );
19
            }
20
        } else if (is_file($path)) {
21
            $this->config[$this->context] = $this->expand(File::read($path), $namespace);
22
        } else {
23
            throw new \ntentan\utils\exceptions\FileNotFoundException($path);
24
        }
25
    }
26
27
    public function isKeySet($key) {
28
        return isset($this->config[$this->context][$key]);
29
    }
30
31 2
    public function get($key, $default = null) {
32 2
        return isset($this->config[$this->context][$key]) ? $this->config[$this->context][$key] : $default;
33
    }
34
35
    public function setContext($context) {
36
        $this->context = $context;
37
    }
38
39 2
    public function set($key, $value) {
40 2
        $keys = explode('.', $key);
41 2
        $this->config[$this->context] = $this->setValue($keys, $value, $this->config[$this->context]);
42 2
        $this->config[$this->context][$key] = $value;
43 2
        if (is_array($value)) {
44 1
            $this->config[$this->context] += $this->expand($value, null, $key);
45
        }
46 2
    }
47
48 2
    private function setValue($keys, $value, $config) {
49 2
        if (!empty($keys)) {
50 2
            $key = array_shift($keys);
51 2
            $config[$key] = $this->setValue($keys, $value, isset($config[$key]) ? $config[$key] : []);
52 2
            return $config;
53
        } else {
54 2
            return $value;
55
        }
56
    }
57
58 1
    private function expand($array, $namespace, $prefix = null) {
59 1
        $config = [];
60 1
        if (!is_array($array))
61 1
            return $config;
62 1
        $dottedNamespace = $namespace ? "$namespace:" : "";
63 1
        $dottedPrefix = $prefix ? "$prefix." : "";
64 1
        foreach ($array as $key => $value) {
65 1
            $newPrefix = $dottedNamespace . $dottedPrefix . $key;
66 1
            $config[$newPrefix] = $value;
67 1
            $config += $this->expand($value, null, $newPrefix);
68
        }
69 1
        if ($prefix)
70 1
            $config[$prefix] = $array;
71 1
        return $config;
72
    }
73
74
}
75