codebase.watch what is new in your dependencies

Options::replace(array $options)

At a glance

Type
New feature
replace() added
2.1

Since symfony/options-resolver 2.1, Options::replace() replaces all the options by the given ones.

Replace all options

Options replaced

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->replace(array('key' => 'bar'));

// array('key' => 'bar')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->replace(['key' => 'bar']);

// ['key' => 'bar']
$all = $options->all();

Empty array

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->replace(array());

// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->replace([]);

// []
$all = $options->all();

Order of the given options

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set('lastName', 'bar');
$options->replace(array('lastName' => 'bar', 'firstName' => 'foo'));

// The options are in the order of the given array, whatever their order before
// array('lastName' => 'bar', 'firstName' => 'foo')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set('lastName', 'bar');
$options->replace(['lastName' => 'bar', 'firstName' => 'foo']);

// The options are in the order of the given array, whatever their order before
// ['lastName' => 'bar', 'firstName' => 'foo']
$all = $options->all();

Lazy values

A closure whose first argument is type-hinted Options is only called when the option is read, and can read the other options.

Its optional second argument, $previousValue, always receives null: replace() removes all the previous values first.

Lazy value reading an option given after it

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace(array(
    'key' => function (Options $options) {
        return $options->get('firstName');
    },
    'firstName' => 'foo',
));

// 'foo'
$key = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    'key' => function (Options $options) {
        return $options->get('firstName');
    },
    'firstName' => 'foo',
]);

// 'foo'
$key = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    'key' => fn (Options $options): string => $options->get('firstName'),
    'firstName' => 'foo',
]);

// 'foo'
$key = $options->get('key');

Previous value discarded

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->replace(array(
    'key' => function (Options $options, $previousValue) {
        return $previousValue;
    },
));

// null
$key = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->replace([
    'key' => function (Options $options, $previousValue) {
        return $previousValue;
    },
]);

// null
$key = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->replace([
    'key' => fn (Options $options, $previousValue) => $previousValue,
]);

// null
$key = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->replace([
    'key' => fn (Options $options, mixed $previousValue): mixed => $previousValue,
]);

// null
$key = $options->get('key');

Closure not typed with Options

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace(array(
    // A closure whose first argument, $options, is not type-hinted Options is not lazy:
    // it is the value of the option
    'key' => function ($options) {
        return 'foo';
    },
));

// true
$isClosure = $options->get('key') instanceof Closure;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    // A closure whose first argument, $options, is not type-hinted Options is not lazy:
    // it is the value of the option
    'key' => function ($options) {
        return 'foo';
    },
]);

// true
$isClosure = $options->get('key') instanceof Closure;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    // A closure whose first argument, $options, is not type-hinted Options is not lazy:
    // it is the value of the option
    'key' => fn ($options): string => 'foo',
]);

// true
$isClosure = $options->get('key') instanceof Closure;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    // A closure whose first argument, $options, is not type-hinted Options is not lazy:
    // it is the value of the option
    'key' => fn (mixed $options): string => 'foo',
]);

// true
$isClosure = $options->get('key') instanceof Closure;

Cyclic dependency

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace(array(
    'firstName' => function (Options $options) {
        return $options->get('lastName');
    },
    'lastName' => function (Options $options) {
        return $options->get('firstName');
    },
));

// OptionDefinitionException: The options "firstName", "lastName" have a cyclic dependency.
$options->all();
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    'firstName' => function (Options $options) {
        return $options->get('lastName');
    },
    'lastName' => function (Options $options) {
        return $options->get('firstName');
    },
]);

// OptionDefinitionException: The options "firstName", "lastName" have a cyclic dependency.
$options->all();
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->replace([
    'firstName' => fn (Options $options): string => $options->get('lastName'),
    'lastName' => fn (Options $options): string => $options->get('firstName'),
]);

// OptionDefinitionException: The options "firstName", "lastName" have a cyclic dependency.
$options->all();

Normalizers kept

Unlike clear(), replace() keeps the normalizers: they apply to the new values.

Normalizer set before

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->replace(array('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',
    function (Options $options, $value) {
        return strtoupper($value);
    }
);
$options->replace(['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->replace(['email' => 'foo@bar.com']);

// Calls the normalizer
// 'FOO@BAR.COM'
$email = $options->get('email');

Option not given

The normalizer of an option not given to replace() is still called, with null: PHP reports the undefined key, and the option is added back.

  • PHP 5.3 – 7.4An E_NOTICE, Undefined index: email, then the normalizer receives null.
  • PHP >= 8.0An E_WARNING, Undefined array key "email", then the normalizer receives null.
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
$options->replace(array('key' => 'bar'));

// E_NOTICE: Undefined index: email
// array('key' => 'bar', 'email' => null)
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
$options->replace(['key' => 'bar']);

// E_NOTICE: Undefined index: email
// ['key' => 'bar', 'email' => null]
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    fn (Options $options, $value) => $value
);
$options->replace(['key' => 'bar']);

// E_NOTICE: Undefined index: email
// ['key' => 'bar', 'email' => null]
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    fn (Options $options, mixed $value): mixed => $value
);
$options->replace(['key' => 'bar']);

// E_WARNING: Undefined array key "email"
// ['key' => 'bar', 'email' => null]
$all = $options->all();

Read-only once read

Once an option is read, the options cannot be set, overloaded, replaced, removed or cleared anymore.

Reading one option locks all of them.

Replace after count()

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
count($options);
// count() does not read the options: they can still be replaced
$options->replace(array('key' => 'bar'));

// array('key' => 'bar')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
count($options);
// count() does not read the options: they can still be replaced
$options->replace(['key' => 'bar']);

// ['key' => 'bar']
$all = $options->all();

Replace after a read

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->get('key');

// OptionDefinitionException: Options cannot be replaced anymore once options have been read.
$options->replace(array('key' => 'bar'));
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->get('key');

// OptionDefinitionException: Options cannot be replaced anymore once options have been read.
$options->replace(['key' => 'bar']);

Replace after a loop

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// A foreach on the options reads each of them: they cannot be replaced anymore
foreach ($options as $value) {}

// OptionDefinitionException: Options cannot be replaced anymore once options have been read.
$options->replace(array('key' => 'bar'));
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// A foreach on the options reads each of them: they cannot be replaced anymore
foreach ($options as $value) {}

// OptionDefinitionException: Options cannot be replaced anymore once options have been read.
$options->replace(['key' => 'bar']);

The sources