diff --git a/src/Symfony/Component/OptionsResolver/OptionsResolver.php b/src/Symfony/Component/OptionsResolver/OptionsResolver.php index bcaea9e4cbc0f..8067fda0cd39f 100644 --- a/src/Symfony/Component/OptionsResolver/OptionsResolver.php +++ b/src/Symfony/Component/OptionsResolver/OptionsResolver.php @@ -367,7 +367,7 @@ public function getDefinedOptions() } /** - * Sets the normalizer for an option. + * Sets the normalizer for one or more option. * * The normalizer should be a closure with the following signature: * @@ -386,32 +386,34 @@ public function getDefinedOptions() * * The resolved option value is set to the return value of the closure. * - * @param string $option The option name - * @param \Closure $normalizer The normalizer + * @param string|string[] $optionNames One or more option names + * @param \Closure $normalizer The normalizer * * @return OptionsResolver This instance * * @throws UndefinedOptionsException If the option is undefined * @throws AccessException If called from a lazy option or normalizer */ - public function setNormalizer($option, \Closure $normalizer) + public function setNormalizer($optionNames, \Closure $normalizer) { if ($this->locked) { throw new AccessException('Normalizers cannot be set from a lazy option or normalizer.'); } - if (!isset($this->defined[$option])) { - throw new UndefinedOptionsException(sprintf( - 'The option "%s" does not exist. Defined options are: "%s".', - $option, - implode('", "', array_keys($this->defined)) - )); - } + foreach ((array) $optionNames as $option) { + if (!isset($this->defined[$option])) { + throw new UndefinedOptionsException(sprintf( + 'The option "%s" does not exist. Defined options are: "%s".', + $option, + implode('", "', array_keys($this->defined)) + )); + } - $this->normalizers[$option] = $normalizer; + $this->normalizers[$option] = $normalizer; - // Make sure the option is processed - unset($this->resolved[$option]); + // Make sure the option is processed + unset($this->resolved[$option]); + } return $this; } diff --git a/src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php b/src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php index 216347a7950fa..edf2b30b72d1a 100644 --- a/src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php +++ b/src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php @@ -940,6 +940,18 @@ public function testSetNormalizerClosure() $this->assertEquals(array('foo' => 'normalized'), $this->resolver->resolve()); } + public function testSetNormalizerWithMultipleFieldsClosure() + { + $this->resolver->setDefault('foo', 'bar'); + $this->resolver->setDefault('bar', 'qux'); + + $this->resolver->setNormalizer(array('foo', 'bar'), function () { + return 'normalized'; + }); + + $this->assertEquals(array('foo' => 'normalized', 'bar' => 'normalized'), $this->resolver->resolve()); + } + /** * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException */