Passed
Push — latest ( 7d037c...aae611 )
by Colin
02:38 queued 12s
created

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