1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tleckie\Config; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Config |
7
|
|
|
* |
8
|
|
|
* @package Tleckie\Config |
9
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class Config implements ConfigInterface |
12
|
|
|
{ |
13
|
|
|
/** @var mixed[] */ |
14
|
|
|
protected array $data; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Config constructor. |
18
|
|
|
* |
19
|
|
|
* @param mixed[] $data |
20
|
|
|
*/ |
21
|
|
|
public function __construct(array $data = []) |
22
|
|
|
{ |
23
|
|
|
$this->data = $this->check($data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $key |
28
|
|
|
* @param mixed|null $default |
29
|
|
|
* @return mixed |
30
|
|
|
*/ |
31
|
|
|
public function get(string $key, mixed $default = null): mixed |
32
|
|
|
{ |
33
|
|
|
return $this->offsetGet($key) ?? $default; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param mixed $offset |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function offsetGet(mixed $offset): mixed |
41
|
|
|
{ |
42
|
|
|
if (!isset($this->data[$offset])) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (is_array($this->data[$offset])) { |
47
|
|
|
return new Config($this->data[$offset]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->data[$offset]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $key |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function has(string $key): bool |
58
|
|
|
{ |
59
|
|
|
return $this->offsetExists($key); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param mixed $offset |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function offsetExists(mixed $offset): bool |
67
|
|
|
{ |
68
|
|
|
return isset($this->data[$offset]); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $key |
73
|
|
|
* @param mixed $value |
74
|
|
|
* @return ConfigInterface |
75
|
|
|
*/ |
76
|
|
|
public function set(string $key, mixed $value): ConfigInterface |
77
|
|
|
{ |
78
|
|
|
$this->offsetSet($key, $value); |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $offset |
85
|
|
|
* @param mixed $value |
86
|
|
|
*/ |
87
|
|
|
public function offsetSet(mixed $offset, mixed $value): void |
88
|
|
|
{ |
89
|
|
|
$this->data[$offset] = $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return mixed[] |
94
|
|
|
*/ |
95
|
|
|
public function jsonSerialize(): array |
96
|
|
|
{ |
97
|
|
|
return $this->toArray(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array[] |
102
|
|
|
*/ |
103
|
|
|
public function toArray(): array |
104
|
|
|
{ |
105
|
|
|
return json_decode( |
106
|
|
|
json_encode($this->data), |
107
|
|
|
true |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $key |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
|
|
public function __get(string $key): mixed |
116
|
|
|
{ |
117
|
|
|
return $this->offsetGet($key); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $key |
122
|
|
|
* @param mixed $value |
123
|
|
|
*/ |
124
|
|
|
public function __set(string $key, mixed $value): void |
125
|
|
|
{ |
126
|
|
|
$this->offsetSet($key, $value); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
public function __isset(string $name): bool |
134
|
|
|
{ |
135
|
|
|
return $this->offsetExists($name); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $name |
140
|
|
|
*/ |
141
|
|
|
public function __unset(string $name): void |
142
|
|
|
{ |
143
|
|
|
$this->offsetUnset($name); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param mixed $offset |
148
|
|
|
*/ |
149
|
|
|
public function offsetUnset(mixed $offset): void |
150
|
|
|
{ |
151
|
|
|
unset($this->data[$offset]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param array|ConfigInterface $config |
156
|
|
|
* @return ConfigInterface |
157
|
|
|
*/ |
158
|
|
|
public function merge(array|ConfigInterface $config): ConfigInterface |
159
|
|
|
{ |
160
|
|
|
$data = $config; |
161
|
|
|
if ($config instanceof ConfigInterface) { |
162
|
|
|
$data = $config->toArray(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$data = $this->check($data); |
166
|
|
|
$this->data = array_merge($this->data, $data); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $data |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
private function check(array $data): array |
176
|
|
|
{ |
177
|
|
|
array_walk($data, static function (&$item) { |
178
|
|
|
$item = is_array($item) ? new Config($item) : $item; |
179
|
|
|
}); |
180
|
|
|
|
181
|
|
|
return $data; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|