W3cubDocs

/Symfony 2.8

Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator

class PropertyAccessDecorator implements ChoiceListFactoryInterface

Adds property path support to a choice list factory.

Pass the decorated factory to the constructor:

php $decorator = new PropertyAccessDecorator($factory);

You can now pass property paths for generating choice values, labels, view indices, HTML attributes and for determining the preferred choices and the choice groups:

php // extract values from the $value property $list = $createListFromChoices($objects, 'value');

Methods

__construct(ChoiceListFactoryInterface $decoratedFactory, PropertyAccessorInterface $propertyAccessor = null)

Decorates the given factory.

ChoiceListFactoryInterface getDecoratedFactory()

Returns the decorated factory.

ChoiceListInterface createListFromChoices(array|Traversable $choices, null|callable|string|PropertyPath $value = null)

Creates a choice list for the given choices.

ChoiceListInterface createListFromFlippedChoices(array|Traversable $choices, null|callable $value = null, $triggerDeprecationNotice = true)

Creates a choice list for the given choices.

ChoiceListInterface createListFromLoader(ChoiceLoaderInterface $loader, null|callable|string|PropertyPath $value = null)

Creates a choice list that is loaded with the given loader.

ChoiceListView createView(ChoiceListInterface $list, null|array|callable|string|PropertyPath $preferredChoices = null, null|callable|string|PropertyPath $label = null, null|callable|string|PropertyPath $index = null, null|callable|string|PropertyPath $groupBy = null, null|array|callable|string|PropertyPath $attr = null)

Creates a view for the given choice list.

Details

public __construct(ChoiceListFactoryInterface $decoratedFactory, PropertyAccessorInterface $propertyAccessor = null)

Decorates the given factory.

Parameters

ChoiceListFactoryInterface $decoratedFactory The decorated factory
PropertyAccessorInterface $propertyAccessor The used property accessor

public ChoiceListFactoryInterface getDecoratedFactory()

Returns the decorated factory.

Return Value

ChoiceListFactoryInterface The decorated factory

public ChoiceListInterface createListFromChoices(array|Traversable $choices, null|callable|string|PropertyPath $value = null)

Creates a choice list for the given choices.

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 first and the array key as the second argument.

Parameters

array|Traversable $choices The choices
null|callable|string|PropertyPath $value The callable or path for generating the choice values

Return Value

ChoiceListInterface The choice list

public ChoiceListInterface createListFromFlippedChoices(array|Traversable $choices, null|callable $value = null, $triggerDeprecationNotice = true)

Creates a choice list for the given choices.

The choices should be passed in the keys of the choices array. Since the choices array will be flipped, the entries of the array must be strings or integers.

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

array|Traversable $choices The choices
null|callable $value The callable generating the choice values
$triggerDeprecationNotice

Return Value

ChoiceListInterface The choice list

public ChoiceListInterface createListFromLoader(ChoiceLoaderInterface $loader, null|callable|string|PropertyPath $value = null)

Creates a choice list that is loaded with 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
null|callable|string|PropertyPath $value The callable or path for generating the choice values

Return Value

ChoiceListInterface The choice list

public ChoiceListView createView(ChoiceListInterface $list, null|array|callable|string|PropertyPath $preferredChoices = null, null|callable|string|PropertyPath $label = null, null|callable|string|PropertyPath $index = null, null|callable|string|PropertyPath $groupBy = null, null|array|callable|string|PropertyPath $attr = null)

Creates a view for the given choice list.

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

ChoiceListInterface $list The choice list
null|array|callable|string|PropertyPath $preferredChoices The preferred choices
null|callable|string|PropertyPath $label The callable or path generating the choice labels
null|callable|string|PropertyPath $index The callable or path generating the view indices
null|callable|string|PropertyPath $groupBy The callable or path generating the group names
null|array|callable|string|PropertyPath $attr The callable or path generating the HTML attributes

Return Value

ChoiceListView The choice list view

© 2004–2016 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/2.8/Symfony/Component/Form/ChoiceList/Factory/PropertyAccessDecorator.html