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