[Form] Made $name parameters optional in PropertyPathBuilder:replaceBy(Index|Property)

This commit is contained in:
Bernhard Schussek 2012-05-21 09:15:05 +02:00
parent 081c6437e4
commit 0c09a0e07e
2 changed files with 32 additions and 8 deletions

View File

@ -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
*/

View File

@ -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;
}