codebase.watch what is new in your dependencies

Options::all()

At a glance

Type
New feature
all() added
2.1

Since symfony/options-resolver 2.1, Options::all() reads the values of all the options.

It evaluates the lazy ones.

All the options

No option

use Symfony\Component\OptionsResolver\Options;

$options = new Options();

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

$options = new Options();

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

Options set

The options are returned in the order they were set.

use Symfony\Component\OptionsResolver\Options;

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

// array('firstName' => 'foo', 'lastName' => 'bar')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

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

// ['firstName' => 'foo', 'lastName' => 'bar']
$all = $options->all();

Lazy option evaluated

The lazy options are evaluated.

use Symfony\Component\OptionsResolver\Options;

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

// array('firstName' => 'foo', 'key' => 'FOO')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

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

// ['firstName' => 'foo', 'key' => 'FOO']
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set(
    'key',
    fn (Options $options): string => strtoupper($options->get('firstName'))
);

// ['firstName' => 'foo', 'key' => 'FOO']
$all = $options->all();

Lazy option set first

A lazy option keeps its place in the result, even when it reads an option set after it.

use Symfony\Component\OptionsResolver\Options;

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

// array('key' => 'FOO', 'firstName' => 'foo')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

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

// ['key' => 'FOO', 'firstName' => 'foo']
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    fn (Options $options): string => strtoupper($options->get('firstName'))
);
$options->set('firstName', 'foo');

// ['key' => 'FOO', 'firstName' => 'foo']
$all = $options->all();

Normalized options

Option normalized

The order of the calls to set() and setNormalizer() does not matter: the normalizer can be set before the value.

use Symfony\Component\OptionsResolver\Options;

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

// array('email' => 'FOO@BAR.COM')
$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 strtoupper($value);
    }
);

// ['email' => 'FOO@BAR.COM']
$all = $options->all();
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)
);

// ['email' => 'FOO@BAR.COM']
$all = $options->all();

Normalizer without value

Unlike get(), all() calls the normalizer of an option that does not exist: it receives null, and its result creates the option.

  • PHP 5.3 – 7.4An E_NOTICE, Undefined index: email, then the option is created with the result of the normalizer.
  • PHP >= 8.0An E_WARNING, Undefined array key "email", then the option is created with the result of the normalizer.
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);

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

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);

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

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, $value) => $value
);

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

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, mixed $value): mixed => $value
);

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

Read-only once read

all() reads every option: the options cannot be set, overloaded, replaced, removed or cleared anymore.

Set after a call

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

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

// OptionDefinitionException: Options cannot be set anymore once options have been read.
$options->set('key', 'bar');

Normalizer set after a call

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

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->all();

// 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->all();

// OptionDefinitionException: Normalizers cannot be added anymore once options have been read.
$options->setNormalizer(
    'email',
    fn (Options $options, string $value): string => strtoupper($value)
);

Cleared after a call

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

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

// OptionDefinitionException: Options cannot be cleared anymore once options have been read.
$options->clear();

Set after a call without option

all() locks the options, even when there is none.

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

$options = new Options();
$options->all();

// OptionDefinitionException: Options cannot be set anymore once options have been read.
$options->set('key', 'foo');

The sources