diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathBuilderTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathBuilderTest.php index 0c0424531e..db0f18b396 100644 --- a/src/Symfony/Component/Form/Tests/Util/PropertyPathBuilderTest.php +++ b/src/Symfony/Component/Form/Tests/Util/PropertyPathBuilderTest.php @@ -100,6 +100,15 @@ class PropertyPathBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($path, $this->builder->getPropertyPath()); } + public function testReplaceByIndexWithoutName() + { + $this->builder->replaceByIndex(0); + + $path = new PropertyPath('[old1][old2].old3[old4][old5].old6'); + + $this->assertEquals($path, $this->builder->getPropertyPath()); + } + /** * @expectedException \OutOfBoundsException */ @@ -125,6 +134,15 @@ class PropertyPathBuilderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($path, $this->builder->getPropertyPath()); } + public function testReplaceByPropertyWithoutName() + { + $this->builder->replaceByProperty(1); + + $path = new PropertyPath('old1.old2.old3[old4][old5].old6'); + + $this->assertEquals($path, $this->builder->getPropertyPath()); + } + /** * @expectedException \OutOfBoundsException */ diff --git a/src/Symfony/Component/Form/Util/PropertyPathBuilder.php b/src/Symfony/Component/Form/Util/PropertyPathBuilder.php index ed0e5a263a..32b483ca68 100644 --- a/src/Symfony/Component/Form/Util/PropertyPathBuilder.php +++ b/src/Symfony/Component/Form/Util/PropertyPathBuilder.php @@ -131,38 +131,44 @@ class PropertyPathBuilder } /** - * Replaces a sub-path by a single index element. + * Replaces a property element by an index element. * * @param integer $offset The offset at which to replace. - * @param string $name The inserted index name. + * @param string $name The new name of the element. Optional. * * @throws \OutOfBoundsException If the offset is invalid. */ - public function replaceByIndex($offset, $name) + public function replaceByIndex($offset, $name = null) { if (!isset($this->elements[$offset])) { throw new \OutOfBoundsException('The offset ' . $offset . ' is not within the property path'); } - $this->elements[$offset] = $name; + if (null !== $name) { + $this->elements[$offset] = $name; + } + $this->isIndex[$offset] = true; } /** - * Replaces a sub-path by a single property element. + * Replaces an index element by a property element. * * @param integer $offset The offset at which to replace. - * @param string $name The inserted property name. + * @param string $name The new name of the element. Optional. * * @throws \OutOfBoundsException If the offset is invalid. */ - public function replaceByProperty($offset, $name) + public function replaceByProperty($offset, $name = null) { if (!isset($this->elements[$offset])) { throw new \OutOfBoundsException('The offset ' . $offset . ' is not within the property path'); } - $this->elements[$offset] = $name; + if (null !== $name) { + $this->elements[$offset] = $name; + } + $this->isIndex[$offset] = false; }