Passed
Push — master ( e37793...fa5b51 )
by Enjoys
15:05 queued 13:22
created

Config::setSeparator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Config;
6
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\NullLogger;
9
10
11
final class Config
12
{
13
14
    public const YAML = Parse\YAML::class;
15
    public const INI = Parse\INI::class;
16
    public const JSON = Parse\Json::class;
17
18
19
    private array $config = [];
20
21
    private string $separator = '->';
22
23
    private LoggerInterface $logger;
24
25 11
    public function __construct(?LoggerInterface $logger = null)
26
    {
27 11
        $this->logger = $logger ?? new NullLogger();
28
    }
29
30
    /**
31
     *
32
     * @param array|string $params
33
     * @param array $options
34
     * @param class-string<ParseInterface> $parseClass
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ParseInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ParseInterface>.
Loading history...
35
     * @param bool $replace
36
     * @return void
37
     * @throws \Exception
38
     */
39 11
    public function addConfig($params, array $options = [], string $parseClass = self::INI, bool $replace = true): void
40
    {
41 11
        $params = (array)$params;
42
43 11
        if (!class_exists($parseClass)) {
44 1
            throw new \Exception(sprintf('Not found parse class: %s', $parseClass));
45
        }
46 10
        $parser = new $parseClass();
47 10
        $parser->setOptions($options);
48 10
        $parser->setLogger($this->logger);
49
50
        /** @var string|string[] $config */
51 10
        foreach ($params as $namespace => $config) {
52 10
            if (is_int($namespace)) {
53 1
                $namespace = null;
54
            }
55
56 10
            if (is_string($namespace)) {
57 9
                $namespace = \trim($namespace);
58
            }
59
60 10
            if (is_array($config)) {
61 2
                foreach ($config as $_config) {
62 2
                    $this->parse($parser, $_config, $namespace, $replace);
63
                }
64 2
                continue;
65
            }
66 8
            $this->parse($parser, $config, $namespace, $replace);
67
        }
68
    }
69
70 10
    private function parse(
71
        ParseInterface $parser,
72
        string $config,
73
        ?string $namespace = null,
74
        bool $replace = true
75
    ): void {
76 10
        $parser->addConfigSource($config);
77
78 10
        $result = $parser->parse();
79
80 10
        if (is_array($result)) {
81 10
            if ($namespace === null) {
82 1
                if ($replace === true) {
83 1
                    $this->config = \array_merge_recursive_distinct($this->config, $result);
84
                } else {
85 1
                    $this->config = \array_merge_recursive_distinct($result, $this->config);
86
                }
87
            } else {
88 9
                if (!array_key_exists($namespace, $this->config)) {
89 9
                    $this->config[$namespace] = [];
90
                }
91 9
                if ($replace === true) {
92 9
                    $this->config[$namespace] = \array_merge_recursive_distinct(
93 9
                        (array)$this->config[$namespace],
94
                        $result
95
                    );
96
                } else {
97 2
                    $this->config[$namespace] = \array_merge_recursive_distinct(
98
                        $result,
99 2
                        (array)$this->config[$namespace]
100
                    );
101
                }
102
            }
103
        }
104
    }
105
106
107
    /**
108
     *
109
     * @param string|null $key
110
     * @param mixed $default
111
     * @return mixed
112
     */
113 10
    public function getConfig(string $key = null, $default = null)
114
    {
115 10
        if ($key === null) {
116 7
            return $this->config;
117
        }
118
119 4
        $parts = explode($this->separator, $key);
120
121
        try {
122 4
            return $this->getValue($parts, $this->config);
123 2
        } catch (Exception $e) {
124 2
            return $default;
125
        }
126
    }
127
128
    /**
129
     * @param string|null $key
130
     * @param mixed $default
131
     * @return mixed
132
     */
133 3
    public function get(string $key = null, $default = null)
134
    {
135 3
        return $this->getConfig($key, $default);
136
    }
137
138
    /**
139
     * @param string[] $parts
140
     * @param mixed $array
141
     * @return mixed
142
     * @throws Exception
143
     */
144 4
    private function getValue(array $parts, $array)
145
    {
146 4
        if (!is_array($array)){
147 1
            throw new Exception();
148
        }
149
150 4
        $key = array_shift($parts);
151
152 4
        if (!array_key_exists($key, $array)) {
153 1
            throw new Exception();
154
        }
155
156 4
        if (count($parts) > 0) {
157 3
            return $this->getValue($parts, $array[$key]);
158
        }
159
160 3
        return $array[$key];
161
162
163
    }
164
165 1
    public function setLogger(LoggerInterface $logger): void
166
    {
167 1
        $this->logger = $logger;
168
    }
169
170
    /**
171
     * @param string $separator
172
     */
173 1
    public function setSeparator(string $separator): void
174
    {
175 1
        $this->separator = $separator;
176
    }
177
}
178