1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Config; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use Wandu\Config\Contracts\Config as ConfigContract; |
6
|
|
|
use Wandu\Config\Contracts\Loader; |
7
|
|
|
use Wandu\Config\Exception\NotAllowedMethodException; |
8
|
|
|
|
9
|
|
|
class Config implements ConfigContract |
10
|
|
|
{ |
11
|
|
|
/** @var array */ |
12
|
|
|
protected $items; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param array $items |
16
|
|
|
*/ |
17
|
18 |
|
public function __construct(array $items = []) |
18
|
|
|
{ |
19
|
18 |
|
$this->items = $items; |
20
|
18 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Wandu\Config\Contracts\Loader $loader |
24
|
|
|
*/ |
25
|
1 |
|
public function append(Loader $loader) |
26
|
|
|
{ |
27
|
1 |
|
$this->merge($loader->load()); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $appender |
32
|
|
|
*/ |
33
|
3 |
|
public function merge(array $appender) |
34
|
|
|
{ |
35
|
3 |
|
$this->items = $this->recursiveMerge($this->items, $appender); |
|
|
|
|
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param mixed $origin |
40
|
|
|
* @param mixed $appender |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
3 |
|
private function recursiveMerge($origin, $appender) |
44
|
|
|
{ |
45
|
3 |
|
if (is_array($origin) |
46
|
3 |
|
&& array_values($origin) !== $origin |
47
|
3 |
|
&& is_array($appender) |
48
|
3 |
|
&& array_values($appender) !== $appender) { |
49
|
3 |
|
foreach ($appender as $key => $value) { |
50
|
3 |
|
if (isset($origin[$key])) { |
51
|
2 |
|
$origin[$key] = $this->recursiveMerge($origin[$key], $value); |
52
|
|
|
} else { |
53
|
3 |
|
$origin[$key] = $value; |
54
|
|
|
} |
55
|
|
|
} |
56
|
3 |
|
return $origin; |
57
|
|
|
} |
58
|
3 |
|
return $appender; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
3 |
|
public function toArray(): array |
65
|
|
|
{ |
66
|
3 |
|
return $this->items ?? []; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function has($name): bool |
73
|
|
|
{ |
74
|
1 |
|
if ($name === '') { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
1 |
|
$names = explode('.', $name); |
78
|
1 |
|
$dataToReturn = $this->items; |
79
|
1 |
|
while (count($names)) { |
80
|
1 |
|
$name = array_shift($names); |
81
|
1 |
|
if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) { |
82
|
1 |
|
return false; |
83
|
|
|
} |
84
|
1 |
|
$dataToReturn = $dataToReturn[$name]; |
85
|
|
|
} |
86
|
1 |
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
9 |
|
public function get($name, $default = null) |
93
|
|
|
{ |
94
|
9 |
|
if ($name === '') { |
95
|
1 |
|
return $this->items; |
96
|
|
|
} |
97
|
9 |
|
$names = explode('.', $name); |
98
|
9 |
|
$dataToReturn = $this->items; |
99
|
9 |
|
while (count($names)) { |
100
|
9 |
|
$name = array_shift($names); |
101
|
9 |
|
if (!is_array($dataToReturn) || !array_key_exists($name, $dataToReturn)) { |
102
|
6 |
|
return $default; |
103
|
|
|
} |
104
|
7 |
|
$dataToReturn = $dataToReturn[$name]; |
105
|
|
|
} |
106
|
7 |
|
return $dataToReturn; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function subset($name): ConfigContract |
113
|
|
|
{ |
114
|
1 |
|
$subset = $this->get($name); |
115
|
1 |
|
if (!is_array($subset)) { |
116
|
1 |
|
throw new InvalidArgumentException('subset must be an array.'); |
117
|
|
|
} |
118
|
1 |
|
return new static($subset); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function offsetExists($offset) |
125
|
|
|
{ |
126
|
|
|
return $this->has($offset); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
1 |
|
public function offsetGet($offset) |
133
|
|
|
{ |
134
|
1 |
|
if (strpos($offset, '||') !== false) { |
135
|
1 |
|
list($offset, $default) = explode('||', $offset); |
136
|
1 |
|
return $this->get($offset, $default); |
137
|
|
|
} |
138
|
1 |
|
return $this->get($offset); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function offsetSet($offset, $value) |
145
|
|
|
{ |
146
|
1 |
|
throw new NotAllowedMethodException(__FUNCTION__, __CLASS__); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
1 |
|
public function offsetUnset($offset) |
153
|
|
|
{ |
154
|
1 |
|
throw new NotAllowedMethodException(__FUNCTION__, __CLASS__); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..