浏览代码

[Form] Refactored Field and FieldGroup to facilitate modifications in subclasses

Bernhard Schussek 14 年之前
父节点
当前提交
131b3fe373

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

@@ -497,7 +497,7 @@ class Field extends Configurable implements FieldInterface
     /**
      * {@inheritDoc}
      */
-    public function updateFromObject(&$objectOrArray)
+    public function updateFromProperty(&$objectOrArray)
     {
         // TODO throw exception if not object or array
         if ($this->propertyPath !== null) {
@@ -511,7 +511,7 @@ class Field extends Configurable implements FieldInterface
     /**
      * {@inheritDoc}
      */
-    public function updateObject(&$objectOrArray)
+    public function updateProperty(&$objectOrArray)
     {
         // TODO throw exception if not object or array
 

+ 42 - 12
src/Symfony/Component/Form/FieldGroup.php

@@ -99,7 +99,7 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         // if the property "data" is NULL, getTransformedData() returns an empty
         // string
         if (!empty($data) && $field->getPropertyPath() !== null) {
-            $field->updateFromObject($data);
+            $field->updateFromProperty($data);
         }
 
         return $field;
@@ -285,12 +285,7 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         }
 
         if (!empty($data)) {
-            $iterator = new RecursiveFieldsWithPropertyPathIterator($this);
-            $iterator = new \RecursiveIteratorIterator($iterator);
-
-            foreach ($iterator as $field) {
-                $field->updateFromObject($data);
-            }
+            $this->updateFromObject($data);
         }
     }
 
@@ -341,12 +336,8 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         }
 
         $data = $this->getTransformedData();
-        $iterator = new RecursiveFieldsWithPropertyPathIterator($this);
-        $iterator = new \RecursiveIteratorIterator($iterator);
 
-        foreach ($iterator as $field) {
-            $field->updateObject($data);
-        }
+        $this->updateObject($data);
 
         // bind and reverse transform the data
         parent::bind($data);
@@ -360,6 +351,45 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         }
     }
 
+    /**
+     * Updates the chield fields from the properties of the given data
+     *
+     * This method calls updateFromProperty() on all child fields that have a
+     * property path set. If a child field has no property path set but
+     * implements FieldGroupInterface, updateProperty() is called on its
+     * children instead.
+     *
+     * @param array|object $objectOrArray
+     */
+    protected function updateFromObject(&$objectOrArray)
+    {
+        $iterator = new RecursiveFieldsWithPropertyPathIterator($this);
+        $iterator = new \RecursiveIteratorIterator($iterator);
+
+        foreach ($iterator as $field) {
+            $field->updateFromProperty($objectOrArray);
+        }
+    }
+
+    /**
+     * Updates all properties of the given data from the child fields
+     *
+     * This method calls updateProperty() on all child fields that have a property
+     * path set. If a child field has no property path set but implements
+     * FieldGroupInterface, updateProperty() is called on its children instead.
+     *
+     * @param array|object $objectOrArray
+     */
+    protected function updateObject(&$objectOrArray)
+    {
+        $iterator = new RecursiveFieldsWithPropertyPathIterator($this);
+        $iterator = new \RecursiveIteratorIterator($iterator);
+
+        foreach ($iterator as $field) {
+            $field->updateProperty($objectOrArray);
+        }
+    }
+
     /**
      * Processes the bound data before it is passed to the individual fields
      *

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

@@ -114,7 +114,7 @@ interface FieldInterface extends Localizable
      *
      * @param array|object $objectOrArray
      */
-    function updateFromObject(&$objectOrArray);
+    function updateFromProperty(&$objectOrArray);
 
     /**
      * Writes a the field value into a property of the object
@@ -123,7 +123,7 @@ interface FieldInterface extends Localizable
      *
      * @param array|object $objectOrArray
      */
-    function updateObject(&$objectOrArray);
+    function updateProperty(&$objectOrArray);
 
     /**
      * Returns the normalized data of the field.

+ 7 - 7
tests/Symfony/Tests/Component/Form/FieldGroupTest.php

@@ -444,7 +444,7 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
                     ->method('getPropertyPath')
                     ->will($this->returnValue(new PropertyPath('firstName')));
         $field->expects($this->once())
-                    ->method('updateFromObject')
+                    ->method('updateFromProperty')
                     ->with($this->equalTo($transformedAuthor));
 
         $group->add($field);
@@ -460,7 +460,7 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
                     ->method('getPropertyPath')
                     ->will($this->returnValue(null));
         $field->expects($this->never())
-                    ->method('updateFromObject');
+                    ->method('updateFromProperty');
 
         $group->add($field);
     }
@@ -482,7 +482,7 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
 
         $field = $this->createMockField('firstName');
         $field->expects($this->never())
-                    ->method('updateFromObject');
+                    ->method('updateFromProperty');
 
         $group->add($field);
     }
@@ -506,14 +506,14 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
 
         $field = $this->createMockField('firstName');
         $field->expects($this->once())
-                    ->method('updateFromObject')
+                    ->method('updateFromProperty')
                     ->with($this->equalTo($transformedAuthor));
 
         $group->add($field);
 
         $field = $this->createMockField('lastName');
         $field->expects($this->once())
-                    ->method('updateFromObject')
+                    ->method('updateFromProperty')
                     ->with($this->equalTo($transformedAuthor));
 
         $group->add($field);
@@ -553,14 +553,14 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
 
         $field = $this->createMockField('firstName');
         $field->expects($this->once())
-                    ->method('updateObject')
+                    ->method('updateProperty')
                     ->with($this->equalTo($transformedAuthor));
 
         $group->add($field);
 
         $field = $this->createMockField('lastName');
         $field->expects($this->once())
-                    ->method('updateObject')
+                    ->method('updateProperty')
                     ->with($this->equalTo($transformedAuthor));
 
         $group->add($field);

+ 6 - 6
tests/Symfony/Tests/Component/Form/FieldTest.php

@@ -438,13 +438,13 @@ class FieldTest extends \PHPUnit_Framework_TestCase
      * Even if the field group itself is not associated to a specific property,
      * nested fields might be.
      */
-    public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
+    public function testUpdateFromPropertyPassesObjectThroughIfPropertyPathIsEmpty()
     {
         $object = new Author();
         $object->firstName = 'Bernhard';
 
         $field = new TestField('firstName', array('property_path' => null));
-        $field->updateFromObject($object);
+        $field->updateFromProperty($object);
 
         $this->assertEquals($object, $field->getData());
     }
@@ -453,24 +453,24 @@ class FieldTest extends \PHPUnit_Framework_TestCase
      * This is important so that bind() can work even if setData() was not called
      * before
      */
-    public function testUpdateObjectTreatsEmptyValuesAsArrays()
+    public function testUpdatePropertyTreatsEmptyValuesAsArrays()
     {
         $array = null;
 
         $field = new TestField('firstName');
         $field->bind('Bernhard');
-        $field->updateObject($array);
+        $field->updateProperty($array);
 
         $this->assertEquals(array('firstName' => 'Bernhard'), $array);
     }
 
-    public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
+    public function testUpdatePropertyDoesNotUpdatePropertyIfPropertyPathIsEmpty()
     {
         $object = new Author();
 
         $field = new TestField('firstName', array('property_path' => null));
         $field->bind('Bernhard');
-        $field->updateObject($object);
+        $field->updateProperty($object);
 
         $this->assertEquals(null, $object->firstName);
     }