W3cubDocs

/Symfony 4.1

LazyChoiceList

class LazyChoiceList implements ChoiceListInterface

A choice list that loads its choices lazily.

The choices are fetched using a {@link ChoiceLoaderInterface} instance. If only {@link getChoicesForValues()} or {@link getValuesForChoices()} is called, the choice list is only loaded partially for improved performance.

Once {@link getChoices()} or {@link getValues()} is called, the list is loaded fully.

Methods

__construct(ChoiceLoaderInterface $loader, callable $value = null)

Creates a lazily-loaded list using the given loader.

array getChoices()

Returns all selectable choices.

string[] getValues()

Returns the values for the choices.

string[] getStructuredValues()

Returns the values in the structure originally passed to the list.

int[]|string[] getOriginalKeys()

Returns the original keys of the choices.

array getChoicesForValues(array $values)

Returns the choices corresponding to the given values.

string[] getValuesForChoices(array $choices)

Returns the values corresponding to the given choices.

Details

__construct(ChoiceLoaderInterface $loader, callable $value = null)

Creates a lazily-loaded list using the given loader.

Optionally, a callable can be passed for generating the choice values. The callable receives the choice as first and the array key as the second argument.

Parameters

ChoiceLoaderInterface $loader The choice loader
callable $value The callable generating the choice values

array getChoices()

Returns all selectable choices.

Return Value

array The selectable choices indexed by the corresponding values

string[] getValues()

Returns the values for the choices.

The values are strings that do not contain duplicates.

Return Value

string[] The choice values

string[] getStructuredValues()

Returns the values in the structure originally passed to the list.

Contrary to {@link getValues()}, the result is indexed by the original keys of the choices. If the original array contained nested arrays, these nested arrays are represented here as well:

$form->add('field', 'choice', array(
    'choices' => array(
        'Decided' => array('Yes' => true, 'No' => false),
        'Undecided' => array('Maybe' => null),
    ),
));

In this example, the result of this method is:

array(
    'Decided' => array('Yes' => '0', 'No' => '1'),
    'Undecided' => array('Maybe' => '2'),
)

Return Value

string[] The choice values

int[]|string[] getOriginalKeys()

Returns the original keys of the choices.

The original keys are the keys of the choice array that was passed in the "choice" option of the choice type. Note that this array may contain duplicates if the "choice" option contained choice groups:

$form->add('field', 'choice', array(
    'choices' => array(
        'Decided' => array(true, false),
        'Undecided' => array(null),
    ),
));

In this example, the original key 0 appears twice, once for true and once for null.

Return Value

int[]|string[] The original choice keys indexed by the corresponding choice values

array getChoicesForValues(array $values)

Returns the choices corresponding to the given values.

The choices are returned with the same keys and in the same order as the corresponding values in the given array.

Parameters

array $values An array of choice values. Non-existing values in this array are ignored

Return Value

array An array of choices

string[] getValuesForChoices(array $choices)

Returns the values corresponding to the given choices.

The values are returned with the same keys and in the same order as the corresponding choices in the given array.

Parameters

array $choices An array of choices. Non-existing choices in this array are ignored

Return Value

string[] An array of choice values

© 2004–2017 Fabien Potencier
Licensed under the MIT License.
https://api.symfony.com/4.1/Symfony/Component/Form/ChoiceList/LazyChoiceList.html