Sfoglia il codice sorgente

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

Bernhard Schussek 14 anni fa
parent
commit
50955a3919

+ 2 - 2
src/Symfony/Component/Form/PropertyPath.php

@@ -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)) {

+ 18 - 0
tests/Symfony/Tests/Component/Form/PropertyPathTest.php

@@ -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')));