Skip to content

Commit 131b3fe

Browse files
Bernhard Schussekfabpot
authored andcommitted
[Form] Refactored Field and FieldGroup to facilitate modifications in subclasses
1 parent 984a857 commit 131b3fe

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

src/Symfony/Component/Form/Field.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ protected function reverseTransform($value)
497497
/**
498498
* {@inheritDoc}
499499
*/
500-
public function updateFromObject(&$objectOrArray)
500+
public function updateFromProperty(&$objectOrArray)
501501
{
502502
// TODO throw exception if not object or array
503503
if ($this->propertyPath !== null) {
@@ -511,7 +511,7 @@ public function updateFromObject(&$objectOrArray)
511511
/**
512512
* {@inheritDoc}
513513
*/
514-
public function updateObject(&$objectOrArray)
514+
public function updateProperty(&$objectOrArray)
515515
{
516516
// TODO throw exception if not object or array
517517

src/Symfony/Component/Form/FieldGroup.php

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function add(FieldInterface $field)
9999
// if the property "data" is NULL, getTransformedData() returns an empty
100100
// string
101101
if (!empty($data) && $field->getPropertyPath() !== null) {
102-
$field->updateFromObject($data);
102+
$field->updateFromProperty($data);
103103
}
104104

105105
return $field;
@@ -285,12 +285,7 @@ public function setData($data)
285285
}
286286

287287
if (!empty($data)) {
288-
$iterator = new RecursiveFieldsWithPropertyPathIterator($this);
289-
$iterator = new \RecursiveIteratorIterator($iterator);
290-
291-
foreach ($iterator as $field) {
292-
$field->updateFromObject($data);
293-
}
288+
$this->updateFromObject($data);
294289
}
295290
}
296291

@@ -341,12 +336,8 @@ public function bind($taintedData)
341336
}
342337

343338
$data = $this->getTransformedData();
344-
$iterator = new RecursiveFieldsWithPropertyPathIterator($this);
345-
$iterator = new \RecursiveIteratorIterator($iterator);
346339

347-
foreach ($iterator as $field) {
348-
$field->updateObject($data);
349-
}
340+
$this->updateObject($data);
350341

351342
// bind and reverse transform the data
352343
parent::bind($data);
@@ -360,6 +351,45 @@ public function bind($taintedData)
360351
}
361352
}
362353

354+
/**
355+
* Updates the chield fields from the properties of the given data
356+
*
357+
* This method calls updateFromProperty() on all child fields that have a
358+
* property path set. If a child field has no property path set but
359+
* implements FieldGroupInterface, updateProperty() is called on its
360+
* children instead.
361+
*
362+
* @param array|object $objectOrArray
363+
*/
364+
protected function updateFromObject(&$objectOrArray)
365+
{
366+
$iterator = new RecursiveFieldsWithPropertyPathIterator($this);
367+
$iterator = new \RecursiveIteratorIterator($iterator);
368+
369+
foreach ($iterator as $field) {
370+
$field->updateFromProperty($objectOrArray);
371+
}
372+
}
373+
374+
/**
375+
* Updates all properties of the given data from the child fields
376+
*
377+
* This method calls updateProperty() on all child fields that have a property
378+
* path set. If a child field has no property path set but implements
379+
* FieldGroupInterface, updateProperty() is called on its children instead.
380+
*
381+
* @param array|object $objectOrArray
382+
*/
383+
protected function updateObject(&$objectOrArray)
384+
{
385+
$iterator = new RecursiveFieldsWithPropertyPathIterator($this);
386+
$iterator = new \RecursiveIteratorIterator($iterator);
387+
388+
foreach ($iterator as $field) {
389+
$field->updateProperty($objectOrArray);
390+
}
391+
}
392+
363393
/**
364394
* Processes the bound data before it is passed to the individual fields
365395
*

src/Symfony/Component/Form/FieldInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function getPropertyPath();
114114
*
115115
* @param array|object $objectOrArray
116116
*/
117-
function updateFromObject(&$objectOrArray);
117+
function updateFromProperty(&$objectOrArray);
118118

119119
/**
120120
* Writes a the field value into a property of the object
@@ -123,7 +123,7 @@ function updateFromObject(&$objectOrArray);
123123
*
124124
* @param array|object $objectOrArray
125125
*/
126-
function updateObject(&$objectOrArray);
126+
function updateProperty(&$objectOrArray);
127127

128128
/**
129129
* Returns the normalized data of the field.

tests/Symfony/Tests/Component/Form/FieldGroupTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ public function testAddUpdatesFieldFromTransformedData()
444444
->method('getPropertyPath')
445445
->will($this->returnValue(new PropertyPath('firstName')));
446446
$field->expects($this->once())
447-
->method('updateFromObject')
447+
->method('updateFromProperty')
448448
->with($this->equalTo($transformedAuthor));
449449

450450
$group->add($field);
@@ -460,7 +460,7 @@ public function testAddDoesNotUpdateFieldsWithEmptyPropertyPath()
460460
->method('getPropertyPath')
461461
->will($this->returnValue(null));
462462
$field->expects($this->never())
463-
->method('updateFromObject');
463+
->method('updateFromProperty');
464464

465465
$group->add($field);
466466
}
@@ -482,7 +482,7 @@ public function testAddDoesNotUpdateFieldIfTransformedDataIsEmpty()
482482

483483
$field = $this->createMockField('firstName');
484484
$field->expects($this->never())
485-
->method('updateFromObject');
485+
->method('updateFromProperty');
486486

487487
$group->add($field);
488488
}
@@ -506,14 +506,14 @@ public function testSetDataUpdatesAllFieldsFromTransformedData()
506506

507507
$field = $this->createMockField('firstName');
508508
$field->expects($this->once())
509-
->method('updateFromObject')
509+
->method('updateFromProperty')
510510
->with($this->equalTo($transformedAuthor));
511511

512512
$group->add($field);
513513

514514
$field = $this->createMockField('lastName');
515515
$field->expects($this->once())
516-
->method('updateFromObject')
516+
->method('updateFromProperty')
517517
->with($this->equalTo($transformedAuthor));
518518

519519
$group->add($field);
@@ -553,14 +553,14 @@ public function testBindUpdatesTransformedDataFromAllFields()
553553

554554
$field = $this->createMockField('firstName');
555555
$field->expects($this->once())
556-
->method('updateObject')
556+
->method('updateProperty')
557557
->with($this->equalTo($transformedAuthor));
558558

559559
$group->add($field);
560560

561561
$field = $this->createMockField('lastName');
562562
$field->expects($this->once())
563-
->method('updateObject')
563+
->method('updateProperty')
564564
->with($this->equalTo($transformedAuthor));
565565

566566
$group->add($field);

tests/Symfony/Tests/Component/Form/FieldTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,13 @@ public function testBoundValuesAreNotTrimmedBeforeTransformingIfDisabled()
438438
* Even if the field group itself is not associated to a specific property,
439439
* nested fields might be.
440440
*/
441-
public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
441+
public function testUpdateFromPropertyPassesObjectThroughIfPropertyPathIsEmpty()
442442
{
443443
$object = new Author();
444444
$object->firstName = 'Bernhard';
445445

446446
$field = new TestField('firstName', array('property_path' => null));
447-
$field->updateFromObject($object);
447+
$field->updateFromProperty($object);
448448

449449
$this->assertEquals($object, $field->getData());
450450
}
@@ -453,24 +453,24 @@ public function testUpdateFromObjectPassesObjectThroughIfPropertyPathIsEmpty()
453453
* This is important so that bind() can work even if setData() was not called
454454
* before
455455
*/
456-
public function testUpdateObjectTreatsEmptyValuesAsArrays()
456+
public function testUpdatePropertyTreatsEmptyValuesAsArrays()
457457
{
458458
$array = null;
459459

460460
$field = new TestField('firstName');
461461
$field->bind('Bernhard');
462-
$field->updateObject($array);
462+
$field->updateProperty($array);
463463

464464
$this->assertEquals(array('firstName' => 'Bernhard'), $array);
465465
}
466466

467-
public function testUpdateObjectDoesNotUpdatePropertyIfPropertyPathIsEmpty()
467+
public function testUpdatePropertyDoesNotUpdatePropertyIfPropertyPathIsEmpty()
468468
{
469469
$object = new Author();
470470

471471
$field = new TestField('firstName', array('property_path' => null));
472472
$field->bind('Bernhard');
473-
$field->updateObject($object);
473+
$field->updateProperty($object);
474474

475475
$this->assertEquals(null, $object->firstName);
476476
}

0 commit comments

Comments
 (0)