ArrayKeyChoiceList deprecated

class ArrayKeyChoiceList extends ArrayChoiceList

deprecated

since version 2.8, to be removed in 3.0. Use ArrayChoiceList instead.

A list of choices that can be stored in the keys of a PHP array.

PHP arrays accept only strings and integers as array keys. Other scalar types are cast to integers and strings according to the description of {@link toArrayKey()}. This implementation applies the same casting rules for the choices passed to the constructor and to {@link getValuesForChoices()}.

By default, the choices are cast to strings and used as values. Optionally, you may pass custom values. The keys of the value array must match the keys of the choice array.

Example:

```php $choices = array('' => 'Don\'t know', 0 => 'No', 1 => 'Yes'); $choiceList = new ArrayKeyChoiceList(array_keys($choices));

$values = $choiceList->getValues() // => array('', '0', '1')

$selectedValues = $choiceList->getValuesForChoices(array(true)); // => array('1') ```

Methods

__construct(array|Traversable $choices, callable|null $value = null)

Creates a list with the given choices and values.

array getChoices()

Returns all selectable choices.

from ArrayChoiceList
string[] getValues()

Returns the values for the choices.

from ArrayChoiceList
string[] getStructuredValues()

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

from ArrayChoiceList
int[]|string[] getOriginalKeys()

Returns the original keys of the choices.

from ArrayChoiceList
array getChoicesForValues(array $values)

Returns the choices corresponding to the given values.

string[] getValuesForChoices(array $choices)

Returns the values corresponding to the given choices.

static int|string toArrayKey(mixed $choice)

Casts the given choice to an array key.

Details

__construct(array|Traversable $choices, callable|null $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. Each choice must be castable to an integer/string according to the casting rules described in {@link toArrayKey()}.

If no values are given, the choices are cast to strings and used as values.

Parameters

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

Exceptions

InvalidArgumentException If the keys of the choices don't match the keys of the values or if any of the choices is not scalar

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

static int|string toArrayKey(mixed $choice)

Casts the given choice to an array key.

PHP arrays accept only strings and integers as array keys. Integer strings such as "42" are automatically cast to integers. The boolean values "true" and "false" are cast to the integers 1 and 0. Every other scalar value is cast to a string.

Parameters

mixed $choice The choice

Return Value

int|string The choice as PHP array key

Exceptions

InvalidArgumentException If the choice is not scalar
doc_Symfony
2016-10-28 06:05:53
Comments
Leave a Comment

Please login to continue.