You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/OptionsResolver.php
+28-6Lines changed: 28 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,13 @@ class OptionsResolver implements Options
36
36
*/
37
37
private$defaults = array();
38
38
39
+
/**
40
+
* A list of nested options.
41
+
*
42
+
* @var self[]
43
+
*/
44
+
private$nested = array();
45
+
39
46
/**
40
47
* The names of required options.
41
48
*/
@@ -171,12 +178,17 @@ public function setDefault($option, $value)
171
178
// This option is not lazy anymore
172
179
unset($this->lazy[$option]);
173
180
174
-
// Yet undefined options can be marked as resolved, because we only need
175
-
// to resolve options with lazy closures, normalizers or validation
176
-
// rules, none of which can exist for undefined options
177
-
// If the option was resolved before, update the resolved value
178
-
if (!isset($this->defined[$option]) || array_key_exists($option, $this->resolved)) {
179
-
$this->resolved[$option] = $value;
181
+
if ($valueinstanceof self) {
182
+
$this->nested[$option] = $value;
183
+
$value = array();
184
+
} else {
185
+
// Yet undefined options can be marked as resolved, because we only need
186
+
// to resolve options with lazy closures, normalizers or validation
187
+
// rules, none of which can exist for undefined options
188
+
// If the option was resolved before, update the resolved value
189
+
if (!isset($this->defined[$option]) || array_key_exists($option, $this->resolved)) {
190
+
$this->resolved[$option] = $value;
191
+
}
180
192
}
181
193
182
194
$this->defaults[$option] = $value;
@@ -614,6 +626,7 @@ public function clear()
614
626
615
627
$this->defined = array();
616
628
$this->defaults = array();
629
+
$this->nested = array();
617
630
$this->required = array();
618
631
$this->resolved = array();
619
632
$this->lazy = array();
@@ -745,6 +758,15 @@ public function offsetGet($option)
745
758
746
759
$value = $this->defaults[$option];
747
760
761
+
// Resolve the option if the default value is nested
762
+
if (isset($this->nested[$option])) {
763
+
if (!\is_array($value)) {
764
+
thrownewInvalidOptionsException(sprintf('The nested option "%s" with value %s is expected to be of type array, but is of type "%s".', $option, $this->formatValue($value), $this->formatTypeOf($value, 'array')));
765
+
}
766
+
767
+
$value = $this->nested[$option]->resolve($value);
768
+
}
769
+
748
770
// Resolve the option if the default value is lazily evaluated
749
771
if (isset($this->lazy[$option])) {
750
772
// If the closure is already being called, we have a cyclic
Copy file name to clipboardExpand all lines: src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php
+212-1Lines changed: 212 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -593,7 +593,6 @@ public function provideInvalidTypes()
593
593
array(false, 'string', 'The option "option" with value false is expected to be of type "string", but is of type "boolean".'),
594
594
array(fopen(__FILE__, 'r'), 'string', 'The option "option" with value resource is expected to be of type "string", but is of type "resource".'),
595
595
array(array(), 'string', 'The option "option" with value array is expected to be of type "string", but is of type "array".'),
596
-
array(newOptionsResolver(), 'string', 'The option "option" with value Symfony\Component\OptionsResolver\OptionsResolver is expected to be of type "string", but is of type "Symfony\Component\OptionsResolver\OptionsResolver".'),
597
596
array(42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "integer".'),
598
597
array(null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "NULL".'),
599
598
array('bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'),
@@ -1650,4 +1649,216 @@ public function testCountFailsOutsideResolve()
0 commit comments