Skip to content

Commit 6d0bbf5

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: [Form] fixed BC-break on grouped choice lists [WebProfilerBundle] add import for Twig macro made Symfony compatible with both Twig 1.x and 2.x [Debug/VarDumper] minor cleanups [Form] only use PropertyPath if not already callable [Form] fix reworked choice list phpdoc [DoctrineBridge][Form] Add old tests to legacy group Fixed warning when command alias is longer than command name removed _self usage when not needed Implement the support of timezone objects in the stub IntlDateFormatter typofix - https://github.com/vlajos/misspell_fixer make doctrine mappings compiler pass exception message more understandable fix debug-ext 003.phpt [Yaml] Nested merge keys [FrameworkBundle] [Command] removed unused variable. [Debug] Enhance DebugClassLoader performance on MacOSX Add support for variadic arguments in the GetSetNormalizer [DoctrineBridge][Form] Fix IdReader when indexing by primary foreign key [DoctrineBridge][Form] Fix EntityChoiceList when indexing by primary foreign key
2 parents f45a402 + c0f0ad2 commit 6d0bbf5

File tree

3 files changed

+52
-20
lines changed

3 files changed

+52
-20
lines changed

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,18 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
6464
// Backwards compatibility
6565
if ($list instanceof LegacyChoiceListAdapter && empty($preferredChoices)
6666
&& null === $label && null === $index && null === $groupBy && null === $attr) {
67-
$mapToNonLegacyChoiceView = function (LegacyChoiceView $choiceView) {
68-
return new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
67+
$mapToNonLegacyChoiceView = function (LegacyChoiceView &$choiceView) {
68+
$choiceView = new ChoiceView($choiceView->data, $choiceView->value, $choiceView->label);
6969
};
7070

7171
$adaptedList = $list->getAdaptedList();
7272

73-
return new ChoiceListView(
74-
array_map($mapToNonLegacyChoiceView, $adaptedList->getRemainingViews()),
75-
array_map($mapToNonLegacyChoiceView, $adaptedList->getPreferredViews())
76-
);
73+
$remainingViews = $adaptedList->getRemainingViews();
74+
$preferredViews = $adaptedList->getPreferredViews();
75+
array_walk_recursive($remainingViews, $mapToNonLegacyChoiceView);
76+
array_walk_recursive($preferredViews, $mapToNonLegacyChoiceView);
77+
78+
return new ChoiceListView($remainingViews, $preferredViews);
7779
}
7880

7981
$preferredViews = array();

ChoiceList/Factory/PropertyAccessDecorator.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getDecoratedFactory()
8484
*/
8585
public function createListFromChoices($choices, $value = null)
8686
{
87-
if (is_string($value)) {
87+
if (is_string($value) && !is_callable($value)) {
8888
$value = new PropertyPath($value);
8989
}
9090

@@ -134,7 +134,7 @@ public function createListFromFlippedChoices($choices, $value = null)
134134
*/
135135
public function createListFromLoader(ChoiceLoaderInterface $loader, $value = null)
136136
{
137-
if (is_string($value)) {
137+
if (is_string($value) && !is_callable($value)) {
138138
$value = new PropertyPath($value);
139139
}
140140

@@ -151,20 +151,20 @@ public function createListFromLoader(ChoiceLoaderInterface $loader, $value = nul
151151
/**
152152
* {@inheritdoc}
153153
*
154-
* @param ChoiceListInterface $list The choice list
155-
* @param null|array|callable|string|PropertyPath $preferredChoices The preferred choices
156-
* @param null|callable|string|PropertyPath $label The callable or path generating the choice labels
157-
* @param null|callable|string|PropertyPath $index The callable or path generating the view indices
158-
* @param null|array|\Traversable|callable|string|PropertyPath $groupBy The callable or path generating the group names
159-
* @param null|array|callable|string|PropertyPath $attr The callable or path generating the HTML attributes
154+
* @param ChoiceListInterface $list The choice list
155+
* @param null|array|callable|string|PropertyPath $preferredChoices The preferred choices
156+
* @param null|callable|string|PropertyPath $label The callable or path generating the choice labels
157+
* @param null|callable|string|PropertyPath $index The callable or path generating the view indices
158+
* @param null|callable|string|PropertyPath $groupBy The callable or path generating the group names
159+
* @param null|array|callable|string|PropertyPath $attr The callable or path generating the HTML attributes
160160
*
161161
* @return ChoiceListView The choice list view
162162
*/
163163
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null)
164164
{
165165
$accessor = $this->propertyAccessor;
166166

167-
if (is_string($label)) {
167+
if (is_string($label) && !is_callable($label)) {
168168
$label = new PropertyPath($label);
169169
}
170170

@@ -174,7 +174,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
174174
};
175175
}
176176

177-
if (is_string($preferredChoices)) {
177+
if (is_string($preferredChoices) && !is_callable($preferredChoices)) {
178178
$preferredChoices = new PropertyPath($preferredChoices);
179179
}
180180

@@ -189,7 +189,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
189189
};
190190
}
191191

192-
if (is_string($index)) {
192+
if (is_string($index) && !is_callable($index)) {
193193
$index = new PropertyPath($index);
194194
}
195195

@@ -199,7 +199,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
199199
};
200200
}
201201

202-
if (is_string($groupBy)) {
202+
if (is_string($groupBy) && !is_callable($groupBy)) {
203203
$groupBy = new PropertyPath($groupBy);
204204
}
205205

@@ -213,7 +213,7 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
213213
};
214214
}
215215

216-
if (is_string($attr)) {
216+
if (is_string($attr) && !is_callable($attr)) {
217217
$attr = new PropertyPath($attr);
218218
}
219219

Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ function ($object, $key, $value) {
737737
/**
738738
* @group legacy
739739
*/
740-
public function testCreateViewForLegacyChoiceList()
740+
public function testCreateViewForFlatLegacyChoiceList()
741741
{
742742
// legacy ChoiceList instances provide legacy ChoiceView objects
743743
$preferred = array(new LegacyChoiceView('x', 'x', 'Preferred'));
@@ -758,6 +758,36 @@ public function testCreateViewForLegacyChoiceList()
758758
$this->assertEquals(array(new ChoiceView('x', 'x', 'Preferred')), $view->preferredChoices);
759759
}
760760

761+
/**
762+
* @group legacy
763+
*/
764+
public function testCreateViewForNestedLegacyChoiceList()
765+
{
766+
// legacy ChoiceList instances provide legacy ChoiceView objects
767+
$preferred = array('Section 1' => array(new LegacyChoiceView('x', 'x', 'Preferred')));
768+
$other = array(
769+
'Section 2' => array(new LegacyChoiceView('y', 'y', 'Other')),
770+
new LegacyChoiceView('z', 'z', 'Other one'),
771+
);
772+
773+
$list = $this->getMock('Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface');
774+
775+
$list->expects($this->once())
776+
->method('getPreferredViews')
777+
->will($this->returnValue($preferred));
778+
$list->expects($this->once())
779+
->method('getRemainingViews')
780+
->will($this->returnValue($other));
781+
782+
$view = $this->factory->createView(new LegacyChoiceListAdapter($list));
783+
784+
$this->assertEquals(array(
785+
'Section 2' => array(new ChoiceView('y', 'y', 'Other')),
786+
new ChoiceView('z', 'z', 'Other one'),
787+
), $view->choices);
788+
$this->assertEquals(array('Section 1' => array(new ChoiceView('x', 'x', 'Preferred'))), $view->preferredChoices);
789+
}
790+
761791
private function assertScalarListWithChoiceValues(ChoiceListInterface $list)
762792
{
763793
$this->assertSame(array('a', 'b', 'c', 'd'), $list->getValues());

0 commit comments

Comments
 (0)