Allow giving a callback as an allowedValue to OptionsResolver
This commit is contained in:
parent
fae3e35ef5
commit
07d1d3074e
|
@ -294,8 +294,14 @@ class OptionsResolver implements OptionsResolverInterface
|
|||
private function validateOptionValues(array $options)
|
||||
{
|
||||
foreach ($this->allowedValues as $option => $allowedValues) {
|
||||
if (isset($options[$option]) && !in_array($options[$option], $allowedValues, true)) {
|
||||
throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"', $option, $options[$option], implode('", "', $allowedValues)));
|
||||
if (isset($options[$option])) {
|
||||
if (is_array($allowedValues) && !in_array($options[$option], $allowedValues, true)) {
|
||||
throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"', $option, $options[$option], implode('", "', $allowedValues)));
|
||||
}
|
||||
|
||||
if (is_callable($allowedValues) && !call_user_func($allowedValues, $options[$option])) {
|
||||
throw new InvalidOptionsException(sprintf('The option "%s" has the value "%s", which it is not valid', $option, $options[$option]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -658,6 +658,45 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
|
|||
$this->assertEquals($options, $this->resolver->resolve($options));
|
||||
}
|
||||
|
||||
public function testResolveSucceedsIfValueAllowedCallbackReturnsTrue()
|
||||
{
|
||||
$this->resolver->setRequired(array(
|
||||
'test',
|
||||
));
|
||||
$this->resolver->setAllowedValues(array(
|
||||
'test' => function ($value) {
|
||||
return true;
|
||||
},
|
||||
));
|
||||
|
||||
$options = array(
|
||||
'test' => true,
|
||||
);
|
||||
|
||||
$this->assertEquals($options, $this->resolver->resolve($options));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
|
||||
*/
|
||||
public function testResolveFailsIfValueAllowedCallbackReturnsFalse()
|
||||
{
|
||||
$this->resolver->setRequired(array(
|
||||
'test',
|
||||
));
|
||||
$this->resolver->setAllowedValues(array(
|
||||
'test' => function ($value) {
|
||||
return false;
|
||||
},
|
||||
));
|
||||
|
||||
$options = array(
|
||||
'test' => true,
|
||||
);
|
||||
|
||||
$this->assertEquals($options, $this->resolver->resolve($options));
|
||||
}
|
||||
|
||||
public function testClone()
|
||||
{
|
||||
$this->resolver->setDefaults(array('one' => '1'));
|
||||
|
|
Reference in New Issue