ArrayChoiceList
in package
implements
ChoiceListInterface
A list of choices with arbitrary data types.
The user of this class is responsible for assigning string values to the choices and 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.
Tags
Table of Contents
Interfaces
- ChoiceListInterface
- A list of choices that can be selected in a choice field.
Properties
- $choices : array<string|int, mixed>
- The choices in the list.
- $originalKeys : array<string|int, int>|array<string|int, string>
- The original keys of the choices array.
- $structuredValues : array<string|int, mixed>
- The values indexed by the original keys.
- $valueCallback : mixed
Methods
- __construct() : mixed
- Creates a list with the given choices and values.
- getChoices() : array<string|int, mixed>
- Returns all selectable choices.
- getChoicesForValues() : array<string|int, mixed>
- Returns the choices corresponding to the given values.
- getOriginalKeys() : array<string|int, int>|array<string|int, string>
- Returns the original keys of the choices.
- getStructuredValues() : array<string|int, string>
- Returns the values in the structure originally passed to the list.
- getValues() : array<string|int, string>
- Returns the values for the choices.
- getValuesForChoices() : array<string|int, string>
- Returns the values corresponding to the given choices.
- castableToString() : bool
- Checks whether the given choices can be cast to strings without generating duplicates.
Properties
$choices
The choices in the list.
protected
array<string|int, mixed>
$choices
$originalKeys
The original keys of the choices array.
protected
array<string|int, int>|array<string|int, string>
$originalKeys
$structuredValues
The values indexed by the original keys.
protected
array<string|int, mixed>
$structuredValues
$valueCallback
protected
mixed
$valueCallback
Methods
__construct()
Creates a list with the given choices and values.
public
__construct(iterable<string|int, mixed> $choices[, callable|null $value = null ]) : mixed
The given choice array must have the same array keys as the value array.
Parameters
- $choices : iterable<string|int, mixed>
-
The selectable choices
- $value : callable|null = null
-
The callable for creating the value for a choice. If
null
is passed, incrementing integers are used as values
getChoices()
Returns all selectable choices.
public
getChoices() : array<string|int, mixed>
Return values
array<string|int, mixed> —The selectable choices indexed by the corresponding values
getChoicesForValues()
Returns the choices corresponding to the given values.
public
getChoicesForValues(array<string|int, mixed> $values) : array<string|int, mixed>
Parameters
- $values : array<string|int, mixed>
-
An array of choice values. Non-existing values in this array are ignored
Return values
array<string|int, mixed>getOriginalKeys()
Returns the original keys of the choices.
public
getOriginalKeys() : array<string|int, int>|array<string|int, string>
Return values
array<string|int, int>|array<string|int, string> —The original choice keys indexed by the corresponding choice values
getStructuredValues()
Returns the values in the structure originally passed to the list.
public
getStructuredValues() : array<string|int, string>
Return values
array<string|int, string>getValues()
Returns the values for the choices.
public
getValues() : array<string|int, string>
Return values
array<string|int, string>getValuesForChoices()
Returns the values corresponding to the given choices.
public
getValuesForChoices(array<string|int, mixed> $choices) : array<string|int, string>
Parameters
- $choices : array<string|int, mixed>
-
An array of choices. Non-existing choices in this array are ignored
Return values
array<string|int, string>castableToString()
Checks whether the given choices can be cast to strings without generating duplicates.
private
castableToString(array<string|int, mixed> $choices[, array<string|int, mixed> &$cache = [] ]) : bool
This method is responsible for preventing conflict between scalar values and the empty value.
Parameters
- $choices : array<string|int, mixed>
- $cache : array<string|int, mixed> = []