[Form] readPropertyPath should return null instead of empty array

When reading the last bit of a property path mapped to a missing array index, the method would initialize the value to an empty array.  This makes sense for cases where readPropertyPath would again be called recursively, but not when the value would be immediately returned (null would be preferable in that case).

For example, we have an object with a property called "options" that's an array of arbitrary key/value pairs.  That "options" property (and getOptions()) maps directly to a FieldGroup within the Form for this object.  That FieldGroup contains multiple TextFields for a few expected keys in the array.  As-is, if those keys were not defined, the default data set for those TextFields could end up being "Array" (string representation of an empty array).  If readPropertyPath instead returns null for this case, the default data would be transformed into an empty string.
This commit is contained in:
Jeremy Mikola 2010-10-11 15:49:30 -04:00 committed by Fabien Potencier
parent 2b8dfe1ccf
commit df9ef79953
2 changed files with 10 additions and 1 deletions

View File

@ -237,7 +237,7 @@ class PropertyPath implements \IteratorAggregate
// http://bugs.php.net/bug.php?id=52133
else {
if (!array_key_exists($property, $objectOrArray)) {
$objectOrArray[$property] = array();
$objectOrArray[$property] = $currentIndex + 1 < $this->length ? array() : null;
}
$value =& $objectOrArray[$property];

View File

@ -37,6 +37,15 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('Bernhard', $path->getValue($array));
}
public function testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
{
$array = array('child' => array('index' => array()));
$path = new PropertyPath('child[index].firstName');
$this->assertNull($path->getValue($array));
}
public function testGetValueReadsProperty()
{
$object = new Author();