Completed
Push — 1.6 ( bb055d )
by Colin
01:28
created

Configuration::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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
    /** @var array<string, mixed> */
20
    private $config;
21
22
    /**
23
     * @param array<string, mixed> $config
24
     */
25 3231
    public function __construct(array $config = [])
26
    {
27 3231
        $this->config = $config;
28 3231
    }
29
30 2937
    public function merge(array $config = [])
31
    {
32 2937
        $this->config = \array_replace_recursive($this->config, $config);
33 2937
    }
34
35 24
    public function replace(array $config = [])
36
    {
37 24
        $this->config = $config;
38 24
    }
39
40 3078
    public function get(?string $key = null, $default = null)
41
    {
42 3078
        if ($key === null) {
43 18
            @\trigger_error('Calling Configuration::get() without a $key is deprecated in league/commonmark 1.6 and will not be allowed in 2.0', \E_USER_DEPRECATED);
44
45 18
            return $this->config;
46
        }
47
48
        // accept a/b/c as ['a']['b']['c']
49 3078
        if (\strpos($key, '/')) {
50 2991
            return $this->getConfigByPath($key, $default);
51
        }
52
53 3021
        if (!isset($this->config[$key])) {
54 2952
            return $default;
55
        }
56
57 2988
        return $this->config[$key];
58
    }
59
60 21
    public function set(string $key, $value = null)
61
    {
62 21
        if (\func_num_args() === 1) {
63
            @\trigger_error('Calling Configuration::set() without a $value is deprecated in league/commonmark 1.6 and will not be allowed in 2.0', \E_USER_DEPRECATED);
64
        }
65
66
        // accept a/b/c as ['a']['b']['c']
67 21
        if (\strpos($key, '/')) {
68 12
            $this->setByPath($key, $value);
69
        }
70
71 21
        $this->config[$key] = $value;
72 21
    }
73
74 3
    public function exists(string $key): bool
75
    {
76 3
        return $this->getConfigByPath($key, self::MISSING) !== self::MISSING;
0 ignored issues
show
Deprecated Code introduced by
The constant League\CommonMark\Util\C...ationInterface::MISSING has been deprecated.

This class constant has been deprecated.

Loading history...
77
    }
78
79
    /**
80
     * @param string      $keyPath
81
     * @param string|null $default
82
     *
83
     * @return mixed|null
84
     */
85 2994
    private function getConfigByPath(string $keyPath, $default = null)
86
    {
87 2994
        $keyArr = \explode('/', $keyPath);
88 2994
        $data = $this->config;
89 2994
        foreach ($keyArr as $k) {
90 2994
            if (!\is_array($data) || !isset($data[$k])) {
91 2976
                return $default;
92
            }
93
94 2928
            $data = $data[$k];
95
        }
96
97 2928
        return $data;
98
    }
99
100
    /**
101
     * @param string      $keyPath
102
     * @param string|null $value
103
     */
104 12
    private function setByPath(string $keyPath, $value = null): void
105
    {
106 12
        $keyArr = \explode('/', $keyPath);
107 12
        $pointer = &$this->config;
108 12
        while (($k = \array_shift($keyArr)) !== null) {
109 12
            if (!\is_array($pointer)) {
110 12
                $pointer = [];
111
            }
112
113 12
            if (!isset($pointer[$k])) {
114 12
                $pointer[$k] = null;
115
            }
116
117 12
            $pointer = &$pointer[$k];
118
        }
119
120 12
        $pointer = $value;
121 12
    }
122
}
123