Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit07d1d30

Browse files
committed
Allow giving a callback as an allowedValue to OptionsResolver
1 parentfae3e35 commit07d1d30

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

‎src/Symfony/Component/OptionsResolver/OptionsResolver.php‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,14 @@ private function validateOptionsCompleteness(array $options)
294294
privatefunctionvalidateOptionValues(array$options)
295295
{
296296
foreach ($this->allowedValuesas$option =>$allowedValues) {
297-
if (isset($options[$option]) && !in_array($options[$option],$allowedValues,true)) {
298-
thrownewInvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"',$option,$options[$option],implode('", "',$allowedValues)));
297+
if (isset($options[$option])) {
298+
if (is_array($allowedValues) && !in_array($options[$option],$allowedValues,true)) {
299+
thrownewInvalidOptionsException(sprintf('The option "%s" has the value "%s", but is expected to be one of "%s"',$option,$options[$option],implode('", "',$allowedValues)));
300+
}
301+
302+
if (is_callable($allowedValues) && !call_user_func($allowedValues,$options[$option])) {
303+
thrownewInvalidOptionsException(sprintf('The option "%s" has the value "%s", which it is not valid',$option,$options[$option]));
304+
}
299305
}
300306
}
301307
}

‎src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,45 @@ public function testResolveSucceedsIfOptionRequiredAndValueAllowed()
658658
$this->assertEquals($options,$this->resolver->resolve($options));
659659
}
660660

661+
publicfunctiontestResolveSucceedsIfValueAllowedCallbackReturnsTrue()
662+
{
663+
$this->resolver->setRequired(array(
664+
'test',
665+
));
666+
$this->resolver->setAllowedValues(array(
667+
'test' =>function ($value) {
668+
returntrue;
669+
},
670+
));
671+
672+
$options =array(
673+
'test' =>true,
674+
);
675+
676+
$this->assertEquals($options,$this->resolver->resolve($options));
677+
}
678+
679+
/**
680+
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
681+
*/
682+
publicfunctiontestResolveFailsIfValueAllowedCallbackReturnsFalse()
683+
{
684+
$this->resolver->setRequired(array(
685+
'test',
686+
));
687+
$this->resolver->setAllowedValues(array(
688+
'test' =>function ($value) {
689+
returnfalse;
690+
},
691+
));
692+
693+
$options =array(
694+
'test' =>true,
695+
);
696+
697+
$this->assertEquals($options,$this->resolver->resolve($options));
698+
}
699+
661700
publicfunctiontestClone()
662701
{
663702
$this->resolver->setDefaults(array('one' =>'1'));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp