Passed
Push — 4.9 ( 8f0f3a...f6adb4 )
by Mikhail
01:57
created

Config::getSystemKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
class Config
4
{
5
    private $params = [];
6
    private $systemKeys = [];
7
8
    function __construct(array $arguments, array $defaults = [])
9
    {
10
        $this->set('path', ROOT_DIR);
11
12
        $self = $this;
13
        array_walk($defaults, function($setting, $key) use ($self) {
14
            $self->set(
15
                $key,
16
                $setting
17
            );
18
            $self->systemKeys[] = $key;
19
        });
20
        
21
        foreach($arguments as $key => $value) {
22
            $this->set($key, $value);
23
        }
24
    }
25
26
    public function set(string $key, $param): void
27
    {
28
        $this->params[$key] = $param;
29
    }
30
31
    public function get(string $key)
32
    {
33
        $result = @$this->params[$key] ?: null;
34
        $config = $this;
35
36
        return !is_string($result) ? $result : preg_replace_callback('/(\$\{([\w\.-_]+)\})/', function($matches) use ($config) {
37
            return $config->get($matches[2]);
38
        }, $result);
39
    }
40
41
    public function getOr(string ...$keys)
42
    {
43
        foreach($keys as $key) {
44
            $value = $this->get($key);
45
46
            if ($value) {
47
                return $value;
48
            }
49
        }
50
51
        return null;
52
    }
53
54
    public function getAll(): array
55
    {
56
        return $this->params;
57
    }
58
59
    public function getSystemKeys(): array
60
    {
61
        return $this->systemKeys;
62
    }
63
}
64