Skip to content

Commit

Permalink
[Form] Merged Field and Form. Merged FieldBuilder and FormBuilder. Af…
Browse files Browse the repository at this point in the history
…ter the refactoring, the distinction between the two concepts is small enough to merge them
  • Loading branch information
Bernhard Schussek committed Mar 20, 2011
1 parent b620dc6 commit 889a160
Show file tree
Hide file tree
Showing 62 changed files with 993 additions and 1,148 deletions.
9 changes: 4 additions & 5 deletions DataMapper/DataMapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@

namespace Symfony\Component\Form\DataMapper;

use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;

interface DataMapperInterface
{
function createEmptyData();

function mapDataToForm(&$data, FormInterface $form);
function mapDataToForms($data, array $forms);

function mapDataToField(&$data, FieldInterface $field);
function mapDataToForm($data, FormInterface $form);

function mapFormToData(FormInterface $form, &$data);
function mapFormsToData(array $forms, &$data);

function mapFieldToData(FieldInterface $field, &$data);
function mapFormToData(FormInterface $field, &$data);
}
37 changes: 19 additions & 18 deletions DataMapper/PropertyPathMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Form\DataMapper;

use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\RecursiveFieldIterator;
use Symfony\Component\Form\Exception\FormException;
Expand Down Expand Up @@ -51,7 +50,7 @@ public function createEmptyData()
return array();
}

public function mapDataToForm(&$data, FormInterface $form)
public function mapDataToForms($data, array $forms)
{
if (!empty($data) && !is_array($data) && !is_object($data)) {
throw new \InvalidArgumentException(sprintf('Expected argument of type object or array, %s given', gettype($data)));
Expand All @@ -62,49 +61,51 @@ public function mapDataToForm(&$data, FormInterface $form)
throw new FormException(sprintf('Form data should be instance of %s', $this->dataClass));
}

$iterator = new RecursiveFieldIterator($form);
$iterator = new RecursiveFieldIterator($forms);
$iterator = new \RecursiveIteratorIterator($iterator);

foreach ($iterator as $field) {
$this->mapDataToField($data, $field);
foreach ($iterator as $form) {
$this->mapDataToForm($data, $form);
}
}
}

public function mapDataToField(&$data, FieldInterface $field)
public function mapDataToForm($data, FormInterface $form)
{
if ($field->getAttribute('property_path') !== null) {
$field->setData($field->getAttribute('property_path')->getValue($data));
if (!empty($data)) {
if ($form->getAttribute('property_path') !== null) {
$form->setData($form->getAttribute('property_path')->getValue($data));
}
}
}

public function mapFormToData(FormInterface $form, &$data)
public function mapFormsToData(array $forms, &$data)
{
$iterator = new RecursiveFieldIterator($form);
$iterator = new RecursiveFieldIterator($forms);
$iterator = new \RecursiveIteratorIterator($iterator);

foreach ($iterator as $field) {
foreach ($iterator as $form) {
$isReference = false;

// If the data is identical to the value in $data, we are
// dealing with a reference
if ($field->getAttribute('property_path') !== null) {
$isReference = $field->getData() === $field->getAttribute('property_path')->getValue($data);
if ($form->getAttribute('property_path') !== null) {
$isReference = $form->getData() === $form->getAttribute('property_path')->getValue($data);
}

// Don't write into $data if $data is an object,
// $isReference is true (see above) and the option "by_reference" is
// true as well
if (!is_object($data) || !$isReference || !$field->getAttribute('by_reference')) {
$this->mapFieldToData($field, $data);
if (!is_object($data) || !$isReference || !$form->getAttribute('by_reference')) {
$this->mapFormToData($form, $data);
}
}
}

public function mapFieldToData(FieldInterface $field, &$data)
public function mapFormToData(FormInterface $form, &$data)
{
if ($field->getAttribute('property_path') !== null) {
$field->getAttribute('property_path')->setValue($data, $field->getData());
if ($form->getAttribute('property_path') !== null) {
$form->getAttribute('property_path')->setValue($data, $form->getData());
}
}
}
4 changes: 2 additions & 2 deletions Event/DataEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Symfony\Component\Form\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;

class DataEvent extends Event
{
private $field;

protected $data;

public function __construct(FieldInterface $field, $data)
public function __construct(FormInterface $field, $data)
{
$this->field = $field;
$this->data = $data;
Expand Down
2 changes: 1 addition & 1 deletion EventListener/FixFileUploadListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Form\EventListener;

use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Events;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down
2 changes: 1 addition & 1 deletion EventListener/FixRadioInputListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Form\EventListener;

use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Events;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down
2 changes: 1 addition & 1 deletion EventListener/FixUrlProtocolListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Component\Form\EventListener;

use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Events;
use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down
3 changes: 1 addition & 2 deletions EventListener/ResizeFormListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

use Symfony\Component\Form\Events;
use Symfony\Component\Form\Event\DataEvent;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FieldInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down
Loading

0 comments on commit 889a160

Please sign in to comment.