ArrayConfig::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Config;
5
6
/**
7
 * Class for config component getting data directly from Array.
8
 *
9
 * @package Config
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class ArrayConfig extends AbstractConfig
14
{
15
    /**
16
     * The array containing configuartion data can directly be passed to the constructor.
17
     * Additional data can be passed with the `setValue()` and `addConfig()` methods.
18
     * @param array<mixed> $aConfig    associative array
19
     */
20
    public function __construct(array $aConfig = [])
21
    {
22
        $this->aConfig = $aConfig;
23
    }
24
25
    /**
26
     * Add additional data to the current config.
27
     * <b>Note: This method uses the php `array_merge()` function! </b><br/>
28
     * If the additional data contains section(s) the current config at least
29
     * contains, the complete section is overwritten! <br/>
30
     * If you want to 'realy' merge two configurations, you have to use separate
31
     * instances and use the mergeWith() method!
32
     * @param array<mixed> $aConfig    associative array
33
     */
34
    public function addConfig(array $aConfig) : void
35
    {
36
        $this->aConfig = ($this->aConfig ? array_merge($this->aConfig, $aConfig) : $aConfig);
37
    }
38
39
    /**
40
     * Add new value to the config.
41
     * Currently only values of highest level are supported.
42
     * @param string $strName   name of the value to set
43
     * @param mixed $value      value to set
44
     */
45
    public function setValue(string $strName, $value) : void
46
    {
47
        $this->aConfig ?: $this->aConfig = [];
48
        $this->aConfig[$strName] = $value;
49
    }
50
}
51