4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Core\ChoiceList
;
15 * A choice list for choices of type string or integer.
17 * Choices and their associated labels can be passed in a single array. Since
18 * choices are passed as array keys, only strings or integer choices are
19 * allowed. Choices may also be given as hierarchy of unlimited depth by
20 * creating nested arrays. The title of the sub-hierarchy can be stored in the
21 * array key pointing to the nested array.
24 * $choiceList = new SimpleChoiceList(array(
25 * 'creditcard' => 'Credit card payment',
26 * 'cash' => 'Cash payment',
30 * @author Bernhard Schussek <bschussek@gmail.com>
32 class SimpleChoiceList
extends ChoiceList
35 * Creates a new simple choice list.
37 * @param array $choices The array of choices with the choices as keys and
38 * the labels as values. Choices may also be given
39 * as hierarchy of unlimited depth by creating nested
40 * arrays. The title of the sub-hierarchy is stored
41 * in the array key pointing to the nested array.
42 * @param array $preferredChoices A flat array of choices that should be
43 * presented to the user with priority.
45 public function __construct(array $choices, array $preferredChoices = array())
47 // Flip preferred choices to speed up lookup
48 parent
::__construct($choices, $choices, array_flip($preferredChoices));
54 public function getChoicesForValues(array $values)
56 $values = $this->fixValues($values);
58 // The values are identical to the choices, so we can just return them
59 // to improve performance a little bit
60 return $this->fixChoices(array_intersect($values, $this->getValues()));
66 public function getValuesForChoices(array $choices)
68 $choices = $this->fixChoices($choices);
70 // The choices are identical to the values, so we can just return them
71 // to improve performance a little bit
72 return $this->fixValues(array_intersect($choices, $this->getValues()));
76 * Recursively adds the given choices to the list.
78 * Takes care of splitting the single $choices array passed in the
79 * constructor into choices and labels.
81 * @param array $bucketForPreferred The bucket where to store the preferred
83 * @param array $bucketForRemaining The bucket where to store the
84 * non-preferred view objects.
85 * @param array|\Traversable $choices The list of choices.
86 * @param array $labels Ignored.
87 * @param array $preferredChoices The preferred choices.
89 protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices)
91 // Add choices to the nested buckets
92 foreach ($choices as $choice => $label) {
93 if (is_array($label)) {
94 // Don't do the work if the array is empty
95 if (count($label) > 0) {
96 $this->addChoiceGroup(
118 * Returns whether the given choice should be preferred judging by the
119 * given array of preferred choices.
121 * Optimized for performance by treating the preferred choices as array
122 * where choices are stored in the keys.
124 * @param mixed $choice The choice to test.
125 * @param array $preferredChoices An array of preferred choices.
127 * @return Boolean Whether the choice is preferred.
129 protected function isPreferred($choice, array $preferredChoices)
131 // Optimize performance over the default implementation
132 return isset($preferredChoices[$choice]);
136 * Converts the choice to a valid PHP array key.
138 * @param mixed $choice The choice.
140 * @return string|integer A valid PHP array key.
142 protected function fixChoice($choice)
144 return $this->fixIndex($choice);
150 protected function fixChoices(array $choices)
152 return $this->fixIndices($choices);
158 protected function createValue($choice)
160 // Choices are guaranteed to be unique and scalar, so we can simply
161 // convert them to strings
162 return (string) $choice;