merged branch danielholmes/prop_access_additions (PR #7876)

This PR was merged into the master branch.

Discussion
----------

Added negative path replaces and optional string arguments for PropertyPathBuilder.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

7c729f5 [PropertyAccess] Added negative path replaces and optional string arguments for PropertyPathBuilder.
This commit is contained in:
Fabien Potencier 2013-04-30 18:43:32 +02:00
commit be335d0ca4
2 changed files with 64 additions and 26 deletions

View File

@ -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);
@ -44,14 +44,18 @@ class PropertyPathBuilder
/**
* 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 {
@ -106,20 +110,26 @@ class PropertyPathBuilder
/**
* 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) {

View File

@ -73,6 +73,15 @@ class PropertyPathBuilderTest extends \PHPUnit_Framework_TestCase
$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);
@ -168,20 +177,39 @@ class PropertyPathBuilderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($path, $this->builder->getPropertyPath());
}
/**
* @expectedException \OutOfBoundsException
*/
public function testReplaceDoesNotAllowInvalidOffsets()
public function testReplaceUsingString()
{
$this->builder->replace(6, 1, new PropertyPath('new1[new2].new3'));
$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(-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()