Skip to content

Commit 9ca087d

Browse files
committed
bug #14887 [Form] Swap new ChoiceView constructor arguments to ease migrating from the deprecated one (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Swap new ChoiceView constructor arguments to ease migrating from the deprecated one | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14193 | License | MIT | Doc PR | - Commits ------- 909d2b9 [Form] Swap new ChoiceView constructor arguments to ease migrating from the deprecated one
2 parents 4449433 + 49283e0 commit 9ca087d

File tree

12 files changed

+88
-89
lines changed

12 files changed

+88
-89
lines changed

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
144144
if ($list instanceof LegacyChoiceListInterface && empty($preferredChoices)
145145
&& null === $label && null === $index && null === $groupBy && null === $attr) {
146146
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) {
147-
return new ChoiceView($choiceView->label, $choiceView->value, $choiceView->data);
147+
return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
148148
};
149149

150150
return new ChoiceListView(
@@ -245,10 +245,10 @@ private static function addChoiceView($choice, $key, $label, $values, &$index, $
245245
$nextIndex = is_int($index) ? $index++ : call_user_func($index, $choice, $key, $value);
246246

247247
$view = new ChoiceView(
248+
$choice,
249+
$value,
248250
// If the labels are null, use the choice key by default
249251
null === $label ? (string) $key : (string) call_user_func($label, $choice, $key, $value),
250-
$value,
251-
$choice,
252252
// The attributes may be a callable or a mapping from choice indices
253253
// to nested arrays
254254
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())

ChoiceList/View/ChoiceView.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ class ChoiceView extends LegacyChoiceView
7878
/**
7979
* Creates a new choice view.
8080
*
81-
* @param string $label The label displayed to humans
82-
* @param string $value The view representation of the choice
8381
* @param mixed $data The original choice
82+
* @param string $value The view representation of the choice
83+
* @param string $label The label displayed to humans
8484
* @param array $attr Additional attributes for the HTML tag
8585
*/
86-
public function __construct($label, $value, $data, array $attr = array())
86+
public function __construct($data, $value, $label, array $attr = array())
8787
{
8888
parent::__construct($data, $value, $label);
8989

Extension/Core/Type/ChoiceType.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
16-
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
1716
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
1817
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
1918
use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
@@ -71,7 +70,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7170
// Check if the choices already contain the empty value
7271
// Only add the placeholder option if this is not the case
7372
if (null !== $options['placeholder'] && 0 === count($options['choice_list']->getChoicesForValues(array('')))) {
74-
$placeholderView = new ChoiceView($options['placeholder'], '', null);
73+
$placeholderView = new ChoiceView(null, '', $options['placeholder']);
7574

7675
// "placeholder" is a reserved name
7776
$this->addSubForm($builder, 'placeholder', $placeholderView, $options);
@@ -436,7 +435,7 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
436435
// information from the "choices" option for creating groups
437436
if (!$options['group_by'] && $options['choices']) {
438437
$options['group_by'] = !$options['choices_as_values']
439-
? ChoiceType::flipRecursive($options['choices'])
438+
? self::flipRecursive($options['choices'])
440439
: $options['choices'];
441440
}
442441

Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,10 @@ public function testCreateViewFlat()
320320

321321
$this->assertEquals(new ChoiceListView(
322322
array(
323-
0 => new ChoiceView('A', '0', $this->obj1),
324-
1 => new ChoiceView('B', '1', $this->obj2),
325-
2 => new ChoiceView('C', '2', $this->obj3),
326-
3 => new ChoiceView('D', '3', $this->obj4),
323+
0 => new ChoiceView($this->obj1, '0', 'A'),
324+
1 => new ChoiceView($this->obj2, '1', 'B'),
325+
2 => new ChoiceView($this->obj3, '2', 'C'),
326+
3 => new ChoiceView($this->obj4, '3', 'D'),
327327
), array()
328328
), $view);
329329
}
@@ -347,10 +347,10 @@ public function testCreateViewFlatPreferredChoicesEmptyArray()
347347

348348
$this->assertEquals(new ChoiceListView(
349349
array(
350-
0 => new ChoiceView('A', '0', $this->obj1),
351-
1 => new ChoiceView('B', '1', $this->obj2),
352-
2 => new ChoiceView('C', '2', $this->obj3),
353-
3 => new ChoiceView('D', '3', $this->obj4),
350+
0 => new ChoiceView($this->obj1, '0', 'A'),
351+
1 => new ChoiceView($this->obj2, '1', 'B'),
352+
2 => new ChoiceView($this->obj3, '2', 'C'),
353+
3 => new ChoiceView($this->obj4, '3', 'D'),
354354
), array()
355355
), $view);
356356
}
@@ -751,8 +751,8 @@ public function testCreateViewForLegacyChoiceList()
751751

752752
$view = $this->factory->createView($list);
753753

754-
$this->assertEquals(array(new ChoiceView('Other', 'y', 'y')), $view->choices);
755-
$this->assertEquals(array(new ChoiceView('Preferred', 'x', 'x')), $view->preferredChoices);
754+
$this->assertEquals(array(new ChoiceView('y', 'y', 'Other')), $view->choices);
755+
$this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices);
756756
}
757757

758758
private function assertScalarListWithGeneratedValues(ChoiceListInterface $list)
@@ -827,11 +827,11 @@ private function assertFlatView($view)
827827
{
828828
$this->assertEquals(new ChoiceListView(
829829
array(
830-
0 => new ChoiceView('A', '0', $this->obj1),
831-
3 => new ChoiceView('D', '3', $this->obj4),
830+
0 => new ChoiceView($this->obj1, '0', 'A'),
831+
3 => new ChoiceView($this->obj4, '3', 'D'),
832832
), array(
833-
1 => new ChoiceView('B', '1', $this->obj2),
834-
2 => new ChoiceView('C', '2', $this->obj3),
833+
1 => new ChoiceView($this->obj2, '1', 'B'),
834+
2 => new ChoiceView($this->obj3, '2', 'C'),
835835
)
836836
), $view);
837837
}
@@ -840,11 +840,11 @@ private function assertFlatViewWithCustomIndices($view)
840840
{
841841
$this->assertEquals(new ChoiceListView(
842842
array(
843-
'w' => new ChoiceView('A', '0', $this->obj1),
844-
'z' => new ChoiceView('D', '3', $this->obj4),
843+
'w' => new ChoiceView($this->obj1, '0', 'A'),
844+
'z' => new ChoiceView($this->obj4, '3', 'D'),
845845
), array(
846-
'x' => new ChoiceView('B', '1', $this->obj2),
847-
'y' => new ChoiceView('C', '2', $this->obj3),
846+
'x' => new ChoiceView($this->obj2, '1', 'B'),
847+
'y' => new ChoiceView($this->obj3, '2', 'C'),
848848
)
849849
), $view);
850850
}
@@ -853,19 +853,19 @@ private function assertFlatViewWithAttr($view)
853853
{
854854
$this->assertEquals(new ChoiceListView(
855855
array(
856-
0 => new ChoiceView('A', '0', $this->obj1),
857-
3 => new ChoiceView('D', '3', $this->obj4),
856+
0 => new ChoiceView($this->obj1, '0', 'A'),
857+
3 => new ChoiceView($this->obj4, '3', 'D'),
858858
), array(
859859
1 => new ChoiceView(
860-
'B',
861-
'1',
862860
$this->obj2,
861+
'1',
862+
'B',
863863
array('attr1' => 'value1')
864864
),
865865
2 => new ChoiceView(
866-
'C',
867-
'2',
868866
$this->obj3,
867+
'2',
868+
'C',
869869
array('attr2' => 'value2')
870870
),
871871
)
@@ -878,20 +878,20 @@ private function assertGroupedView($view)
878878
array(
879879
'Group 1' => new ChoiceGroupView(
880880
'Group 1',
881-
array(0 => new ChoiceView('A', '0', $this->obj1))
881+
array(0 => new ChoiceView($this->obj1, '0', 'A'))
882882
),
883883
'Group 2' => new ChoiceGroupView(
884884
'Group 2',
885-
array(3 => new ChoiceView('D', '3', $this->obj4))
885+
array(3 => new ChoiceView($this->obj4, '3', 'D'))
886886
),
887887
), array(
888888
'Group 1' => new ChoiceGroupView(
889889
'Group 1',
890-
array(1 => new ChoiceView('B', '1', $this->obj2))
890+
array(1 => new ChoiceView($this->obj2, '1', 'B'))
891891
),
892892
'Group 2' => new ChoiceGroupView(
893893
'Group 2',
894-
array(2 => new ChoiceView('C', '2', $this->obj3))
894+
array(2 => new ChoiceView($this->obj3, '2', 'C'))
895895
),
896896
)
897897
), $view);

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,10 +1556,10 @@ public function testPassChoicesToView()
15561556
$view = $form->createView();
15571557

15581558
$this->assertEquals(array(
1559-
new ChoiceView('A', 'a', 'a'),
1560-
new ChoiceView('B', 'b', 'b'),
1561-
new ChoiceView('C', 'c', 'c'),
1562-
new ChoiceView('D', 'd', 'd'),
1559+
new ChoiceView('a', 'a', 'A'),
1560+
new ChoiceView('b', 'b', 'B'),
1561+
new ChoiceView('c', 'c', 'C'),
1562+
new ChoiceView('d', 'd', 'D'),
15631563
), $view->vars['choices']);
15641564
}
15651565

@@ -1573,12 +1573,12 @@ public function testPassPreferredChoicesToView()
15731573
$view = $form->createView();
15741574

15751575
$this->assertEquals(array(
1576-
0 => new ChoiceView('A', 'a', 'a'),
1577-
2 => new ChoiceView('C', 'c', 'c'),
1576+
0 => new ChoiceView('a', 'a', 'A'),
1577+
2 => new ChoiceView('c', 'c', 'C'),
15781578
), $view->vars['choices']);
15791579
$this->assertEquals(array(
1580-
1 => new ChoiceView('B', 'b', 'b'),
1581-
3 => new ChoiceView('D', 'd', 'd'),
1580+
1 => new ChoiceView('b', 'b', 'B'),
1581+
3 => new ChoiceView('d', 'd', 'D'),
15821582
), $view->vars['preferred_choices']);
15831583
}
15841584

@@ -1592,19 +1592,19 @@ public function testPassHierarchicalChoicesToView()
15921592

15931593
$this->assertEquals(array(
15941594
'Symfony' => new ChoiceGroupView('Symfony', array(
1595-
0 => new ChoiceView('Bernhard', 'a', 'a'),
1596-
2 => new ChoiceView('Kris', 'c', 'c'),
1595+
0 => new ChoiceView('a', 'a', 'Bernhard'),
1596+
2 => new ChoiceView('c', 'c', 'Kris'),
15971597
)),
15981598
'Doctrine' => new ChoiceGroupView('Doctrine', array(
1599-
4 => new ChoiceView('Roman', 'e', 'e'),
1599+
4 => new ChoiceView('e', 'e', 'Roman'),
16001600
)),
16011601
), $view->vars['choices']);
16021602
$this->assertEquals(array(
16031603
'Symfony' => new ChoiceGroupView('Symfony', array(
1604-
1 => new ChoiceView('Fabien', 'b', 'b'),
1604+
1 => new ChoiceView('b', 'b', 'Fabien'),
16051605
)),
16061606
'Doctrine' => new ChoiceGroupView('Doctrine', array(
1607-
3 => new ChoiceView('Jon', 'd', 'd'),
1607+
3 => new ChoiceView('d', 'd', 'Jon'),
16081608
)),
16091609
), $view->vars['preferred_choices']);
16101610
}
@@ -1624,10 +1624,10 @@ public function testPassChoiceDataToView()
16241624
$view = $form->createView();
16251625

16261626
$this->assertEquals(array(
1627-
new ChoiceView('A', 'a', $obj1),
1628-
new ChoiceView('B', 'b', $obj2),
1629-
new ChoiceView('C', 'c', $obj3),
1630-
new ChoiceView('D', 'd', $obj4),
1627+
new ChoiceView($obj1, 'a', 'A'),
1628+
new ChoiceView($obj2, 'b', 'B'),
1629+
new ChoiceView($obj3, 'c', 'C'),
1630+
new ChoiceView($obj4, 'd', 'D'),
16311631
), $view->vars['choices']);
16321632
}
16331633

Tests/Extension/Core/Type/CountryTypeTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function testCountriesAreSelectable()
3131
$choices = $view->vars['choices'];
3232

3333
// Don't check objects for identity
34-
$this->assertContains(new ChoiceView('Germany', 'DE', 'DE'), $choices, '', false, false);
35-
$this->assertContains(new ChoiceView('United Kingdom', 'GB', 'GB'), $choices, '', false, false);
36-
$this->assertContains(new ChoiceView('United States', 'US', 'US'), $choices, '', false, false);
37-
$this->assertContains(new ChoiceView('France', 'FR', 'FR'), $choices, '', false, false);
38-
$this->assertContains(new ChoiceView('Malaysia', 'MY', 'MY'), $choices, '', false, false);
34+
$this->assertContains(new ChoiceView('DE', 'DE', 'Germany'), $choices, '', false, false);
35+
$this->assertContains(new ChoiceView('GB', 'GB', 'United Kingdom'), $choices, '', false, false);
36+
$this->assertContains(new ChoiceView('US', 'US', 'United States'), $choices, '', false, false);
37+
$this->assertContains(new ChoiceView('FR', 'FR', 'France'), $choices, '', false, false);
38+
$this->assertContains(new ChoiceView('MY', 'MY', 'Malaysia'), $choices, '', false, false);
3939
}
4040

4141
public function testUnknownCountryIsNotIncluded()

Tests/Extension/Core/Type/CurrencyTypeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function testCurrenciesAreSelectable()
3030
$view = $form->createView();
3131
$choices = $view->vars['choices'];
3232

33-
$this->assertContains(new ChoiceView('Euro', 'EUR', 'EUR'), $choices, '', false, false);
34-
$this->assertContains(new ChoiceView('US Dollar', 'USD', 'USD'), $choices, '', false, false);
35-
$this->assertContains(new ChoiceView('Slovenian Tolar', 'SIT', 'SIT'), $choices, '', false, false);
33+
$this->assertContains(new ChoiceView('EUR', 'EUR', 'Euro'), $choices, '', false, false);
34+
$this->assertContains(new ChoiceView('USD', 'USD', 'US Dollar'), $choices, '', false, false);
35+
$this->assertContains(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar'), $choices, '', false, false);
3636
}
3737
}

Tests/Extension/Core/Type/DateTypeTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,8 @@ public function testMonthsOption()
440440
$view = $form->createView();
441441

442442
$this->assertEquals(array(
443-
new ChoiceView('06', '6', '6'),
444-
new ChoiceView('07', '7', '7'),
443+
new ChoiceView('6', '6', '06'),
444+
new ChoiceView('7', '7', '07'),
445445
), $view['month']->vars['choices']);
446446
}
447447

@@ -455,8 +455,8 @@ public function testMonthsOptionShortFormat()
455455
$view = $form->createView();
456456

457457
$this->assertEquals(array(
458-
new ChoiceView('Jän', '1', '1'),
459-
new ChoiceView('Apr.', '4', '4'),
458+
new ChoiceView('1', '1', 'Jän'),
459+
new ChoiceView('4', '4', 'Apr.'),
460460
), $view['month']->vars['choices']);
461461
}
462462

@@ -470,8 +470,8 @@ public function testMonthsOptionLongFormat()
470470
$view = $form->createView();
471471

472472
$this->assertEquals(array(
473-
new ChoiceView('Jänner', '1', '1'),
474-
new ChoiceView('April', '4', '4'),
473+
new ChoiceView('1', '1', 'Jänner'),
474+
new ChoiceView('4', '4', 'April'),
475475
), $view['month']->vars['choices']);
476476
}
477477

@@ -485,8 +485,8 @@ public function testMonthsOptionLongFormatWithDifferentTimezone()
485485
$view = $form->createView();
486486

487487
$this->assertEquals(array(
488-
new ChoiceView('Jänner', '1', '1'),
489-
new ChoiceView('April', '4', '4'),
488+
new ChoiceView('1', '1', 'Jänner'),
489+
new ChoiceView('4', '4', 'April'),
490490
), $view['month']->vars['choices']);
491491
}
492492

@@ -499,8 +499,8 @@ public function testIsDayWithinRangeReturnsTrueIfWithin()
499499
$view = $form->createView();
500500

501501
$this->assertEquals(array(
502-
new ChoiceView('06', '6', '6'),
503-
new ChoiceView('07', '7', '7'),
502+
new ChoiceView('6', '6', '06'),
503+
new ChoiceView('7', '7', '07'),
504504
), $view['day']->vars['choices']);
505505
}
506506

Tests/Extension/Core/Type/LanguageTypeTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ public function testCountriesAreSelectable()
3030
$view = $form->createView();
3131
$choices = $view->vars['choices'];
3232

33-
$this->assertContains(new ChoiceView('English', 'en', 'en'), $choices, '', false, false);
34-
$this->assertContains(new ChoiceView('British English', 'en_GB', 'en_GB'), $choices, '', false, false);
35-
$this->assertContains(new ChoiceView('American English', 'en_US', 'en_US'), $choices, '', false, false);
36-
$this->assertContains(new ChoiceView('French', 'fr', 'fr'), $choices, '', false, false);
37-
$this->assertContains(new ChoiceView('Burmese', 'my', 'my'), $choices, '', false, false);
33+
$this->assertContains(new ChoiceView('en', 'en', 'English'), $choices, '', false, false);
34+
$this->assertContains(new ChoiceView('en_GB', 'en_GB', 'British English'), $choices, '', false, false);
35+
$this->assertContains(new ChoiceView('en_US', 'en_US', 'American English'), $choices, '', false, false);
36+
$this->assertContains(new ChoiceView('fr', 'fr', 'French'), $choices, '', false, false);
37+
$this->assertContains(new ChoiceView('my', 'my', 'Burmese'), $choices, '', false, false);
3838
}
3939

4040
public function testMultipleLanguagesIsNotIncluded()
@@ -43,6 +43,6 @@ public function testMultipleLanguagesIsNotIncluded()
4343
$view = $form->createView();
4444
$choices = $view->vars['choices'];
4545

46-
$this->assertNotContains(new ChoiceView('Mehrsprachig', 'mul', 'mul'), $choices, '', false, false);
46+
$this->assertNotContains(new ChoiceView('mul', 'mul', 'Mehrsprachig'), $choices, '', false, false);
4747
}
4848
}

Tests/Extension/Core/Type/LocaleTypeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function testLocalesAreSelectable()
3030
$view = $form->createView();
3131
$choices = $view->vars['choices'];
3232

33-
$this->assertContains(new ChoiceView('English', 'en', 'en'), $choices, '', false, false);
34-
$this->assertContains(new ChoiceView('English (United Kingdom)', 'en_GB', 'en_GB'), $choices, '', false, false);
35-
$this->assertContains(new ChoiceView('Chinese (Traditional, Macau SAR China)', 'zh_Hant_MO', 'zh_Hant_MO'), $choices, '', false, false);
33+
$this->assertContains(new ChoiceView('en', 'en', 'English'), $choices, '', false, false);
34+
$this->assertContains(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)'), $choices, '', false, false);
35+
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
3636
}
3737
}

0 commit comments

Comments
 (0)