[Form] Added getParent() to PropertyPath
This commit is contained in:
parent
7e5104e09b
commit
b56502f023
@ -27,26 +27,32 @@ class PropertyPath implements \IteratorAggregate
|
|||||||
* The elements of the property path
|
* The elements of the property path
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $elements = array();
|
private $elements = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The number of elements in the property path
|
* The number of elements in the property path
|
||||||
* @var integer
|
* @var integer
|
||||||
*/
|
*/
|
||||||
protected $length;
|
private $length;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains a Boolean for each property in $elements denoting whether this
|
* Contains a Boolean for each property in $elements denoting whether this
|
||||||
* element is an index. It is a property otherwise.
|
* element is an index. It is a property otherwise.
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $isIndex = array();
|
private $isIndex = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String representation of the path
|
* String representation of the path
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $string;
|
private $string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Positions where the individual elements start in the string representation
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $positions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses the given property path
|
* Parses the given property path
|
||||||
@ -67,6 +73,8 @@ class PropertyPath implements \IteratorAggregate
|
|||||||
$pattern = '/^(([^\.\[]+)|\[([^\]]+)\])(.*)/';
|
$pattern = '/^(([^\.\[]+)|\[([^\]]+)\])(.*)/';
|
||||||
|
|
||||||
while (preg_match($pattern, $remaining, $matches)) {
|
while (preg_match($pattern, $remaining, $matches)) {
|
||||||
|
$this->positions[] = $position;
|
||||||
|
|
||||||
if ('' !== $matches[2]) {
|
if ('' !== $matches[2]) {
|
||||||
$this->elements[] = $matches[2];
|
$this->elements[] = $matches[2];
|
||||||
$this->isIndex[] = false;
|
$this->isIndex[] = false;
|
||||||
@ -102,6 +110,33 @@ class PropertyPath implements \IteratorAggregate
|
|||||||
return $this->string;
|
return $this->string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the parent property path.
|
||||||
|
*
|
||||||
|
* The parent property path is the one that contains the same items as
|
||||||
|
* this one except for the last one.
|
||||||
|
*
|
||||||
|
* If this property path only contains one item, null is returned.
|
||||||
|
*
|
||||||
|
* @return PropertyPath The parent path or null.
|
||||||
|
*/
|
||||||
|
public function getParent()
|
||||||
|
{
|
||||||
|
if ($this->length <= 1) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parent = clone $this;
|
||||||
|
|
||||||
|
--$parent->length;
|
||||||
|
$parent->string = substr($parent->string, 0, $parent->positions[$parent->length]);
|
||||||
|
array_pop($parent->elements);
|
||||||
|
array_pop($parent->isIndex);
|
||||||
|
array_pop($parent->positions);
|
||||||
|
|
||||||
|
return $parent;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new iterator for this path
|
* Returns a new iterator for this path
|
||||||
*
|
*
|
||||||
|
@ -391,4 +391,25 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
new PropertyPath(null);
|
new PropertyPath(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetParent_dot()
|
||||||
|
{
|
||||||
|
$propertyPath = new PropertyPath('grandpa.parent.child');
|
||||||
|
|
||||||
|
$this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetParent_index()
|
||||||
|
{
|
||||||
|
$propertyPath = new PropertyPath('grandpa.parent[child]');
|
||||||
|
|
||||||
|
$this->assertEquals(new PropertyPath('grandpa.parent'), $propertyPath->getParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetParent_noParent()
|
||||||
|
{
|
||||||
|
$propertyPath = new PropertyPath('path');
|
||||||
|
|
||||||
|
$this->assertNull($propertyPath->getParent());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user