Skip to content

Commit

Permalink
[PropertyAccess] Added negative path replaces and optional string arg…
Browse files Browse the repository at this point in the history
…uments for PropertyPathBuilder.
  • Loading branch information
danielholmes committed Apr 29, 2013
1 parent 84d759d commit 7c729f5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
48 changes: 29 additions & 19 deletions src/Symfony/Component/PropertyAccess/PropertyPathBuilder.php
Expand Up @@ -31,10 +31,10 @@ class PropertyPathBuilder
/**
* Creates a new property path builder.
*
* @param null|PropertyPathInterface $path The path to initially store
* in the builder. Optional.
* @param null|PropertyPathInterface|string $path The path to initially store
* in the builder. Optional.
*/
public function __construct(PropertyPathInterface $path = null)
public function __construct($path = null)
{
if (null !== $path) {
$this->append($path);
Expand All @@ -44,14 +44,18 @@ public function __construct(PropertyPathInterface $path = null)
/**
* Appends a (sub-) path to the current path.
*
* @param PropertyPathInterface $path The path to append
* @param integer $offset The offset where the appended piece
* starts in $path
* @param integer $length The length of the appended piece.
* If 0, the full path is appended.
* @param PropertyPathInterface|string $path The path to append.
* @param integer $offset The offset where the appended
* piece starts in $path.
* @param integer $length The length of the appended piece.
* If 0, the full path is appended.
*/
public function append(PropertyPathInterface $path, $offset = 0, $length = 0)
public function append($path, $offset = 0, $length = 0)
{
if (is_string($path)) {
$path = new PropertyPath($path);
}

if (0 === $length) {
$end = $path->getLength();
} else {
Expand Down Expand Up @@ -106,20 +110,26 @@ public function remove($offset, $length = 1)
/**
* Replaces a sub-path by a different (sub-) path.
*
* @param integer $offset The offset at which to replace
* @param integer $length The length of the piece to replace
* @param PropertyPathInterface $path The path to insert
* @param integer $pathOffset The offset where the inserted piece
* starts in $path
* @param integer $pathLength The length of the inserted piece.
* If 0, the full path is inserted.
* @param integer $offset The offset at which to replace.
* @param integer $length The length of the piece to replace.
* @param PropertyPathInterface|string $path The path to insert.
* @param integer $pathOffset The offset where the inserted piece
* starts in $path.
* @param integer $pathLength The length of the inserted piece.
* If 0, the full path is inserted.
*
* @throws OutOfBoundsException If the offset is invalid
*/
public function replace($offset, $length, PropertyPathInterface $path, $pathOffset = 0, $pathLength = 0)
public function replace($offset, $length, $path, $pathOffset = 0, $pathLength = 0)
{
if (!isset($this->elements[$offset])) {
throw new OutOfBoundsException(sprintf('The offset %s is not within the property path', $offset));
if (is_string($path)) {
$path = new PropertyPath($path);
}

if ($offset < 0 && abs($offset) <= $this->getLength()) {
$offset = $this->getLength() + $offset;
} elseif (!isset($this->elements[$offset])) {
throw new OutOfBoundsException('The offset ' . $offset . ' is not within the property path');
}

if (0 === $pathLength) {
Expand Down
Expand Up @@ -73,6 +73,15 @@ public function testAppend()
$this->assertEquals($path, $this->builder->getPropertyPath());
}

public function testAppendUsingString()
{
$this->builder->append('new1[new2]');

$path = new PropertyPath(self::PREFIX . '.new1[new2]');

$this->assertEquals($path, $this->builder->getPropertyPath());
}

public function testAppendWithOffset()
{
$this->builder->append(new PropertyPath('new1[new2].new3'), 1);
Expand Down Expand Up @@ -168,20 +177,39 @@ public function testReplace()
$this->assertEquals($path, $this->builder->getPropertyPath());
}

/**
* @expectedException \OutOfBoundsException
*/
public function testReplaceDoesNotAllowInvalidOffsets()
public function testReplaceUsingString()
{
$this->builder->replace(1, 1, 'new1[new2].new3');

$path = new PropertyPath('old1.new1[new2].new3.old3[old4][old5].old6');

$this->assertEquals($path, $this->builder->getPropertyPath());
}

public function testReplaceNegative()
{
$this->builder->replace(6, 1, new PropertyPath('new1[new2].new3'));
$this->builder->replace(-1, 1, new PropertyPath('new1[new2].new3'));

$path = new PropertyPath('old1[old2].old3[old4][old5].new1[new2].new3');

$this->assertEquals($path, $this->builder->getPropertyPath());
}

/**
* @dataProvider provideInvalidOffsets
* @expectedException \OutOfBoundsException
*/
public function testReplaceDoesNotAllowNegativeOffsets()
public function testReplaceDoesNotAllowInvalidOffsets($offset)
{
$this->builder->replace(-1, 1, new PropertyPath('new1[new2].new3'));
$this->builder->replace($offset, 1, new PropertyPath('new1[new2].new3'));
}

public function provideInvalidOffsets()
{
return array(
array(6),
array(-7),
);
}

public function testReplaceWithLengthGreaterOne()
Expand Down

0 comments on commit 7c729f5

Please sign in to comment.