|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Curlyspoon\NestedOptionsResolver; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver as SymfonyOptionsResolver; |
|
6
|
|
|
|
|
7
|
|
|
class OptionsResolver extends SymfonyOptionsResolver |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $nested = []; |
|
13
|
|
|
|
|
14
|
|
|
public static function make(array $config = []): OptionsResolver |
|
15
|
|
|
{ |
|
16
|
|
|
$resolver = new static(); |
|
17
|
|
|
|
|
18
|
|
|
$resolver |
|
19
|
|
|
->loadConfigDefaults($config) |
|
20
|
|
|
->loadConfigRequired($config) |
|
21
|
|
|
->loadConfigTypes($config) |
|
22
|
|
|
->loadConfigValues($config) |
|
23
|
|
|
->loadConfigNested($config); |
|
24
|
|
|
|
|
25
|
|
|
return $resolver; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function setNested(string $key, OptionsResolver $resolver): OptionsResolver |
|
29
|
|
|
{ |
|
30
|
|
|
$this->nested[$key] = $resolver; |
|
31
|
|
|
|
|
32
|
|
|
return $this; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function isNested(): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return !empty($this->nested); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function resolve(array $options = []) |
|
41
|
|
|
{ |
|
42
|
|
|
$resolved = parent::resolve($options); |
|
43
|
|
|
|
|
44
|
|
|
foreach ($this->nested as $key => $resolver) { |
|
45
|
|
|
$isLoop = substr($key, -2) == '.*'; |
|
46
|
|
|
if ($isLoop) { |
|
47
|
|
|
$key = substr($key, 0, -2); |
|
48
|
|
|
} |
|
49
|
|
|
$data = $resolved[$key]; |
|
50
|
|
|
if (!$isLoop) { |
|
51
|
|
|
$resolved[$key] = $resolver->resolve($data); |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
foreach ($data as $subKey => $subData) { |
|
56
|
|
|
$resolved[$key][$subKey] = $resolver->resolve($subData); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $resolved; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function loadConfigDefaults(array $config): OptionsResolver |
|
64
|
|
|
{ |
|
65
|
|
|
if (!empty($config['defaults'])) { |
|
66
|
|
|
$this->setDefaults($config['defaults']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function loadConfigRequired(array $config): OptionsResolver |
|
73
|
|
|
{ |
|
74
|
|
|
if (!empty($config['required'])) { |
|
75
|
|
|
$this->setRequired($config['required']); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function loadConfigTypes(array $config): OptionsResolver |
|
82
|
|
|
{ |
|
83
|
|
|
if (!empty($config['types'])) { |
|
84
|
|
|
foreach ($config['types'] as $option => $types) { |
|
85
|
|
|
$this->setAllowedTypes($option, $types); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function loadConfigValues(array $config): OptionsResolver |
|
93
|
|
|
{ |
|
94
|
|
|
if (!empty($config['values'])) { |
|
95
|
|
|
foreach ($config['values'] as $option => $values) { |
|
96
|
|
|
$this->setAllowedValues($option, $values); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
protected function loadConfigNested(array $config): OptionsResolver |
|
104
|
|
|
{ |
|
105
|
|
|
if (!empty($config['nested'])) { |
|
106
|
|
|
foreach ($config['nested'] as $option => $nestedConfig) { |
|
107
|
|
|
$this->setNested($option, static::make($nestedConfig)); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|