Options::clear()clear() addedSince symfony/options-resolver 2.1, Options::clear() removes all the options.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->clear();
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->clear();
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Never called: the option is cleared before being read
function (Options $options) {
throw new LogicException('foo');
}
);
$options->clear();
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Never called: the option is cleared before being read
function (Options $options) {
throw new LogicException('foo');
}
);
$options->clear();
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Never called: the option is cleared before being read
fn (Options $options): never => throw new LogicException('foo')
);
$options->clear();
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->clear();
$options->set('key', 'bar');
// array('key' => 'bar')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->clear();
$options->set('key', 'bar');
// ['key' => 'bar']
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// Does nothing: there is no option
$options->clear();
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// Does nothing: there is no option
$options->clear();
// []
$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);
}
);
$options->clear();
$options->set('email', 'foo@bar.com');
// "email" not normalized: clear() also removes the normalizers
// 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);
}
);
$options->clear();
$options->set('email', 'foo@bar.com');
// "email" not normalized: clear() also removes the normalizers
// ['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)
);
$options->clear();
$options->set('email', 'foo@bar.com');
// "email" not normalized: clear() also removes the normalizers
// ['email' => 'foo@bar.com']
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
function (Options $options, $value) {
return $value;
}
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
function (Options $options, $value) {
return $value;
}
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
fn (Options $options, $value) => $value
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
fn (Options $options, mixed $value): mixed => $value
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();
// []
$all = $options->all();
Once an option is read, the options cannot be cleared anymore.
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 cleared anymore once options have been read.
$options->clear();