codebase.watch what is new in your dependencies

Options::setNormalizer($option, \Closure $normalizer)

At a glance

Type
New feature
Method added
2.1

Since symfony/options-resolver 2.1, Options::setNormalizer() transforms the value of an option when it is read.

Normalize an option

Option normalized

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');

Normalizer set before the value

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');

Value replaced

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');

Value of another option

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');

Lazy value normalized

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');

Normalizer replaced

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');

Normalizer without value

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');

Own option read

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');

Not a closure

  • PHP 5.3 – 5.6An E_RECOVERABLE_ERROR: Argument 2 passed to Symfony\Component\OptionsResolver\Options::setNormalizer() must be an instance of Closure, string given.
  • PHP 7.0 – 7.4A TypeError is thrown: Argument 2 passed to Symfony\Component\OptionsResolver\Options::setNormalizer() must be an instance of Closure, string given.
  • PHP >= 8.0A 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');

Read-only once read

Once an option is read, normalizers cannot be added anymore.

Normalizer added after a read

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)
);

The sources