ChoiceListFactoryInterface
in
Creates {@link ChoiceListInterface} instances.
Tags
Table of Contents
- createListFromChoices() : ChoiceListInterface
- Creates a choice list for the given choices.
- createListFromLoader() : ChoiceListInterface
- Creates a choice list that is loaded with the given loader.
- createView() : ChoiceListView
- Creates a view for the given choice list.
Methods
createListFromChoices()
Creates a choice list for the given choices.
public
createListFromChoices(iteratable<string|int, mixed> $choices[, callable|null $value = null ]) : ChoiceListInterface
The choices should be passed in the values of the choices array.
Optionally, a callable can be passed for generating the choice values. The callable receives the choice as only argument. Null may be passed when the choice list contains the empty value.
Parameters
- $choices : iteratable<string|int, mixed>
-
The choices
- $value : callable|null = null
-
The callable generating the choice values
Return values
ChoiceListInterface —The choice list
createListFromLoader()
Creates a choice list that is loaded with the given loader.
public
createListFromLoader(ChoiceLoaderInterface $loader[, callable|null $value = null ]) : ChoiceListInterface
Optionally, a callable can be passed for generating the choice values. The callable receives the choice as only argument. Null may be passed when the choice list contains the empty value.
Parameters
- $loader : ChoiceLoaderInterface
- $value : callable|null = null
-
The callable generating the choice values
Return values
ChoiceListInterface —The choice list
createView()
Creates a view for the given choice list.
public
createView(ChoiceListInterface $list[, array<string|int, mixed>|callable|null $preferredChoices = null ][, callable|false|null $label = null ][, callable|null $index = null ][, callable|null $groupBy = null ][, array<string|int, mixed>|callable|null $attr = null ]) : ChoiceListView
Callables may be passed for all optional arguments. The callables receive the choice as first and the array key as the second argument.
- The callable for the label and the name should return the generated label/choice name.
- The callable for the preferred choices should return true or false, depending on whether the choice should be preferred or not.
- The callable for the grouping should return the group name or null if a choice should not be grouped.
- The callable for the attributes should return an array of HTML attributes that will be inserted in the tag of the choice.
If no callable is passed, the labels will be generated from the choice keys. The view indices will be generated using an incrementing integer by default.
The preferred choices can also be passed as array. Each choice that is contained in that array will be marked as preferred.
The attributes can be passed as multi-dimensional array. The keys should match the keys of the choices. The values should be arrays of HTML attributes that should be added to the respective choice.
Parameters
- $list : ChoiceListInterface
- $preferredChoices : array<string|int, mixed>|callable|null = null
-
The preferred choices
- $label : callable|false|null = null
-
The callable generating the choice labels; pass false to discard the label
- $index : callable|null = null
-
The callable generating the view indices
- $groupBy : callable|null = null
-
The callable generating the group names
- $attr : array<string|int, mixed>|callable|null = null
-
The callable generating the HTML attributes
Return values
ChoiceListView —The choice list view