diff options
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php new file mode 100644 index 00000000..914dbe5f --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/ChoiceList/SimpleChoiceList.php | |||
@@ -0,0 +1,164 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\Form\Extension\Core\ChoiceList; | ||
13 | |||
14 | /** | ||
15 | * A choice list for choices of type string or integer. | ||
16 | * | ||
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. | ||
22 | * | ||
23 | * <code> | ||
24 | * $choiceList = new SimpleChoiceList(array( | ||
25 | * 'creditcard' => 'Credit card payment', | ||
26 | * 'cash' => 'Cash payment', | ||
27 | * )); | ||
28 | * </code> | ||
29 | * | ||
30 | * @author Bernhard Schussek <bschussek@gmail.com> | ||
31 | */ | ||
32 | class SimpleChoiceList extends ChoiceList | ||
33 | { | ||
34 | /** | ||
35 | * Creates a new simple choice list. | ||
36 | * | ||
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. | ||
44 | */ | ||
45 | public function __construct(array $choices, array $preferredChoices = array()) | ||
46 | { | ||
47 | // Flip preferred choices to speed up lookup | ||
48 | parent::__construct($choices, $choices, array_flip($preferredChoices)); | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * {@inheritdoc} | ||
53 | */ | ||
54 | public function getChoicesForValues(array $values) | ||
55 | { | ||
56 | $values = $this->fixValues($values); | ||
57 | |||
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())); | ||
61 | } | ||
62 | |||
63 | /** | ||
64 | * {@inheritdoc} | ||
65 | */ | ||
66 | public function getValuesForChoices(array $choices) | ||
67 | { | ||
68 | $choices = $this->fixChoices($choices); | ||
69 | |||
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())); | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Recursively adds the given choices to the list. | ||
77 | * | ||
78 | * Takes care of splitting the single $choices array passed in the | ||
79 | * constructor into choices and labels. | ||
80 | * | ||
81 | * @param array $bucketForPreferred The bucket where to store the preferred | ||
82 | * view objects. | ||
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. | ||
88 | */ | ||
89 | protected function addChoices(array &$bucketForPreferred, array &$bucketForRemaining, $choices, array $labels, array $preferredChoices) | ||
90 | { | ||
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( | ||
97 | $choice, | ||
98 | $bucketForPreferred, | ||
99 | $bucketForRemaining, | ||
100 | $label, | ||
101 | $label, | ||
102 | $preferredChoices | ||
103 | ); | ||
104 | } | ||
105 | } else { | ||
106 | $this->addChoice( | ||
107 | $bucketForPreferred, | ||
108 | $bucketForRemaining, | ||
109 | $choice, | ||
110 | $label, | ||
111 | $preferredChoices | ||
112 | ); | ||
113 | } | ||
114 | } | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Returns whether the given choice should be preferred judging by the | ||
119 | * given array of preferred choices. | ||
120 | * | ||
121 | * Optimized for performance by treating the preferred choices as array | ||
122 | * where choices are stored in the keys. | ||
123 | * | ||
124 | * @param mixed $choice The choice to test. | ||
125 | * @param array $preferredChoices An array of preferred choices. | ||
126 | * | ||
127 | * @return Boolean Whether the choice is preferred. | ||
128 | */ | ||
129 | protected function isPreferred($choice, array $preferredChoices) | ||
130 | { | ||
131 | // Optimize performance over the default implementation | ||
132 | return isset($preferredChoices[$choice]); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Converts the choice to a valid PHP array key. | ||
137 | * | ||
138 | * @param mixed $choice The choice. | ||
139 | * | ||
140 | * @return string|integer A valid PHP array key. | ||
141 | */ | ||
142 | protected function fixChoice($choice) | ||
143 | { | ||
144 | return $this->fixIndex($choice); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * {@inheritdoc} | ||
149 | */ | ||
150 | protected function fixChoices(array $choices) | ||
151 | { | ||
152 | return $this->fixIndices($choices); | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * {@inheritdoc} | ||
157 | */ | ||
158 | protected function createValue($choice) | ||
159 | { | ||
160 | // Choices are guaranteed to be unique and scalar, so we can simply | ||
161 | // convert them to strings | ||
162 | return (string) $choice; | ||
163 | } | ||
164 | } | ||