[PropertyAccess] Allowed non alphanumeric chars in object properties

This commit is contained in:
florianv 2013-12-23 14:17:48 +01:00
parent e00b0f34bd
commit 20d4eb6ad4
4 changed files with 40 additions and 3 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
2.5.0
------
* allowed non alpha numeric characters in second level and deeper object properties names
2.3.0 2.3.0
------ ------

View File

@ -118,7 +118,7 @@ class PropertyPath implements \IteratorAggregate, PropertyPathInterface
$position += strlen($matches[1]); $position += strlen($matches[1]);
$remaining = $matches[4]; $remaining = $matches[4];
$pattern = '/^(\.(\w+)|\[([^\]]+)\])(.*)/'; $pattern = '/^(\.([^\.|\[]+)|\[([^\]]+)\])(.*)/';
} }
if ('' !== $remaining) { if ('' !== $remaining) {

View File

@ -137,6 +137,13 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Bernhard', $this->getPropertyAccessor()->getValue($array, '%!@$§')); $this->assertEquals('Bernhard', $this->getPropertyAccessor()->getValue($array, '%!@$§'));
} }
public function testGetValueReadsPropertyWithSpecialCharsExceptDotNested()
{
$object = (object) array('nested' => (object) array('@child' => 'foo'));
$this->assertEquals('foo', $this->getPropertyAccessor()->getValue($object, 'nested.@child'));
}
public function testGetValueReadsPropertyWithCustomPropertyPath() public function testGetValueReadsPropertyWithCustomPropertyPath()
{ {
$object = new Author(); $object = new Author();
@ -328,6 +335,17 @@ class PropertyAccessorTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Schussek', $object->getLastName()); $this->assertEquals('Schussek', $object->getLastName());
} }
public function testSetValueWithSpecialCharsNested()
{
$object = new \stdClass();
$person = new \stdClass();
$person->{'@email'} = null;
$object->person = $person;
$this->getPropertyAccessor()->setValue($object, 'person.@email', 'bar');
$this->assertEquals('bar', $object->person->{'@email'});
}
/** /**
* @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException * @expectedException \Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException
*/ */

View File

@ -38,12 +38,26 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
new PropertyPath('.property'); new PropertyPath('.property');
} }
public function providePathsContainingUnexpectedCharacters()
{
return array(
array('property.'),
array('property.['),
array('property..'),
array('property['),
array('property[['),
array('property[.'),
array('property[]'),
);
}
/** /**
* @dataProvider providePathsContainingUnexpectedCharacters
* @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException * @expectedException \Symfony\Component\PropertyAccess\Exception\InvalidPropertyPathException
*/ */
public function testUnexpectedCharacters() public function testUnexpectedCharacters($path)
{ {
new PropertyPath('property.$foo'); new PropertyPath($path);
} }
/** /**