Options::all()all() addedSince symfony/options-resolver 2.1, Options::all() reads the values of all the options.
It evaluates the lazy ones.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// []
$all = $options->all();
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();
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();
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();
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();
Unlike get(), all() calls the normalizer of an option that does not exist: it receives null, and its result creates the option.
E_NOTICE, Undefined index: email, then the option is created with the result of the normalizer.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();
all() reads every option: the options cannot be set, overloaded, replaced, removed or cleared anymore.
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');
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)
);
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();
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');