10000 [Form] Fixed ChoiceList::get*By*() methods to preserve order and arra… · symfony/symfony@79a214f · GitHub
[go: up one dir, main page]

Skip to content

Commit 79a214f

Browse files
committed
[Form] Fixed ChoiceList::get*By*() methods to preserve order and array keys
1 parent 62fbed6 commit 79a214f

File tree

4 files changed

+123
-17
lines changed

4 files changed

+123
-17
lines changed

src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ public function getChoicesForValues(array $values)
156156
$values = $this->fixValues($values);
157157
$choices = array();
158158

159-
foreach ($values as $j => $givenValue) {
160-
foreach ($this->values as $i => $value) {
159+
foreach ($values as $i => $givenValue) {
160+
foreach ($this->values as $j => $value) {
161161
if ($value === $givenValue) {
162-
$choices[] = $this->choices[$i];
163-
unset($values[$j]);
162+
$choices[$i] = $this->choices[$j];
163+
unset($values[$i]);
164164

165165
if (0 === count($values)) {
166166
break 2;
@@ -180,11 +180,11 @@ public function getValuesForChoices(array $choices)
180180
$choices = $this->fixChoices($choices);
181181
$values = array();
182182

183-
foreach ($this->choices as $i => $choice) {
184-
foreach ($choices as $j => $givenChoice) {
183+
foreach ($choices as $i => $givenChoice) {
184+
foreach ($this->choices as $j => $choice) {
185185
if ($choice === $givenChoice) {
186-
$values[] = $this->values[$i];
187-
unset($choices[$j]);
186+
$values[$i] = $this->values[$j];
187+
unset($choices[$i]);
188188

189189
if (0 === count($choices)) {
190190
break 2;
@@ -204,11 +204,11 @@ public function getIndicesForChoices(array $choices)
204204
$choices = $this->fixChoices($choices);
205205
$indices = array();
206206

207-
foreach ($this->choices as $i => $choice) {
208-
foreach ($choices as $j => $givenChoice) {
207+
foreach ($choices as $i => $givenChoice) {
208+
foreach ($this->choices as $j => $choice) {
209209
if ($choice === $givenChoice) {
210-
$indices[] = $i;
211-
unset($choices[$j]);
210+
$indices[$i] = $j;
211+
unset($choices[$i]);
212212

213213
if (0 === count($choices)) {
214214
break 2;
@@ -228,11 +228,11 @@ public function getIndicesForValues(array $values)
228228
$values = $this->fixValues($values);
229229
$indices = array();
230230

231-
foreach ($this->values as $i => $value) {
232-
foreach ($values as $j => $givenValue) {
231+
foreach ($values as $i => $givenValue) {
232+
foreach ($this->values as $j => $value) {
233233
if ($value === $givenValue) {
234-
$indices[] = $i;
235-
unset($values[$j]);
234+
$indices[$i] = $j;
235+
unset($values[$i]);
236236

237237
if (0 === count($values)) {
238238
break 2;

src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceListInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function getRemainingViews();
9797
*
9898
* The choices can have any data type.
9999
*
100+
* The choices must be returned with the same keys and in the same order
101+
* as the corresponding values in the given array.
102+
*
100103
* @param array $values An array of choice values. Not existing values in
101104
* this array are ignored
102105
*
@@ -109,6 +112,9 @@ public function getChoicesForValues(array $values);
109112
*
110113
* The values must be strings.
111114
*
115+
* The values must be returned with the same keys and in the same order
116+
* as the corresponding choices in the given array.
117+
*
112118
* @param array $choices An array of choices. Not existing choices in this
113119
* array are ignored
114120
*
@@ -125,6 +131,9 @@ public function getValuesForChoices(array $choices);
125131
*
126132
* The index "placeholder" is internally reserved.
127133
*
134+
* The indices must be returned with the same keys and in the same order
135+
* as the corresponding choices in the given array.
136+
*
128137
* @param array $choices An array of choices. Not existing choices in this
129138
* array are ignored
130139
*
@@ -140,6 +149,9 @@ public function getIndicesForChoices(array $choices);
140149
*
141150
* The index "placeholder" is internally reserved.
142151
*
152+
* The indices must be returned with the same keys and in the same order
153+
* as the corresponding values in the given array.
154+
*
143155
* @param array $values An array of choice values. Not existing values in
144156
* this array are ignored
145157
*

src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ChoiceListTest.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,18 @@ public function testGetIndicesForChoices()
125125
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
126126
}
127127

128+
public function testGetIndicesForChoicesPreservesKeys()
129+
{
130+
$choices = array(5 => $this->obj2, 8 => $this->obj3);
131+
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices));
132+
}
133+
134+
public function testGetIndicesForChoicesPreservesOrder()
135+
{
136+
$choices = array($this->obj3, $this->obj2);
137+
$this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices));
138+
}
139+
128140
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
129141
{
130142
$choices = array($this->obj2, $this->obj3, 'foobar');
@@ -138,6 +150,20 @@ public function testGetIndicesForValues()
138150
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
139151
}
140152

153+
public function testGetIndicesForValuesPreservesKeys()
154+
{
155+
// values and indices are always the same
156+
$values = array(5 => '1', 8 => '2');
157+
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values));
158+
}
159+
160+
public function testGetIndicesForValuesPreservesOrder()
161+
{
162+
// values and indices are always the same
163+
$values = array('2', '1');
164+
$this->assertSame(array(2, 1), $this->list->getIndicesForValues($values));
165+
}
166+
141167
public function testGetIndicesForValuesIgnoresNonExistingValues()
142168
{
143169
$values = array('1', '2', '5');
@@ -150,7 +176,13 @@ public function testGetChoicesForValues()
150176
$this->assertSame(array($this->obj2, $this->obj3), $this->list->getChoicesForValues($values));
151177
}
152178

153-
public function testGetChoicesForValuesCorrectOrderingOfResult()
179+
public function testGetChoicesForValuesPreservesKeys()
180+
{
181+
$values = array(5 => '1', 8 => '2');
182+
$this->assertSame(array(5 => $this->obj2, 8 => $this->obj3), $this->list->getChoicesForValues($values));
183+
}
184+
185+
public function testGetChoicesForValuesPreservesOrder()
154186
{
155187
$values = array('2', '1');
156188
$this->assertSame(array($this->obj3, $this->obj2), $this->list->getChoicesForValues($values));
@@ -168,6 +200,20 @@ public function testGetValuesForChoices()
168200
$this->assertSame(array('1', '2'), $this->list->getValuesForChoices($choices));
169201
}
170202

203+
204+
public function testGetValuesForChoicesPreservesKeys()
205+
{
206+
$choices = array(5 => $this->obj2, 8 => $this->obj3);
207+
$this->assertSame(array(5 => '1', 8 => '2'), $this->list->getValuesForChoices($choices));
208+
}
209+
210+
211+
public function testGetValuesForChoicesPreservesOrder()
212+
{
213+
$choices = array($this->obj3, $this->obj2);
214+
$this->assertSame(array('2', '1'), $this->list->getValuesForChoices($choices));
215+
}
216+
171217
public function testGetValuesForChoicesIgnoresNonExistingChoices()
172218
{
173219
$choices = array($this->obj2, $this->obj3, 'foobar');

src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/SimpleChoiceListTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ public function testGetIndicesForChoices()
7979
$this->assertSame(array(1, 2), $this->list->getIndicesForChoices($choices));
8080
}
8181

82+
public function testGetIndicesForChoicesPreservesKeys()
83+
{
84+
$choices = array(5 => 'b', 8 => 'c');
85+
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForChoices($choices));
86+
}
87+
88+
public function testGetIndicesForChoicesPreservesOrder()
89+
{
90+
$choices = array('c', 'b');
91+
$this->assertSame(array(2, 1), $this->list->getIndicesForChoices($choices));
92+
}
93+
8294
public function testGetIndicesForChoicesIgnoresNonExistingChoices()
8395
{
8496
$choices = array('b', 'c', 'foobar');
@@ -98,6 +110,18 @@ public function testGetIndicesForValues()
98110
$this->assertSame(array(1, 2), $this->list->getIndicesForValues($values));
99111
}
100112

113+
public function testGetIndicesForValuesPreservesKeys()
114+
{
115+
$values = array(5 => 'b', 8 => 'c');
116+
$this->assertSame(array(5 => 1, 8 => 2), $this->list->getIndicesForValues($values));
117+
}
118+
119+
public function testGetIndicesForValuesPreservesOrder()
120+
{
121+
$values = array('c', 'b');
122+
$this->assertSame(array(2, 1), $this->list->getIndicesForValues($values));
123+
}
124+
101125
public function testGetIndicesForValuesIgnoresNonExistingValues()
102126
{
103127
$values = array('b', 'c', '100');
@@ -117,6 +141,18 @@ public function testGetChoicesForValues()
117141
$this->assertSame(array('b', 'c'), $this->list->getChoicesForValues($values));
118142
}
119143

144+
public function testGetChoicesForValuesPreservesKeys()
145+
{
146+
$values = array(5 => 'b', 8 => 'c');
147+
$this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getChoicesForValues($values));
148+
}
149+
150+
public function testGetChoicesForValuesPreservesOrder()
151+
{
152+
$values = array('c', 'b');
153+
$this->assertSame(array('c', 'b'), $this->list->getChoicesForValues($values));
154+
}
155+
120156
public function testGetChoicesForValuesIgnoresNonExistingValues()
121157
{
122158
$values = array('b', 'c', '100');
@@ -136,6 +172,18 @@ public function testGetValuesForChoices()
136172
$this->assertSame(array('b', 'c'), $this->list->getValuesForChoices($choices));
137173
}
138174

175+
public function testGetValuesForChoicesPreservesKeys()
176+
{
177+
$choices = array(5 => 'b', 8 => 'c');
178+
$this->assertSame(array(5 => 'b', 8 => 'c'), $this->list->getValuesForChoices($choices));
179+
}
180+
181+
public function testGetValuesForChoicesPreservesOrder()
182+
{
183+
$choices = array('c', 'b');
184+
$this->assertSame(array('c', 'b'), $this->list->getValuesForChoices($choices));
185+
}
186+
139187
public function testGetValuesForChoicesIgnoresNonExistingValues()
140188
{
141189
$choices = array('b', 'c', 'foobar');

0 commit comments

Comments
 (0)
2A61
0