/** * {@inheritdoc} */ public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null) { // Backwards compatibility if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices) && null === $label && null === $index && null === $groupBy && null === $attr) { $mapToNonLegacyChoiceView = function (LegacyChoiceView &$choiceView) { $choiceView = new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label); }; $adaptedList = $list->getAdaptedList(); $remainingViews = $adaptedList->getRemainingViews(); $preferredViews = $adaptedList->getPreferredViews(); array_walk_recursive($remainingViews, $mapToNonLegacyChoiceView); array_walk_recursive($preferredViews, $mapToNonLegacyChoiceView); return new ChoiceListView($remainingViews, $preferredViews); } $preferredViews = array(); $otherViews = array(); $choices = $list->getChoices(); $keys = $list->getOriginalKeys(); if (!is_callable($preferredChoices) && !empty($preferredChoices)) { $preferredChoices = function ($choice) use($preferredChoices) { return false !== array_search($choice, $preferredChoices, true); }; } // The names are generated from an incrementing integer by default if (null === $index) { $index = 0; } // If $groupBy is a callable, choices are added to the group with the // name returned by the callable. If the callable returns null, the // choice is not added to any group if (is_callable($groupBy)) { foreach ($choices as $value => $choice) { self::addChoiceViewGroupedBy($groupBy, $choice, (string) $value, $label, $keys, $index, $attr, $preferredChoices, $preferredViews, $otherViews); } } else { // Otherwise use the original structure of the choices self::addChoiceViewsGroupedBy($list->getStructuredValues(), $label, $choices, $keys, $index, $attr, $preferredChoices, $preferredViews, $otherViews); } // Remove any empty group view that may have been created by // addChoiceViewGroupedBy() foreach ($preferredViews as $key => $view) { if ($view instanceof ChoiceGroupView && 0 === count($view->choices)) { unset($preferredViews[$key]); } } foreach ($otherViews as $key => $view) { if ($view instanceof ChoiceGroupView && 0 === count($view->choices)) { unset($otherViews[$key]); } } return new ChoiceListView($otherViews, $preferredViews); }
/** * {@inheritdoc} */ public function mapFormsToData($radios, &$choice) { $choice = null; foreach ($radios as $radio) { if ($radio->getData()) { if ('placeholder' === $radio->getName()) { return; } $value = $radio->getConfig()->getOption('value'); $choice = current($this->choiceList->getChoicesForValues(array($value))); return; } } }
/** * {@inheritdoc} */ public function mapFormsToData($checkboxes, &$choices) { $values = array(); foreach ($checkboxes as $checkbox) { if ($checkbox->getData()) { // construct an array of choice values $values[] = $checkbox->getConfig()->getOption('value'); } } try { $choices = $this->choiceList->getChoicesForValues($values); } catch (\Exception $e) { throw new TransformationFailedException('Can not read the values from the choice list.', $e->getCode(), $e); } }
/** * {@inheritdoc} */ public function getValuesForChoices(array $choices) { if (!$this->loadedList) { return $this->loader->loadValuesForChoices($choices, $this->value); } return $this->loadedList->getValuesForChoices($choices); }
/** * {@inheritdoc} */ public function loadChoicesForValues(array $values, $value = null) { if (empty(array_filter($values))) { return []; } if (!$this->choiceList) { $this->loadChoiceList($value); } return $this->choiceList->getChoicesForValues($values); }
/** * {@inheritdoc} */ public function getValuesForChoices(array $choices) { if ($this->loaded) { // Check whether the loader has the same cache if ($this->loadedList !== $this->loader->loadChoiceList($this->value)) { @trigger_error(sprintf('Caching the choice list in %s is deprecated since 3.1 and will not happen in 4.0. Cache the list in the %s instead.', __CLASS__, ChoiceLoaderInterface::class), E_USER_DEPRECATED); } return $this->loadedList->getValuesForChoices($choices); } return $this->loader->loadValuesForChoices($choices, $this->value); }
private function assertObjectListWithCustomValues(ChoiceListInterface $list) { $this->assertSame(array('a', 'b', '1', '2'), $list->getValues()); $this->assertSame(array('a' => $this->obj1, 'b' => $this->obj2, 1 => $this->obj3, 2 => $this->obj4), $list->getChoices()); $this->assertSame(array('a' => 'A', 'b' => 'B', 1 => 'C', 2 => 'D'), $list->getOriginalKeys()); }
public function testGetValuesForChoicesEmpty() { $this->assertSame(array(), $this->list->getValuesForChoices(array())); }
private function handleChoiceListValues(ChoiceListInterface $choiceList) { $choices = array(); foreach (array($choiceList->getPreferredViews(), $choiceList->getRemainingViews()) as $viewList) { $choices = array_merge($choices, $this->handleChoiceViewsHierarchy($viewList)); } return $choices; }
public function testGetChoicesForValuesWithNull() { $values = $this->list->getValuesForChoices(array(null)); $this->assertNotEmpty($this->list->getChoicesForValues($values)); }
private function assertObjectListWithCustomValues(ChoiceListInterface $list) { $this->assertSame(array('A' => $this->obj1, 'B' => $this->obj2, 'C' => $this->obj3, 'D' => $this->obj4), $list->getChoices()); $this->assertSame(array('A' => 'a', 'B' => 'b', 'C' => '1', 'D' => '2'), $list->getValues()); }
protected function init(ChoiceListInterface $choiceList) { $this->choiceLoader->expects($this->any())->method('getSize')->will($this->returnValue(count($choiceList->getChoices()))); $this->choiceLoader->expects($this->any())->method('loadPaginatedChoiceList')->will($this->returnValue($choiceList)); }
/** * {@inheritdoc} */ public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null) { // Backwards compatibility if ($list instanceof LegacyChoiceListInterface && empty($preferredChoices) && null === $label && null === $index && null === $groupBy && null === $attr) { $mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) { return new ChoiceView($choiceView->label, $choiceView->value, $choiceView->data); }; return new ChoiceListView(array_map($mapToNonLegacyChoiceView, $list->getRemainingViews()), array_map($mapToNonLegacyChoiceView, $list->getPreferredViews())); } $preferredViews = array(); $otherViews = array(); $choices = $list->getChoices(); $values = $list->getValues(); if (!is_callable($preferredChoices) && !empty($preferredChoices)) { $preferredChoices = function ($choice) use($preferredChoices) { return false !== array_search($choice, $preferredChoices, true); }; } // The names are generated from an incrementing integer by default if (null === $index) { $index = 0; } // If $groupBy is not given, no grouping is done if (empty($groupBy)) { foreach ($choices as $key => $choice) { self::addChoiceView($choice, $key, $label, $values, $index, $attr, $preferredChoices, $preferredViews, $otherViews); } return new ChoiceListView($otherViews, $preferredViews); } // If $groupBy is a callable, choices are added to the group with the // name returned by the callable. If the callable returns null, the // choice is not added to any group if (is_callable($groupBy)) { foreach ($choices as $key => $choice) { self::addChoiceViewGroupedBy($groupBy, $choice, $key, $label, $values, $index, $attr, $preferredChoices, $preferredViews, $otherViews); } } else { // If $groupBy is passed as array, use that array as template for // constructing the groups self::addChoiceViewsGroupedBy($groupBy, $label, $choices, $values, $index, $attr, $preferredChoices, $preferredViews, $otherViews); } // Remove any empty group view that may have been created by // addChoiceViewGroupedBy() foreach ($preferredViews as $key => $view) { if ($view instanceof ChoiceGroupView && 0 === count($view->choices)) { unset($preferredViews[$key]); } } foreach ($otherViews as $key => $view) { if ($view instanceof ChoiceGroupView && 0 === count($view->choices)) { unset($otherViews[$key]); } } return new ChoiceListView($otherViews, $preferredViews); }