Kaynağa Gözat

Refactor callback, and some signatures

Thomas Rabaix 13 yıl önce
ebeveyn
işleme
f07c933786

+ 1 - 7
Builder/ORM/DatagridBuilder.php

@@ -105,16 +105,10 @@ class DatagridBuilder implements DatagridBuilderInterface
             $fieldDescription->setType($type);
         }
 
-        $options = array(
-            'options'       => $fieldDescription->getOption('options', array()),
-            'field_options' => $fieldDescription->getOption('field_options', array()),
-            'field_type'    => $fieldDescription->getOption('field_type', array())
-        );
-
         $this->fixFieldDescription($admin, $fieldDescription);
         $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
 
-        $filter = $this->filterFactory->create($fieldDescription, $options);
+        $filter = $this->filterFactory->create($fieldDescription);
 
         $datagrid->addFilter($filter);
 

+ 1 - 0
Filter/Filter.php

@@ -32,6 +32,7 @@ abstract class Filter implements FilterInterface
     {
         $this->name               = $fieldDescription->getName();
         $this->fieldDescription   = $fieldDescription;
+        $this->options            = $fieldDescription->getOptions();
     }
 
     public function initialize(array $options = array())

+ 2 - 4
Filter/FilterFactory.php

@@ -39,7 +39,7 @@ class FilterFactory implements FilterFactoryInterface
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
      * @return void
      */
-    public function create(FieldDescriptionInterface $fieldDescription, array $options = array())
+    public function create(FieldDescriptionInterface $fieldDescription)
     {
         if (!$fieldDescription->getType()) {
             throw new \RunTimeException('The type must be defined');
@@ -82,10 +82,8 @@ class FilterFactory implements FilterFactoryInterface
             throw new \RunTimeException(sprintf('The service `%s` must implement `FilterInterface`', $id));
         }
 
+        $fieldDescription->mergeOption('field_options', array('required' => false));
         $filter->setFieldDescription($fieldDescription);
-        $options['field_options']['required'] = false;
-
-        $filter->initialize($options);
         $filter->defineFieldBuilder($this->container->get('form.factory'));
 
         return $filter;

+ 1 - 1
Filter/FilterFactoryInterface.php

@@ -16,5 +16,5 @@ use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 
 interface FilterFactoryInterface
 {
-    public function create(FieldDescriptionInterface $fieldDescription, array $options = array());
+    public function create(FieldDescriptionInterface $fieldDescription);
 }

+ 5 - 24
Filter/ORM/CallbackFilter.php

@@ -36,40 +36,21 @@ class CallbackFilter extends Filter
      */
     public function filter($queryBuilder, $alias, $field, $value)
     {
-        if (!is_callable($this->getOption('filter'))) {
-            throw new \RuntimeException('Please provide a valid callback option "filter" for field "' . $this->getName() . "'");
+        if (!is_callable($this->getOption('callback'))) {
+            throw new \RuntimeException(sprintf('Please provide a valid callback option "filter" for field "%s"', $this->getName()));
         }
 
-        call_user_func($this->getOption('filter'), $queryBuilder, $alias, $field, $value);
+        call_user_func($this->getOption('callback'), $queryBuilder, $alias, $field, $value);
     }
 
     /**
-     *    $this->filter_fields['custom'] = array(
-     *        'type'           => 'callback',
-     *        'filter_options' => array(
-     *           'filter'  => array($this, 'getCustomFilter'),
-     *           'type'    => 'type_name'
-     *       )
-     *    );
-     *
      * @return array
      */
     public function getDefaultOptions()
     {
         return array(
-            'filter' => null,
-            'type'   => 'text',
+            'callback'    => null,
+            'field_type'  => 'text',
         );
     }
-
-    /**
-     * @param \Symfony\Component\Form\FormFactory $formFactory
-     * @return void
-     */
-    public function defineFieldBuilder(FormFactory $formFactory)
-    {
-        $options = $this->getFieldDescription()->getOption('filter_field_options', array());
-
-        $this->field = $formFactory->createNamedBuilder($this->getOption('type'), $this->getName(), null, $options)->getForm();
-    }
 }

+ 0 - 2
Guesser/ORM/FilterTypeGuesser.php

@@ -55,8 +55,6 @@ class FilterTypeGuesser implements TypeGuesserInterface
 
             switch ($mapping['type']) {
                 case ClassMetadataInfo::ONE_TO_MANY:
-                    return new TypeGuess('doctrine_orm_one_to_many', $options, Guess::HIGH_CONFIDENCE);
-
                 case ClassMetadataInfo::MANY_TO_MANY:
                     $options['field_type'] = 'entity';
                     $options['field_options'] = array(