Skip to content

Commit

Permalink
[Form] Refactored Field and FieldGroup to facilitate modifications in…
Browse files Browse the repository at this point in the history
… subclasses
  • Loading branch information
Bernhard Schussek authored and fabpot committed Dec 10, 2010
1 parent 984a857 commit 131b3fe
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Field.php
Expand Up @@ -497,7 +497,7 @@ protected function reverseTransform($value)
/**
* {@inheritDoc}
*/
public function updateFromObject(&$objectOrArray)
public function updateFromProperty(&$objectOrArray)
{
// TODO throw exception if not object or array
if ($this->propertyPath !== null) {
Expand All @@ -511,7 +511,7 @@ public function updateFromObject(&$objectOrArray)
/**
* {@inheritDoc}
*/
public function updateObject(&$objectOrArray)
public function updateProperty(&$objectOrArray)
{
// TODO throw exception if not object or array

Expand Down
54 changes: 42 additions & 12 deletions src/Symfony/Component/Form/FieldGroup.php
Expand Up @@ -99,7 +99,7 @@ public function add(FieldInterface $field)
// 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;
Expand Down Expand Up @@ -285,12 +285,7 @@ public function setData($data)
}

if (!empty($data)) {
$iterator = new RecursiveFieldsWithPropertyPathIterator($this);
$iterator = new \RecursiveIteratorIterator($iterator);

foreach ($iterator as $field) {
$field->updateFromObject($data);
}
$this->updateFromObject($data);
}
}

Expand Down Expand Up @@ -341,12 +336,8 @@ public function bind($taintedData)
}

$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);
Expand All @@ -360,6 +351,45 @@ public function bind($taintedData)
}
}

/**
* 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
*
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/FieldInterface.php
Expand Up @@ -114,7 +114,7 @@ function getPropertyPath();
*
* @param array|object $objectOrArray
*/
function updateFromObject(&$objectOrArray);
function updateFromProperty(&$objectOrArray);

/**
* Writes a the field value into a property of the object
Expand All @@ -123,7 +123,7 @@ function updateFromObject(&$objectOrArray);
*
* @param array|object $objectOrArray
*/
function updateObject(&$objectOrArray);
function updateProperty(&$objectOrArray);

/**
* Returns the normalized data of the field.
Expand Down
14 changes: 7 additions & 7 deletions tests/Symfony/Tests/Component/Form/FieldGroupTest.php
Expand Up @@ -444,7 +444,7 @@ public function testAddUpdatesFieldFromTransformedData()
->method('getPropertyPath')
->will($this->returnValue(new PropertyPath('firstName')));
$field->expects($this->once())
->method('updateFromObject')
->method('updateFromProperty')
->with($this->equalTo($transformedAuthor));

$group->add($field);
Expand All @@ -460,7 +460,7 @@ public function testAddDoesNotUpdateFieldsWithEmptyPropertyPath()
->method('getPropertyPath')
->will($this->returnValue(null));
$field->expects($this->never())
->method('updateFromObject');
->method('updateFromProperty');

$group->add($field);
}
Expand All @@ -482,7 +482,7 @@ public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()

$field = $this->createMockField('firstName');
$field->expects($this->never())
->method('updateFromObject');
->method('updateFromProperty');

$group->add($field);
}
Expand All @@ -506,14 +506,14 @@ public function testSetDataUpdatesAllFieldsFromTransformedData()

$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);
Expand Down Expand Up @@ -553,14 +553,14 @@ public function testBindUpdatesTransformedDataFromAllFields()

$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);
Expand Down
12 changes: 6 additions & 6 deletions tests/Symfony/Tests/Component/Form/FieldTest.php
Expand Up @@ -438,13 +438,13 @@ public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
* 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());
}
Expand All @@ -453,24 +453,24 @@ public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
* 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);
}
Expand Down

0 comments on commit 131b3fe

Please sign in to comment.