Completed
Push — master ( 8efac5...a0c6e2 )
by Gabor
04:36
created

Config::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 2
crap 12
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 5.6
6
 *
7
 * @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
namespace WebHemi\Config;
13
14
use InvalidArgumentException;
15
16
/**
17
 * Class Config.
18
 */
19
class Config implements ConfigInterface
20
{
21
    /** @var array */
22
    private $pathMap = [];
23
    /** @var array */
24
    private $rawConfig;
25
26
    /**
27
     * Config constructor.
28
     *
29
     * @param array $config
30
     */
31
    public function __construct(array $config)
32
    {
33
        $this->rawConfig = $config;
34
        $this->processConfig('', $config);
35
    }
36
37
    /**
38
     * Processes the config into a one dimensional array.
39
     *
40
     * @param $path
41
     * @param $config
42
     */
43
    private function processConfig($path, $config)
44
    {
45
        if (!is_array($config)) {
46
            return;
47
        }
48
49
        foreach ($config as $key => $value) {
50
            $this->pathMap[$path.$key] = $value;
51
52
            if (is_array($value) && !empty($value)) {
53
                $this->processConfig($path.$key.'/', $value);
54
            }
55
        }
56
    }
57
58
    /**
59
     * Checks whether the key-path does exist or not.
60
     *
61
     * @param string $path
62
     *
63
     * @return bool
64
     */
65
    public function has($path)
66
    {
67
        return isset($this->pathMap[$path]);
68
    }
69
70
    /**
71
     * Retrieves configuration data for a specific key.
72
     *
73
     * @param string $path
74
     *
75
     * @throws InvalidArgumentException
76
     *
77
     * @return mixed
78
     */
79
    public function getData($path)
80
    {
81
        if (!$this->has($path)) {
82
            throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path));
83
        }
84
85
        return $this->pathMap[$path];
86
    }
87
88
    /**
89
     * Returns the configuration instance for a specific key. Also add the possibility to merge additional information
90
     * into it.
91
     *
92
     * @param string $path
93
     *
94
     * @throws InvalidArgumentException
95
     *
96
     * @return mixed
97
     */
98
    public function getConfig($path)
99
    {
100
        if (!$this->has($path)) {
101
            throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path));
102
        }
103
104
        return new self($this->getData($path));
105
    }
106
107
    /**
108
     * Returns the stored raw config array.
109
     *
110
     * @return array
111
     */
112
    public function toArray()
113
    {
114
        return $this->rawConfig;
115
    }
116
}
117