Completed
Push — master ( 567b7d...5b8acd )
by James Ekow Abaka
01:48
created

Config::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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