Configuration::replace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Util;
16
17
final class Configuration implements ConfigurationInterface
18
{
19
    private $config;
20
21
    /**
22
     * @param array $config
23
     */
24 2250
    public function __construct(array $config = [])
25
    {
26 2250
        $this->config = $config;
27 2250
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2079
    public function merge(array $config = [])
33
    {
34 2079
        $this->config = \array_replace_recursive($this->config, $config);
35 2079
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    public function replace(array $config = [])
41
    {
42 6
        $this->config = $config;
43 6
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2145
    public function get(?string $key = null, $default = null)
49
    {
50 2145
        if ($key === null) {
51 12
            return $this->config;
52
        }
53
54
        // accept a/b/c as ['a']['b']['c']
55 2145
        if (\strpos($key, '/')) {
56 2070
            return $this->getConfigByPath($key, $default);
57
        }
58
59 2145
        if (!isset($this->config[$key])) {
60 2100
            return $default;
61
        }
62
63 2124
        return $this->config[$key];
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 3
    public function set(string $key, $value = null)
70
    {
71
        // accept a/b/c as ['a']['b']['c']
72 3
        if (\strpos($key, '/')) {
73 3
            $this->setByPath($key, $value);
74
        }
75
76 3
        $this->config[$key] = $value;
77 3
    }
78
79
    /**
80
     * @param string      $keyPath
81
     * @param string|null $default
82
     *
83
     * @return mixed|null
84
     */
85 2070
    private function getConfigByPath(string $keyPath, $default = null)
86
    {
87 2070
        $keyArr = \explode('/', $keyPath);
88 2070
        $data = $this->config;
89 2070
        foreach ($keyArr as $k) {
90 2070
            if (!\is_array($data) || !isset($data[$k])) {
91 6
                return $default;
92
            }
93
94 2070
            $data = $data[$k];
95
        }
96
97 2070
        return $data;
98
    }
99
100
    /**
101
     * @param string      $keyPath
102
     * @param string|null $value
103
     */
104 3
    private function setByPath(string $keyPath, $value = null)
105
    {
106 3
        $keyArr = \explode('/', $keyPath);
107 3
        $pointer = &$this->config;
108 3
        while (($k = array_shift($keyArr)) !== null) {
109 3
            if (!\is_array($pointer)) {
110 3
                $pointer = [];
111
            }
112
113 3
            if (!isset($pointer[$k])) {
114 3
                $pointer[$k] = null;
115
            }
116
117 3
            $pointer = &$pointer[$k];
118
        }
119
120 3
        $pointer = $value;
121 3
    }
122
}
123