8000 bug #13816 [OptionsResolver] fix allowed values with null (Tobion) · symfony/symfony@38a5ebd · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 38a5ebd

Browse files
committed
bug #13816 [OptionsResolver] fix allowed values with null (Tobion)
This PR was merged into the 2.6 branch. Discussion ---------- [OptionsResolver] fix allowed values with null | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | - 1. setNormalizers was missing deprecated annotation 2. setAllowedValues/addAllowedValues with `null` was broken because `(array) null === array()` Commits ------- cb37fbe [OptionsResolver] fix allowed values with null e3e9a4d [OptionsResolver] add missing deprecated phpdoc for setNormalizers
2 parents c9da1ae + cb37fbe commit 38a5ebd

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,18 @@ public function setNormalizer($option, \Closure $normalizer)
418418
}
419419

420420
/**
421-
* {@inheritdoc}
421+
* Sets the normalizers for an array of options.
422+
*
423+
* @param array $normalizers An array of closures
424+
*
425+
* @return OptionsResolver This instance
426+
*
427+
* @throws UndefinedOptionsException If the option is undefined
428+
* @throws AccessException If called from a lazy option or normalizer
429+
*
430+
* @see setNormalizer()
431+
*
432+
* @deprecated since version 2.6, to be removed in 3.0.
422433
*/
423434
public function setNormalizers(array $normalizers)
424435
{
@@ -473,7 +484,7 @@ public function setAllowedValues($option, $allowedValues = null)
473484
));
474485
}
475486

476-
$this->allowedValues[$option] = $allowedValues instanceof \Closure ? array($allowedValues) : (array) $allowedValues;
487+
$this->allowedValues[$option] = is_array($allowedValues) ? $allowedValues : array($allowedValues);
477488

478489
// Make sure the option is processed
479490
unset($this->resolved[$option]);
@@ -527,12 +538,14 @@ public function addAllowedValues($option, $allowedValues = null)
527538
));
528539
}
529540

530-
if ($allowedValues instanceof \Closure) {
531-
$this->allowedValues[$option][] = $allowedValues;
532-
} elseif (!isset($this->allowedValues[$option])) {
533-
$this->allowedValues[$option] = (array) $allowedValues;
541+
if (!is_array($allowedValues)) {
542+
$allowedValues = array($allowedValues);
543+
}
544+
545+
if (!isset($this->allowedValues[$option])) {
546+
$this->allowedValues[$option] = $allowedValues;
534547
} else {
535-
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], (array) $allowedValues);
548+
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues);
536549
}
537550

538551
// Make sure the option is processed

src/Symfony/Component/OptionsResolver/Tests/OptionsResolver2Dot6Test.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,14 @@ public function testResolveSucceedsIfValidValue()
724724
$this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
725725
}
726726

727+
public function testResolveSucceedsIfValidValueIsNull()
728+
{
729+
$this->resolver->setDefault('foo', null);
730+
$this->resolver->setAllowedValues('foo', null);
731+
732+
$this->assertEquals(array('foo' => null), $this->resolver->resolve());
733+
}
734+
727735
/**
728736
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
729737
* @expectedExceptionMessage The option "foo" with value 42 is invalid. Accepted values are: "bar", false, null.
@@ -847,6 +855,14 @@ public function testResolveSucceedsIfValidAddedValue()
847855
$this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
848856
}
849857

858+
public function testResolveSucceedsIfValidAddedValueIsNull()
859+
{
860+
$this->resolver->setDefault('foo', null);
861+
$this->resolver->addAllowedValues('foo', null);
862+
863+
$this->assertEquals(array('foo' => null), $this->resolver->resolve());
864+
}
865+
850866
/**
851867
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
852868
*/

0 commit comments

Comments
 (0)
0