Skip to content

Commit 88a17e5

Browse files
committed
[OptionsResolver] fix allowed values with null
1 parent 689890b commit 88a17e5

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

OptionsResolver.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public function setAllowedValues($option, $allowedValues = null)
484484
));
485485
}
486486

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

489489
// Make sure the option is processed
490490
unset($this->resolved[$option]);
@@ -538,12 +538,14 @@ public function addAllowedValues($option, $allowedValues = null)
538538
));
539539
}
540540

541-
if ($allowedValues instanceof \Closure) {
542-
$this->allowedValues[$option][] = $allowedValues;
543-
} elseif (!isset($this->allowedValues[$option])) {
544-
$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;
545547
} else {
546-
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], (array) $allowedValues);
548+
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues);
547549
}
548550

549551
// Make sure the option is processed

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)