Browse Source

[Form] Further simplified PropertyPath code

Bernhard Schussek 14 years ago
parent
commit
a7ff4f484a
1 changed files with 23 additions and 50 deletions
  1. 23 50
      src/Symfony/Component/Form/Util/PropertyPath.php

+ 23 - 50
src/Symfony/Component/Form/Util/PropertyPath.php

@@ -186,7 +186,25 @@ class PropertyPath implements \IteratorAggregate
      */
     public function getValue($objectOrArray)
     {
-        return $this->readPropertyPath($objectOrArray, 0);
+        for ($i = 0; $i < $this->length; ++$i) {
+            if (is_object($objectOrArray)) {
+                $value = $this->readProperty($objectOrArray, $i);
+            // arrays need to be treated separately (due to PHP bug?)
+            // http://bugs.php.net/bug.php?id=52133
+            } else if (is_array($objectOrArray)){
+                $property = $this->elements[$i];
+                if (!array_key_exists($property, $objectOrArray)) {
+                    $objectOrArray[$property] = $i + 1 < $this->length ? array() : null;
+                }
+                $value =& $objectOrArray[$property];
+            } else {
+                throw new UnexpectedTypeException($objectOrArray, 'object or array');
+            }
+
+            $objectOrArray =& $value;
+        }
+
+        return $value;
     }
 
     /**
@@ -219,59 +237,14 @@ class PropertyPath implements \IteratorAggregate
      */
     public function setValue(&$objectOrArray, $value)
     {
-        $this->writePropertyPath($objectOrArray, 0, $value);
-    }
-
-    /**
-     * Recursive implementation of getValue()
-     *
-     * @param  object|array $objectOrArray  The object or array to traverse
-     * @param  integer $currentIndex        The current index in the property path
-     * @return mixed                        The value at the end of the path
-     */
-    protected function readPropertyPath(&$objectOrArray, $currentIndex)
-    {
-        for (;$currentIndex < $this->length; ++$currentIndex) {
-            if (is_object($objectOrArray)) {
-                $value = $this->readProperty($objectOrArray, $currentIndex);
-            // arrays need to be treated separately (due to PHP bug?)
-            // http://bugs.php.net/bug.php?id=52133
-            } else if (is_array($objectOrArray)){
-                $property = $this->elements[$currentIndex];
-                if (!array_key_exists($property, $objectOrArray)) {
-                    $objectOrArray[$property] = $currentIndex + 1 < $this->length ? array() : null;
-                }
-                $value =& $objectOrArray[$property];
-            } else {
-                throw new UnexpectedTypeException($objectOrArray, 'object or array');
-            }
-
-            $objectOrArray =& $value;
-        }
-
-        return $value;
-    }
-
-    /**
-     * Recursive implementation of setValue()
-     *
-     * @param object|array $objectOrArray  The object or array to traverse
-     * @param integer $currentIndex        The current index in the property path
-     * @param mixed $value                 The value to set at the end of the
-     *                                     property path
-     */
-    protected function writePropertyPath(&$objectOrArray, $currentIndex, $value)
-    {
-        $length = $this->length - 1;
-
-        for (;$currentIndex < $length; ++$currentIndex) {
+        for ($i = 0, $l = $this->length - 1; $i < $l; ++$i) {
 
             if (is_object($objectOrArray)) {
-                $nestedObject = $this->readProperty($objectOrArray, $currentIndex);
+                $nestedObject = $this->readProperty($objectOrArray, $i);
             // arrays need to be treated separately (due to PHP bug?)
             // http://bugs.php.net/bug.php?id=52133
             } else if (is_array($objectOrArray)) {
-                $property = $this->elements[$currentIndex];
+                $property = $this->elements[$i];
                 if (!array_key_exists($property, $objectOrArray)) {
                     $objectOrArray[$property] = array();
                 }
@@ -287,7 +260,7 @@ class PropertyPath implements \IteratorAggregate
             throw new UnexpectedTypeException($objectOrArray, 'object or array');
         }
 
-        $this->writeProperty($objectOrArray, $currentIndex, $value);
+        $this->writeProperty($objectOrArray, $i, $value);
     }
 
     /**