From c2baaa5d39aab64d5e9337ea6b957ca3acca2505 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 10 Dec 2013 19:21:08 +0100 Subject: [PATCH] use the property path to check for equality when a value path is given instead of comparing for object identiy --- .../Core/ChoiceList/ObjectChoiceList.php | 32 ++++++++++++ .../Core/ChoiceList/ObjectChoiceListTest.php | 52 +++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php index 0a153883a3868..c32cb60fe50f6 100644 --- a/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php +++ b/src/Symfony/Component/Form/Extension/Core/ChoiceList/ObjectChoiceList.php @@ -95,6 +95,38 @@ public function __construct($choices, $labelPath = null, array $preferredChoices parent::__construct($choices, array(), $preferredChoices); } + /** + * {@inheritdoc} + */ + public function getValuesForChoices(array $choices) + { + if ($this->valuePath) { + $choices = $this->fixChoices($choices); + $values = array(); + + $availableValues = $this->getValues(); + foreach ($this->getChoices() as $i => $choice) { + foreach ($choices as $j => $givenChoice) { + if ( + (is_array($givenChoice) || is_object($givenChoice)) + && $this->createValue($choice) === $this->createValue($givenChoice) + ) { + $values[] = $availableValues[$i]; + unset($choices[$j]); + + if (0 === count($choices)) { + break 2; + } + } + } + } + + return $values; + } + + return parent::getValuesForChoices($choices); + } + /** * Initializes the list with choices. * diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php index 27effd9f5c2c0..65d3cac7ecb8b 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/ChoiceList/ObjectChoiceListTest.php @@ -185,6 +185,58 @@ public function testInitArrayThrowsExceptionIfToStringNotFound() ); } + public function testGetValuesForChoicesWithIdenticalObjects() + { + $foo = new \stdClass(); + $foo->id = 11; + $foo->name = 'Foo'; + + $bar = new \stdClass(); + $bar->id = 12; + $bar->name = 'Bar'; + + $baz = new \stdClass(); + $baz->id = 13; + $baz->name = 'Baz'; + + $list = new ObjectChoiceList( + array($foo, $bar, $baz), + 'name', + array(), + null, + 'id' + ); + $this->assertEquals(array(12), $list->getValuesForChoices(array($bar))); + } + + public function testGetValuesForChoicesWithEqualObjects() + { + $foo = new \stdClass(); + $foo->id = 11; + $foo->name = 'Foo'; + + $bar = new \stdClass(); + $bar->id = 12; + $bar->name = 'Bar'; + + $baz = new \stdClass(); + $baz->id = 13; + $baz->name = 'Baz'; + + $anotherBar = new \stdClass(); + $anotherBar->id = 12; + $anotherBar->name = 'Bar'; + + $list = new ObjectChoiceList( + array($foo, $bar, $baz), + 'name', + array(), + null, + 'id' + ); + $this->assertEquals(array(12), $list->getValuesForChoices(array($anotherBar))); + } + /** * @return \Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface */