class ArrayChoiceList implements ChoiceListInterface

A list of choices with arbitrary data types.

The user of this class is responsible for assigning string values to the choices annd for their uniqueness. Both the choices and their values are passed to the constructor. Each choice must have a corresponding value (with the same key) in the values array.

Properties

protected array $choices The choices in the list.
protected array $structuredValues The values indexed by the original keys.
protected int[]|string[] $originalKeys The original keys of the choices array.
protected $valueCallback

Methods

__construct(iterable $choices, callable $value = null)

Creates a list with the given choices and values.

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.

flatten(array $choices, callable $value, array $choicesByValues, array $keysByValues, array $structuredValues)

Flattens an array into the given output variables.

Details

at line 60
__construct(iterable $choices, callable $value = null)

Creates a list with the given choices and values.

The given choice array must have the same array keys as the value array.

Parameters

iterable $choices The selectable choices
callable $value The callable for creating the value for a choice. If null is passed, incrementing integers are used as values

at line 96
array getChoices()

Returns all selectable choices.

Return Value

array The selectable choices indexed by the corresponding values

at line 104
string[] getValues()

Returns the values for the choices.

The values are strings that do not contain duplicates:

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

In this example, the result of this method is:

[
    'Yes' => '0',
    'No' => '1',
    'Maybe' => '2',
]

Null and false MUST NOT conflict when being casted to string. For this some default incremented values SHOULD be computed.

Return Value

string[] The choice values

at line 112
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', [
    'choices' => [
        'Decided' => ['Yes' => true, 'No' => false],
        'Undecided' => ['Maybe' => null],
    ],
]);

In this example, the result of this method is:

[
    'Decided' => ['Yes' => '0', 'No' => '1'],
    'Undecided' => ['Maybe' => '2'],
]

Nested arrays do not make sense in a view format unless they are used as a convenient way of grouping. If the implementation does not intend to support grouped choices, this method SHOULD be equivalent to {@link getValues()}. The $groupBy callback parameter SHOULD be used instead.

Return Value

string[] The choice values

at line 120
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', [
    'choices' => [
        'Decided' => [true, false],
        'Undecided' => [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

at line 128
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

at line 144
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

at line 185
protected flatten(array $choices, callable $value, array $choicesByValues, array $keysByValues, array $structuredValues)

Flattens an array into the given output variables.

Parameters

array $choices The array to flatten
callable $value The callable for generating choice values
array $choicesByValues The flattened choices indexed by the corresponding values
array $keysByValues The original keys indexed by the corresponding values
array $structuredValues The values indexed by the original keys