1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @see https://github.com/soluble-io/soluble-mediatools for the canonical repository |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) 2018-2020 Sébastien Vanvelthem. (https://github.com/belgattitude) |
9
|
|
|
* @license https://github.com/soluble-io/soluble-mediatools/blob/master/LICENSE.md MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Soluble\MediaTools\Common\Config; |
13
|
|
|
|
14
|
|
|
use Soluble\MediaTools\Common\Exception\InvalidConfigException; |
15
|
|
|
|
16
|
|
|
final class SafeConfigReader |
17
|
|
|
{ |
18
|
|
|
/** @var null|string */ |
19
|
|
|
private $configKey; |
20
|
|
|
|
21
|
|
|
/** @var array<string, mixed> */ |
22
|
|
|
private $config; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array<string, mixed> $config |
26
|
|
|
*/ |
27
|
65 |
|
public function __construct(array $config, ?string $configKey = null) |
28
|
|
|
{ |
29
|
65 |
|
$this->config = $config; |
30
|
65 |
|
$this->configKey = $configKey; |
31
|
65 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return int|null |
35
|
|
|
* |
36
|
|
|
* @throws InvalidConfigException |
37
|
|
|
*/ |
38
|
49 |
|
public function getNullableInt(string $key, ?int $default = null): ?int |
39
|
|
|
{ |
40
|
49 |
|
$value = $this->getValueOrDefault($key, $default); |
41
|
49 |
|
if (!($value === null) && !is_int($value)) { |
42
|
1 |
|
$this->throwInvalidConfigException(sprintf( |
43
|
1 |
|
'Param \'%s\' must be int, \'%s\' given', |
44
|
|
|
$key, |
45
|
1 |
|
gettype($value) |
46
|
|
|
), $key); |
47
|
|
|
} |
48
|
|
|
|
49
|
48 |
|
return $value; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return float|null |
54
|
|
|
* |
55
|
|
|
* @throws InvalidConfigException |
56
|
|
|
*/ |
57
|
60 |
|
public function getNullableFloat(string $key, ?float $default = null, bool $tolerateInt = true): ?float |
58
|
|
|
{ |
59
|
60 |
|
$value = $this->getValueOrDefault($key, $default); |
60
|
60 |
|
if (!($value === null) && !is_float($value)) { |
61
|
3 |
|
if ($tolerateInt && is_int($value)) { |
62
|
2 |
|
$value = (float) $value; |
63
|
|
|
} else { |
64
|
1 |
|
$this->throwInvalidConfigException(sprintf( |
65
|
1 |
|
'Param \'%s\' must be int, \'%s\' given', |
66
|
|
|
$key, |
67
|
1 |
|
gettype($value) |
68
|
|
|
), $key); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
59 |
|
return $value; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check strict float. |
77
|
|
|
* |
78
|
|
|
* @throws InvalidConfigException |
79
|
|
|
*/ |
80
|
2 |
|
public function getFloat(string $key, ?float $default = null, bool $tolerateInt = true): float |
81
|
|
|
{ |
82
|
2 |
|
$value = $this->getNullableFloat($key, $default, $tolerateInt); |
83
|
1 |
|
$this->ensureNotNull($value, $key); |
84
|
|
|
|
85
|
1 |
|
return (float) $value; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check strict int. |
90
|
|
|
* |
91
|
|
|
* @throws InvalidConfigException |
92
|
|
|
*/ |
93
|
4 |
|
public function getInt(string $key, ?int $default = null): int |
94
|
|
|
{ |
95
|
4 |
|
$value = $this->getNullableInt($key, $default); |
96
|
3 |
|
$this->ensureNotNull($value, $key); |
97
|
|
|
|
98
|
2 |
|
return (int) $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Check strict array. |
103
|
|
|
* |
104
|
|
|
* @throws InvalidConfigException |
105
|
|
|
*/ |
106
|
61 |
|
public function getArray(string $key, ?array $default = null): array |
107
|
|
|
{ |
108
|
61 |
|
$value = $this->getNullableArray($key, $default); |
109
|
60 |
|
$this->ensureNotNull($value, $key); |
110
|
|
|
|
111
|
59 |
|
return (array) $value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array|null |
116
|
|
|
* |
117
|
|
|
* @throws InvalidConfigException |
118
|
|
|
*/ |
119
|
62 |
|
public function getNullableArray(string $key, ?array $default = null): ?array |
120
|
|
|
{ |
121
|
62 |
|
$value = $this->getValueOrDefault($key, $default); |
122
|
62 |
|
if (!($value === null) && !is_array($value)) { |
123
|
1 |
|
$this->throwInvalidConfigException(sprintf( |
124
|
1 |
|
'Param \'%s\' must be array, \'%s\' given', |
125
|
|
|
$key, |
126
|
1 |
|
gettype($value) |
127
|
|
|
), $key); |
128
|
|
|
} |
129
|
|
|
|
130
|
61 |
|
return $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Check strict string. |
135
|
|
|
* |
136
|
|
|
* @throws InvalidConfigException |
137
|
|
|
*/ |
138
|
3 |
|
public function getString(string $key, ?string $default = null): string |
139
|
|
|
{ |
140
|
3 |
|
$value = $this->getNullableString($key, $default); |
141
|
3 |
|
$this->ensureNotNull($value, $key); |
142
|
|
|
|
143
|
2 |
|
return (string) $value; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @throws InvalidConfigException |
148
|
|
|
*/ |
149
|
64 |
|
public function getNullableString(string $key, ?string $default = null): ?string |
150
|
|
|
{ |
151
|
64 |
|
$value = $this->getValueOrDefault($key, $default); |
152
|
|
|
|
153
|
64 |
|
if (!($value === null) && !is_string($value)) { |
154
|
3 |
|
$this->throwInvalidConfigException(sprintf( |
155
|
3 |
|
'Param \'%s\' must be string, \'%s\' given', |
156
|
|
|
$key, |
157
|
3 |
|
gettype($value) |
158
|
|
|
), $key); |
159
|
|
|
} |
160
|
|
|
|
161
|
61 |
|
return $value; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Check strict bool. |
166
|
|
|
* |
167
|
|
|
* @throws InvalidConfigException |
168
|
|
|
*/ |
169
|
4 |
|
public function getBool(string $key, ?bool $default = null): bool |
170
|
|
|
{ |
171
|
4 |
|
$value = $this->getNullableBool($key, $default); |
172
|
3 |
|
$this->ensureNotNull($value, $key); |
173
|
|
|
|
174
|
2 |
|
return (bool) $value; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @throws InvalidConfigException |
179
|
|
|
*/ |
180
|
5 |
|
public function getNullableBool(string $key, ?bool $default = null): ?bool |
181
|
|
|
{ |
182
|
5 |
|
$value = $this->getValueOrDefault($key, $default); |
183
|
5 |
|
if (!($value === null) && !is_bool($value)) { |
184
|
1 |
|
$this->throwInvalidConfigException(sprintf( |
185
|
1 |
|
'Param \'%s\' must be bool, \'%s\' given', |
186
|
|
|
$key, |
187
|
1 |
|
gettype($value) |
188
|
|
|
), $key); |
189
|
|
|
} |
190
|
|
|
|
191
|
4 |
|
return $value; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param mixed|null $default |
196
|
|
|
* |
197
|
|
|
* @return mixed|null |
198
|
|
|
*/ |
199
|
64 |
|
private function getValueOrDefault(string $key, $default) |
200
|
|
|
{ |
201
|
64 |
|
return $this->keyExists($key) ? $this->config[$key] : $default; |
202
|
|
|
} |
203
|
|
|
|
204
|
65 |
|
public function keyExists(string $key): bool |
205
|
|
|
{ |
206
|
65 |
|
return array_key_exists($key, $this->config); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @throws InvalidConfigException |
211
|
|
|
*/ |
212
|
1 |
|
public function ensureKeyExists(string $key): void |
213
|
|
|
{ |
214
|
1 |
|
if ($this->keyExists($key)) { |
215
|
1 |
|
return; |
216
|
|
|
} |
217
|
|
|
|
218
|
1 |
|
$this->throwInvalidConfigException( |
219
|
1 |
|
sprintf( |
220
|
1 |
|
'Required param [\'%s\'] is missing.', |
221
|
|
|
$key |
222
|
|
|
), |
223
|
|
|
$key |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param mixed $value |
229
|
|
|
* |
230
|
|
|
* @throws InvalidConfigException |
231
|
|
|
*/ |
232
|
60 |
|
private function ensureNotNull($value, string $key): void |
233
|
|
|
{ |
234
|
60 |
|
if ($value !== null) { |
235
|
59 |
|
return; |
236
|
|
|
} |
237
|
|
|
|
238
|
1 |
|
$this->throwInvalidConfigException( |
239
|
1 |
|
sprintf( |
240
|
1 |
|
'Param \'%s\' cannot be null.', |
241
|
|
|
$key |
242
|
|
|
), |
243
|
|
|
$key |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
5 |
|
private function throwInvalidConfigException(string $msg, string $key): void |
248
|
|
|
{ |
249
|
5 |
|
throw new InvalidConfigException( |
250
|
5 |
|
sprintf( |
251
|
5 |
|
'%s (check your config entry %s[\'%s\'])', |
252
|
|
|
$msg, |
253
|
5 |
|
$this->configKey === null ? '' : '[' . $this->configKey . ']', |
254
|
|
|
$key |
255
|
|
|
) |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|