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
|
9 |
|
public function __construct(array $config) |
32
|
|
|
{ |
33
|
9 |
|
$this->rawConfig = $config; |
34
|
9 |
|
$this->processConfig('', $config); |
35
|
9 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Processes the config into a one dimensional array. |
39
|
|
|
* |
40
|
|
|
* @param $path |
41
|
|
|
* @param $config |
42
|
|
|
*/ |
43
|
9 |
|
private function processConfig($path, $config) |
44
|
|
|
{ |
45
|
9 |
|
foreach ($config as $key => $value) { |
46
|
8 |
|
$this->pathMap[$path.$key] = $value; |
47
|
|
|
|
48
|
8 |
|
if (is_array($value) && !empty($value)) { |
49
|
8 |
|
$this->processConfig($path.$key.'/', $value); |
50
|
|
|
} |
51
|
|
|
} |
52
|
9 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks whether the key-path does exist or not. |
56
|
|
|
* |
57
|
|
|
* @param string $path |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
7 |
|
public function has($path) |
62
|
|
|
{ |
63
|
7 |
|
return isset($this->pathMap[$path]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Retrieves configuration data for a specific key. |
68
|
|
|
* |
69
|
|
|
* @param string $path |
70
|
|
|
* |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
* |
73
|
|
|
* @return mixed |
74
|
|
|
*/ |
75
|
6 |
|
public function getData($path) |
76
|
|
|
{ |
77
|
6 |
|
if (!$this->has($path)) { |
78
|
1 |
|
throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path)); |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
return $this->pathMap[$path]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Returns the configuration instance for a specific key. Also add the possibility to merge additional information |
86
|
|
|
* into it. |
87
|
|
|
* |
88
|
|
|
* @param string $path |
89
|
|
|
* |
90
|
|
|
* @throws InvalidArgumentException |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
2 |
|
public function getConfig($path) |
95
|
|
|
{ |
96
|
2 |
|
if (!$this->has($path)) { |
97
|
1 |
|
throw new InvalidArgumentException(sprintf('Configuration for path "%s" not found', $path)); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
return new self($this->getData($path)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the stored raw config array. |
105
|
|
|
* |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
1 |
|
public function toArray() |
109
|
|
|
{ |
110
|
1 |
|
return $this->rawConfig; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|