Completed
Push — master ( ca4594...b7998c )
by James Ekow Abaka
01:11
created

Config::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

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 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace ntentan\config;
4
5
class Config
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
            $merge = $this->expand($value);
63 1
            foreach($merge as $mergeKey => $mergeValue) {
64 1
                $this->config[$this->context]["$key.$mergeKey"] = $mergeValue;
65
            }
66
        }
67 3
    }
68
69 3
    private function setValue($keys, $value, $config, $nested = [], &$merge = [])
70
    {
71 3
        if (!empty($keys)) {
72 3
            $key = array_shift($keys);
73 3
            $nested[] = $key;
74 3
            $config[$key] = $this->setValue($keys, $value, isset($config[$key]) ? $config[$key] : [], $nested, $merge);
75 3
            if (count($nested) > 1) {
76 3
                $merge[implode('.', $nested)] = $config[$key];
77 3
            } else if (count($nested) == 1) {
78 3
                $config = array_merge($config, $merge);
79
            }
80 3
            return $config;
81
        } else {
82 3
            return $value;
83
        }
84
    }
85
86 5
    private function expand($array, $prefix = null)
87
    {
88 5
        $config = [];
89 5
        if (!is_array($array)) {
90 5
            return $config;
91
        }
92 5
        $dottedPrefix = $prefix ? "$prefix." : "";
93 5
        foreach ($array as $key => $value) {
94 5
            $newPrefix = $dottedPrefix . $key;
95 5
            $config[$newPrefix] = $value;
96 5
            $config += $this->expand($value, $newPrefix);
97
        }
98 5
        if ($prefix) {
99 4
            $config[$prefix] = $array;
100
        }
101 5
        return $config;
102
    }
103
}
104