{{ message }}
-
-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathOptionsResolver.php
More file actions
375 lines (319 loc) · 10.8 KB
/
Copy pathOptionsResolver.php
File metadata and controls
375 lines (319 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
declare(strict_types=1);
namespace Sentry;
use Psr\Log\LoggerInterface;
use Sentry\Util\Arr;
/**
* A container that declares defaults and allows validation and normalization in a central place.
* When a value fails validation, it will keep the current value (or fall back to the default value if there is no
* current value) and emit debug logs.
*
* Supports a nested config with arbitrary number of layers.
*
* To set validation on nested values, it's possible to use a . (dot) syntax, similar to how JSON can be traversed.
* For example, using 'foo.bar' will refer to
* 'foo' => [
* 'bar' => 'test'
* ]
*
* Dot syntax is generally available to set validators and normalizers, while defaults are specified using
* the real array shape
*
* @internal
*/
class OptionsResolver
{
/**
* Contains all default values and also acts as a kind of schema.
* Only values present in the defaults can be overwritten.
*
* @var array<string, mixed>
*/
private $defaults = [];
/**
* List of all allowed types for a path. Stored using dot syntax.
*
* @var array<string, string[]>
*/
private $allowedTypes = [];
/**
* List of valid values or a validation callback for each option path.
* Stored using dot syntax.
*
* @var array<string, mixed[]|callable|bool|float|int|string|null>
*/
private $allowedValues = [];
/**
* Stores normalizers for each option path. Stored using dot syntax.
*
* @var array<string, callable>
*/
private $normalizers = [];
/**
* @param array<string, mixed> $defaults
*/
public function setDefaults(array $defaults): void
{
$this->defaults = $this->processDefaults($defaults, '');
}
/**
* @param mixed $value
*/
public function setDefault(string $name, $value): void
{
$this->defaults[$name] = $this->processDefaults([$name => $value], '')[$name];
}
/**
* @param mixed $types
*/
public function setAllowedTypes(string $name, $types): void
{
if (\is_string($types)) {
$this->allowedTypes[$name] = [$types];
return;
}
if (!\is_array($types)) {
throw new \InvalidArgumentException('Allowed types must be a string or an array of strings.');
}
$typeSpecs = [];
foreach (array_keys($types) as $key) {
if (!\is_string($types[$key])) {
throw new \InvalidArgumentException('Allowed types must be strings.');
}
$typeSpecs[] = $types[$key];
}
$this->allowedTypes[$name] = $typeSpecs;
}
/**
* @param mixed[]|callable|bool|float|int|string|null $values
*/
public function setAllowedValues(string $path, $values): void
{
$this->allowedValues[$path] = $values;
}
public function setNormalizer(string $path, callable $normalizer): void
{
$this->normalizers[$path] = $normalizer;
}
/**
* Resolves the passed options against the configured defaults.
* If a value does not have a default value, it will be ignored.
* If a value is invalid but has a default, it will fall back to using the default value.
*
* If a value doesn't exist or is invalid, a DEBUG log is generated using the configured logger.
*
* @param array<string, mixed> $options
*
* @return array<string, mixed>
*/
public function resolve(array $options = [], ?LoggerInterface $logger = null): array
{
return array_merge($this->defaults, $this->resolveOnly($options, [], $logger));
}
/**
* Resolves only the options passed as $override and all nested keys that belong to it.
*
* @param array<string, mixed> $override
* @param array<string, mixed> $options
*
* @return array<string, mixed>
*/
public function resolveOnly(
array $override = [],
array $options = [],
?LoggerInterface $logger = null
): array {
return $this->applyOptions(array_intersect_key($options, $override), $this->defaults, $override, '', $logger);
}
/**
* @param array<string, mixed> $defaults
*
* @return array<string, mixed>
*/
private function processDefaults(array $defaults, string $parentPath): array
{
/** @mago-ignore analysis:mixed-assignment */
foreach ($defaults as $option => $value) {
$path = $parentPath === '' ? $option : $parentPath . '.' . $option;
/** @mago-ignore analysis:mixed-assignment */
[$isValid, $processed] = $this->normalizeAndValidate($path, $value);
if (!$isValid) {
$defaults[$option] = null;
} elseif (!Arr::isAssociative($processed)) {
$defaults[$option] = $processed;
} else {
/** @var array<string, mixed> $processed */
$defaults[$option] = $this->processDefaults($processed, $path);
}
}
return $defaults;
}
/**
* @param array<string, mixed> $resolved
* @param array<string, mixed> $defaults
* @param array<string, mixed> $options
*
* @return array<string, mixed>
*/
private function applyOptions(
array $resolved,
array $defaults,
array $options,
string $parentPath,
?LoggerInterface $logger
): array {
/** @mago-ignore analysis:mixed-assignment */
foreach ($options as $option => $value) {
$path = $parentPath === '' ? $option : $parentPath . '.' . $option;
if (!\array_key_exists($option, $defaults)) {
if ($logger !== null) {
$logger->debug(\sprintf('Option "%s" does not exist and will be ignored', $path));
}
continue;
}
/** @mago-ignore analysis:mixed-assignment */
$default = $defaults[$option];
$isBranch = Arr::isAssociative($default);
/** @mago-ignore analysis:mixed-assignment */
[$isValid, $value] = $this->normalizeAndValidate($path, $value);
// If the value is invalid or the value is not in the correct shape, keep the current value if one exists.
// Otherwise, fall back to the default.
// For example, we expected to receive a nested value, but we got a scalar
if (!$isValid || ($isBranch && !\is_array($value))) {
if ($logger !== null) {
$logger->debug(\sprintf('Invalid value for option "%s". The value has been ignored.', $path));
}
if (!\array_key_exists($option, $resolved)) {
$resolved[$option] = $default;
}
continue;
}
if (!$isBranch) {
$resolved[$option] = $value;
continue;
}
$base = $resolved[$option] ?? null;
if (!\is_array($base)) {
$base = $default;
}
/** @var array<string, mixed> $default */
/** @var array<string, mixed> $base */
/** @var array<string, mixed> $value */
$resolved[$option] = $this->applyOptions($base, $default, $value, $path, $logger);
}
return $resolved;
}
/**
* Normalizes and validates a value for a given path.
*
* @param mixed $value
*
* @return array{0: bool, 1: mixed} [isValid, normalizedValue]
*/
private function normalizeAndValidate(string $name, $value): array
{
if (!$this->validateType($name, $value)) {
return [false, $value];
}
if (!$this->validateValue($name, $value)) {
return [false, $value];
}
// If there's no normalizer for this path, or normalization is a no-op, skip re-validation
$normalizer = $this->normalizers[$name] ?? null;
if ($normalizer === null) {
return [true, $value];
}
// Normalize, then validate again only if the value actually changed
/** @mago-ignore analysis:mixed-assignment */
$normalized = $normalizer($value);
if ($normalized === $value) {
return [true, $value];
}
if (!$this->validateType($name, $normalized)) {
return [false, $normalized];
}
if (!$this->validateValue($name, $normalized)) {
return [false, $normalized];
}
return [true, $normalized];
}
/**
* @param mixed $value
*/
private function validateType(string $name, $value): bool
{
$allowedTypes = $this->allowedTypes[$name] ?? null;
if ($allowedTypes === null) {
return true;
}
foreach ($allowedTypes as $typeSpec) {
if ($this->valueMatchesType($value, $typeSpec)) {
return true;
}
}
return false;
}
/**
* Checks whether a value matches a given type specification.
* Supports built-ins, FQCNs/interfaces and typed arrays like "string[]" or Foo\Bar\Baz[].
*
* @param mixed $value
*/
private function valueMatchesType($value, string $typeSpec): bool
{
if (substr($typeSpec, -2) === '[]') {
$elementType = substr($typeSpec, 0, -2);
if (!\is_array($value)) {
return false;
}
foreach (array_keys($value) as $key) {
if (!$this->valueMatchesType($value[$key], $elementType)) {
return false;
}
}
return true;
}
switch ($typeSpec) {
case 'string':
return \is_string($value);
case 'int':
case 'integer':
return \is_int($value);
case 'float':
case 'double':
return \is_float($value);
case 'boolean':
case 'bool':
return \is_bool($value);
case 'array':
return \is_array($value);
case 'object':
return \is_object($value);
case 'callable':
return \is_callable($value);
case 'null':
return $value === null;
}
if (\is_object($value)) {
return $value instanceof $typeSpec;
}
return false;
}
/**
* @param mixed $value
*/
private function validateValue(string $name, $value): bool
{
$allowedValue = $this->allowedValues[$name] ?? null;
if ($allowedValue === null) {
return true;
}
if (\is_callable($allowedValue)) {
return $allowedValue($value) === true;
}
if (!\is_array($allowedValue)) {
return $value === $allowedValue;
}
return \in_array($value, $allowedValue, true);
}
}
You can’t perform that action at this time.
