@throws UndefinedOptionsException If the option is undefined

@throws AccessException           If called from a lazy option or normalizer

public function setNormalizer(string $option, \Closure $normalizer): static


Sometimes, option values need to be normalized before you can use them. For instance, assume that the host should always start with http://. To do that, you can write normalizers. Normalizers are executed after validating an option. You can configure a normalizer by calling setNormalizer():


Dans le composant OptionsResolver de Symfony, la méthode setNormalizer() est utilisée pour normaliser la valeur d'une option.


To normalize a new allowed value in subclasses that are being normalized in parent classes, use addNormalizer() method. 


use Symfony\Component\OptionsResolver\Options;

// ...
class Mailer
{
    // ...

    public function configureOptions(OptionsResolver $resolver): void
    {
        // ...

        $resolver->setNormalizer('host', function (Options $options, string $value): string {
            if (!str_starts_with($value, 'http://')) {
                $value = 'http://'.$value;
            }

            return $value;
        });
    }
}


class Mailer
{
    // ...
    public function configureOptions(OptionsResolver $resolver): void
    {
        // ...
        $resolver->setNormalizer('host', function (Options $options, string $value): string {
            if (!str_starts_with($value, 'http://') && !str_starts_with($value, 'https://')) {
                if ('ssl' === $options['encryption']) {
                    $value = 'https://'.$value;
                } else {
                    $value = 'http://'.$value;
                }
            }

            return $value;
        });
    }
}




Powered by Froala Editor