Options::remove($option)remove() addedSince symfony/options-resolver 2.1, Options::remove() removes an option.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('key');
// array('firstName' => 'foo')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('key');
// ['firstName' => 'foo']
$all = $options->all();
Options implements ArrayAccess: unsetting an offset calls remove().
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('key', 'foo');
// Calls $options->remove('key')
unset($options['key']);
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('key', 'foo');
// Calls $options->remove('key')
unset($options['key']);
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Never called: the option is removed before being read
function (Options $options) {
throw new LogicException('foo');
}
);
$options->remove('key');
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Never called: the option is removed before being read
function (Options $options) {
throw new LogicException('foo');
}
);
$options->remove('key');
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Never called: the option is removed before being read
fn (Options $options): never => throw new LogicException('foo')
);
$options->remove('key');
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// Does nothing: the option does not exist
$options->remove('key');
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// Does nothing: the option does not exist
$options->remove('key');
// []
$all = $options->all();
An option set again after its removal comes after the other options.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('firstName');
$options->set('firstName', 'baz');
// array('key' => 'bar', 'firstName' => 'baz')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('firstName');
$options->set('firstName', 'baz');
// ['key' => 'bar', 'firstName' => 'baz']
$all = $options->all();
The lazy option is only evaluated when it is read: the option it reads no longer exists.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set(
'key',
function (Options $options) {
return strtoupper($options->get('firstName'));
}
);
$options->remove('firstName');
// OutOfBoundsException: The option "firstName" does not exist.
$options->get('key');
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set(
'key',
fn (Options $options): string => strtoupper($options->get('firstName'))
);
$options->remove('firstName');
// OutOfBoundsException: The option "firstName" does not exist.
$options->get('key');
remove() also removes the normalizer of the option: the value set again is not normalized.
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->remove('email');
$options->set('email', 'foo@bar.com');
// 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->remove('email');
$options->set('email', 'foo@bar.com');
// ['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->remove('email');
$options->set('email', 'foo@bar.com');
// ['email' => 'foo@bar.com']
$all = $options->all();
Without remove(), all() would call the normalizer and create the option.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
function (Options $options, $value) {
return $value;
}
);
$options->remove('email');
// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
function (Options $options, $value) {
return $value;
}
);
$options->remove('email');
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
fn (Options $options, $value) => $value
);
$options->remove('email');
// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
fn (Options $options, mixed $value): mixed => $value
);
$options->remove('email');
// []
$all = $options->all();
Once an option is read, the options cannot be removed 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 removed anymore once options have been read.
$options->remove('key');