Options::setNormalizer($option, \Closure $normalizer)Since symfony/options-resolver 2.1, Options::setNormalizer() transforms the value of an option when it is read.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);
// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// Not called yet: the normalizer is called when the option is read
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
$options->set('email', 'foo@bar.com');
// Calls the normalizer
// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// Not called yet: the normalizer is called when the option is read
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);
$options->set('email', 'foo@bar.com');
// Calls the normalizer
// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
// The normalizer is kept: set() only replaces the value
$options->set('email', 'baz@bar.com');
// 'BAZ@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);
// The normalizer is kept: set() only replaces the value
$options->set('email', 'baz@bar.com');
// 'BAZ@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('host', 'foo.com');
$options->set('url', '/bar');
$options->setNormalizer(
'url',
function (Options $options, $value) {
return 'https://' . $options->get('host') . $value;
}
);
// 'https://foo.com/bar'
$url = $options->get('url');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('host', 'foo.com');
$options->set('url', '/bar');
$options->setNormalizer(
'url',
fn (Options $options, string $value): string => 'https://' . $options->get('host') . $value
);
// 'https://foo.com/bar'
$url = $options->get('url');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'email',
function (Options $options) {
return 'foo@bar.com';
}
);
// Receives in $value the result of the lazy option
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'email',
fn (Options $options): string => 'foo@bar.com'
);
// Receives in $value the result of the lazy option
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);
// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
// Replaced by the next normalizer: never called
function (Options $options, $value) {
return strtoupper($value);
}
);
$options->setNormalizer(
'email',
function (Options $options, $value) {
return $value . '.baz';
}
);
// 'foo@bar.com.baz'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
// Replaced by the next normalizer: never called
fn (Options $options, string $value): string => strtoupper($value)
);
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => $value . '.baz'
);
// 'foo@bar.com.baz'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// A normalizer does not create the option
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
// OutOfBoundsException: The option "email" does not exist.
$options->get('email');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// A normalizer does not create the option
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);
// OutOfBoundsException: The option "email" does not exist.
$options->get('email');
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
// The value of the option is given in $value: reading the option calls the normalizer again
function (Options $options, $value) {
return strtoupper($options->get('email'));
}
);
// OptionDefinitionException: The options "email" have a cyclic dependency.
$options->get('email');
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
'email',
// The value of the option is given in $value: reading the option calls the normalizer again
fn (Options $options, string $value): string => strtoupper($options->get('email'))
);
// OptionDefinitionException: The options "email" have a cyclic dependency.
$options->get('email');
E_RECOVERABLE_ERROR: Argument 2 passed to Symfony\Component\OptionsResolver\Options::setNormalizer() must be an instance of Closure, string given.TypeError is thrown: Argument 2 passed to Symfony\Component\OptionsResolver\Options::setNormalizer() must be an instance of Closure, string given.TypeError is thrown: Symfony\Component\OptionsResolver\Options::setNormalizer(): Argument #2 ($normalizer) must be of type Closure, string given.use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
// E_RECOVERABLE_ERROR: Argument 2 passed to Symfony\Component\OptionsResolver\Options::setNormalizer() must be an instance of Closure, string given
$options->setNormalizer('email', 'strtoupper');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
// TypeError: Argument 2 passed to Symfony\Component\OptionsResolver\Options::setNormalizer() must be an instance of Closure, string given
$options->setNormalizer('email', 'strtoupper');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
// TypeError: Symfony\Component\OptionsResolver\Options::setNormalizer(): Argument #2 ($normalizer) must be of type Closure, string given
$options->setNormalizer('email', 'strtoupper');
Once an option is read, normalizers cannot be added anymore.
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->get('email');
// OptionDefinitionException: Normalizers cannot be added anymore once options have been read.
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('email', 'foo@bar.com');
$options->get('email');
// OptionDefinitionException: Normalizers cannot be added anymore once options have been read.
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);