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

Appearance settings

[OptionsResolver] Allow setting same normalizer to multiple option #18254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions 30 src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.