Skip to content

Commit 3da6808

Browse files
committed
[Form] Fixed: Support objects with __toString() in choice groups
1 parent 77e01c0 commit 3da6808

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

ChoiceList/Factory/DefaultChoiceListFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ private static function addChoiceViewGroupedBy($groupBy, $choice, $key, $label,
329329
return;
330330
}
331331

332+
$groupLabel = (string) $groupLabel;
333+
332334
// Initialize the group views if necessary. Unnnecessarily built group
333335
// views will be cleaned up at the end of createView()
334336
if (!isset($preferredViews[$groupLabel])) {

Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ public function getGroup($object)
7777
return $this->obj1 === $object || $this->obj2 === $object ? 'Group 1' : 'Group 2';
7878
}
7979

80+
public function getGroupAsObject($object)
81+
{
82+
return $this->obj1 === $object || $this->obj2 === $object
83+
? new DefaultChoiceListFactoryTest_Castable('Group 1')
84+
: new DefaultChoiceListFactoryTest_Castable('Group 2');
85+
}
86+
8087
protected function setUp()
8188
{
8289
$this->obj1 = (object) array('label' => 'A', 'index' => 'w', 'value' => 'a', 'preferred' => false, 'group' => 'Group 1', 'attr' => array());
@@ -581,6 +588,19 @@ public function testCreateViewFlatGroupByAsCallable()
581588
$this->assertGroupedView($view);
582589
}
583590

591+
public function testCreateViewFlatGroupByObjectThatCanBeCastToString()
592+
{
593+
$view = $this->factory->createView(
594+
$this->list,
595+
array($this->obj2, $this->obj3),
596+
null, // label
597+
null, // index
598+
array($this, 'getGroupAsObject')
599+
);
600+
601+
$this->assertGroupedView($view);
602+
}
603+
584604
public function testCreateViewFlatGroupByAsClosure()
585605
{
586606
$obj1 = $this->obj1;
@@ -897,3 +917,18 @@ private function assertGroupedView($view)
897917
), $view);
898918
}
899919
}
920+
921+
class DefaultChoiceListFactoryTest_Castable
922+
{
923+
private $property;
924+
925+
public function __construct($property)
926+
{
927+
$this->property = $property;
928+
}
929+
930+
public function __toString()
931+
{
932+
return $this->property;
933+
}
934+
}

0 commit comments

Comments
 (0)