Просмотр исходного кода

[Form] Refactored EntityChoiceField::processData() into individual class

Bernhard Schussek 14 лет назад
Родитель
Сommit
f39d8b62f2

+ 49 - 0
src/Symfony/Component/Form/DataProcessor/CollectionMerger.php

@@ -0,0 +1,49 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\DataProcessor;
+
+use Symfony\Component\Form\FieldInterface;
+
+class CollectionMerger implements DataProcessorInterface
+{
+    private $field;
+
+    public function __construct(FieldInterface $field) {
+        $this->field = $field;
+    }
+
+    public function processData($data)
+    {
+        $collection = $this->field->getData();
+
+        if (!$collection) {
+            $collection = $data;
+        } else if (count($data) === 0) {
+            $collection->clear();
+        } else {
+            // merge $data into $collection
+            foreach ($collection as $entity) {
+                if (!$data->contains($entity)) {
+                    $collection->removeElement($entity);
+                } else {
+                    $data->removeElement($entity);
+                }
+            }
+
+            foreach ($data as $entity) {
+                $collection->add($entity);
+            }
+        }
+
+        return $collection;
+    }
+}

+ 17 - 0
src/Symfony/Component/Form/DataProcessor/DataProcessorInterface.php

@@ -0,0 +1,17 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\DataProcessor;
+
+interface DataProcessorInterface
+{
+    function processData($data);
+}

+ 3 - 37
src/Symfony/Component/Form/EntityChoiceField.php

@@ -12,6 +12,7 @@
 namespace Symfony\Component\Form;
 
 use Symfony\Component\Form\ChoiceList\EntityChoiceList;
+use Symfony\Component\Form\DataProcessor\CollectionMerger;
 use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
 use Symfony\Component\Form\ValueTransformer\ValueTransformerChain;
 use Symfony\Component\Form\ValueTransformer\EntitiesToArrayTransformer;
@@ -96,6 +97,8 @@ class EntityChoiceField extends ChoiceField
         $transformers = array();
 
         if ($this->getOption('multiple')) {
+            $this->setDataProcessor(new CollectionMerger($this));
+
             $transformers[] = new EntitiesToArrayTransformer($this->choiceList);
 
             if ($this->getOption('expanded')) {
@@ -115,41 +118,4 @@ class EntityChoiceField extends ChoiceField
             $this->setValueTransformer(current($transformers));
         }
     }
-
-    /**
-     * Merges the selected and deselected entities into the collection passed
-     * when calling setData()
-     *
-     * @see parent::processData()
-     */
-    protected function processData($data)
-    {
-        // reuse the existing collection to optimize for Doctrine
-        if ($data instanceof Collection) {
-            $currentData = $this->getData();
-
-            if (!$currentData) {
-                $currentData = $data;
-            } else if (count($data) === 0) {
-                $currentData->clear();
-            } else {
-                // merge $data into $currentData
-                foreach ($currentData as $entity) {
-                    if (!$data->contains($entity)) {
-                        $currentData->removeElement($entity);
-                    } else {
-                        $data->removeElement($entity);
-                    }
-                }
-
-                foreach ($data as $entity) {
-                    $currentData->add($entity);
-                }
-            }
-
-            return $currentData;
-        }
-
-        return $data;
-    }
 }

+ 34 - 8
src/Symfony/Component/Form/Field.php

@@ -13,6 +13,7 @@ namespace Symfony\Component\Form;
 
 use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
 use Symfony\Component\Form\ValueTransformer\TransformationFailedException;
+use Symfony\Component\Form\DataProcessor\DataProcessorInterface;
 
 /**
  * Base class for form fields
@@ -50,15 +51,16 @@ class Field extends Configurable implements FieldInterface
 {
     private $errors = array();
     private $key = '';
-    private $parent = null;
+    private $parent;
     private $submitted = false;
-    private $required = null;
-    private $data = null;
-    private $normalizedData = null;
-    private $transformedData = null;
-    private $normalizationTransformer = null;
-    private $valueTransformer = null;
-    private $propertyPath = null;
+    private $required;
+    private $data;
+    private $normalizedData;
+    private $transformedData;
+    private $normalizationTransformer;
+    private $valueTransformer;
+    private $dataProcessor;
+    private $propertyPath;
     private $transformationSuccessful = true;
 
     public function __construct($key = null, array $options = array())
@@ -328,6 +330,10 @@ class Field extends Configurable implements FieldInterface
      */
     protected function processData($data)
     {
+        if ($this->dataProcessor) {
+            return $this->dataProcessor->processData($data);
+        }
+
         return $data;
     }
 
@@ -457,6 +463,26 @@ class Field extends Configurable implements FieldInterface
         return $this->valueTransformer;
     }
 
+    /**
+     * Sets the data processor
+     *
+     * @param DataProcessorInterface $dataProcessor
+     */
+    protected function setDataProcessor(DataProcessorInterface $dataProcessor)
+    {
+        $this->dataProcessor = $dataProcessor;
+    }
+
+    /**
+     * Returns the data processor
+     *
+     * @return DataProcessorInterface
+     */
+    protected function getDataProcessor()
+    {
+        return $this->dataProcessor;
+    }
+
     /**
      * Normalizes the value if a normalization transformer is set
      *