[Validator] Fixed PropertyPath to read array indices with special characters

This commit is contained in:
Bernhard Schussek 2011-02-03 11:28:51 +01:00
parent 55a97ec78e
commit 50955a3919
2 changed files with 20 additions and 2 deletions

View File

@ -63,7 +63,7 @@ class PropertyPath implements \IteratorAggregate
$remaining = $propertyPath;
// first element is evaluated differently - no leading dot for properties
$pattern = '/^((\w+)|\[(\w+)\])(.*)/';
$pattern = '/^((\w+)|\[([^\]]+)\])(.*)/';
while (preg_match($pattern, $remaining, $matches)) {
if ($matches[2] !== '') {
@ -76,7 +76,7 @@ class PropertyPath implements \IteratorAggregate
$position += strlen($matches[1]);
$remaining = $matches[4];
$pattern = '/^(\.(\w+)|\[(\w+)\])(.*)/';
$pattern = '/^(\.(\w+)|\[([^\]]+)\])(.*)/';
}
if (!empty($remaining)) {

View File

@ -39,6 +39,24 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Bernhard', $path->getValue($array));
}
public function testGetValueReadsIndexWithSpecialChars()
{
$array = array('#!@$.' => 'Bernhard');
$path = new PropertyPath('[#!@$.]');
$this->assertEquals('Bernhard', $path->getValue($array));
}
public function testGetValueReadsNestedIndexWithSpecialChars()
{
$array = array('root' => array('#!@$.' => 'Bernhard'));
$path = new PropertyPath('root[#!@$.]');
$this->assertEquals('Bernhard', $path->getValue($array));
}
public function testGetValueReadsArrayWithCustomPropertyPath()
{
$array = array('child' => array('index' => array('firstName' => 'Bernhard')));