Options implements CountableCountable addedSince symfony/options-resolver 2.1, Options implements Countable.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// 0
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
// 2
$count = count($options);
count($options) calls the count() method.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
// 2
$count = $options->count();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('key', null);
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('key', 'foo');
$options->set('key', 'bar');
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Not called by count()
function (Options $options) {
throw new LogicException('foo');
}
);
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set(
'key',
// Not called by count()
fn (Options $options): never => throw new LogicException('foo')
);
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('key');
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->clear();
// 0
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// A normalizer does not create the option
$options->setNormalizer(
'email',
function (Options $options, $value) {
return strtoupper($value);
}
);
// 0
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
// A normalizer does not create the option
$options->setNormalizer(
'email',
fn (Options $options, string $value): string => strtoupper($value)
);
// 0
$count = count($options);
COUNT_RECURSIVE is ignored for a Countable object: the values of the options are not counted, unlike with an array.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('roles', array('foo', 'bar'));
// 1
$count = count($options, COUNT_RECURSIVE);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('roles', ['foo', 'bar']);
// 1
$count = count($options, COUNT_RECURSIVE);
all()all() calls the normalizer of an option that does not exist: its result creates the option, which is then counted.
E_NOTICE, Undefined index: email, then the option is created and counted.E_WARNING, Undefined array key "email", then the option is created and counted.use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
function (Options $options, $value) {
return $value;
}
);
// E_NOTICE: Undefined index: email
$options->all();
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
fn (Options $options, $value) => $value
);
// E_NOTICE: Undefined index: email
$options->all();
// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->setNormalizer(
'email',
fn (Options $options, mixed $value): mixed => $value
);
// E_WARNING: Undefined array key "email"
$options->all();
// 1
$count = count($options);
count()count() does not read the options: they can still be set.
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('key', 'foo');
count($options);
$options->set('key', 'bar');
// array('key' => 'bar')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;
$options = new Options();
$options->set('key', 'foo');
count($options);
$options->set('key', 'bar');
// ['key' => 'bar']
$all = $options->all();