Browse Source

Added a simple base mapper that holds some common fields

Dean de Bree 12 năm trước cách đây
mục cha
commit
065bcb7688
5 tập tin đã thay đổi với 64 bổ sung41 xóa
  1. 4 8
      Datagrid/DatagridMapper.php
  2. 4 8
      Datagrid/ListMapper.php
  3. 7 17
      Form/FormMapper.php
  4. 45 0
      Mapper/BaseMapper.php
  5. 4 8
      Show/ShowMapper.php

+ 4 - 8
Datagrid/DatagridMapper.php

@@ -14,19 +14,16 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Datagrid\DatagridInterface;
 use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
+use Sonata\AdminBundle\Mapper\BaseMapper;
 
 /**
  * This class is use to simulate the Form API
  *
  */
-class DatagridMapper
+class DatagridMapper extends BaseMapper
 {
-    protected $datagridBuilder;
-
     protected $datagrid;
 
-    protected $admin;
-
     /**
      * @param \Sonata\AdminBundle\Builder\DatagridBuilderInterface $datagridBuilder
      * @param DatagridInterface                                    $datagrid
@@ -34,9 +31,8 @@ class DatagridMapper
      */
     public function __construct(DatagridBuilderInterface $datagridBuilder, DatagridInterface $datagrid, AdminInterface $admin)
     {
-        $this->datagridBuilder = $datagridBuilder;
+        parent::__construct($datagridBuilder, $admin);
         $this->datagrid        = $datagrid;
-        $this->admin           = $admin;
     }
 
     /**
@@ -76,7 +72,7 @@ class DatagridMapper
         }
 
         // add the field with the DatagridBuilder
-        $this->datagridBuilder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
+        $this->builder->addFilter($this->datagrid, $type, $fieldDescription, $this->admin);
 
         return $this;
     }

+ 4 - 8
Datagrid/ListMapper.php

@@ -14,19 +14,16 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Builder\ListBuilderInterface;
+use Sonata\AdminBundle\Mapper\BaseMapper;
 
 /**
  * This class is used to simulate the Form API
  *
  */
-class ListMapper
+class ListMapper extends BaseMapper
 {
-    protected $listBuilder;
-
     protected $list;
 
-    protected $admin;
-
     /**
      * @param ListBuilderInterface       $listBuilder
      * @param FieldDescriptionCollection $list
@@ -34,9 +31,8 @@ class ListMapper
      */
     public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
     {
-        $this->listBuilder = $listBuilder;
+        parent::__construct($listBuilder, $admin);
         $this->list        = $list;
-        $this->admin       = $admin;
     }
 
     /**
@@ -90,7 +86,7 @@ class ListMapper
         }
 
         // add the field with the FormBuilder
-        $this->listBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
+        $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
 
         return $this;
     }

+ 7 - 17
Form/FormMapper.php

@@ -13,19 +13,16 @@ namespace Sonata\AdminBundle\Form;
 use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Admin\AdminInterface;
 use Symfony\Component\Form\FormBuilder;
+use Sonata\AdminBundle\Mapper\BaseMapper;
 
 /**
  * This class is use to simulate the Form API
  *
  */
-class FormMapper
+class FormMapper extends BaseMapper
 {
     protected $formBuilder;
 
-    protected $formContractor;
-
-    protected $admin;
-
     protected $currentGroup;
 
     /**
@@ -35,9 +32,8 @@ class FormMapper
      */
     public function __construct(FormContractorInterface $formContractor, FormBuilder $formBuilder, AdminInterface $admin)
     {
+        parent::__construct($formContractor, $admin);
         $this->formBuilder    = $formBuilder;
-        $this->formContractor = $formContractor;
-        $this->admin          = $admin;
     }
 
     /**
@@ -122,14 +118,16 @@ class FormMapper
             $fieldDescriptionOptions
         );
 
-        $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
+        //Note that the builder var is actually the formContractor:
+        $this->builder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
 
         $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
 
         if ($name instanceof FormBuilder) {
             $this->formBuilder->add($name);
         } else {
-            $options = array_replace_recursive($this->formContractor->getDefaultOptions($type, $fieldDescription), $options);
+            //Note that the builder var is actually the formContractor:
+            $options = array_replace_recursive($this->builder->getDefaultOptions($type, $fieldDescription), $options);
 
             if (!isset($options['label'])) {
                 $options['label'] = $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'form', 'label');
@@ -192,14 +190,6 @@ class FormMapper
         return $this->formBuilder;
     }
 
-    /**
-     * @return \Sonata\AdminBundle\Admin\AdminInterface
-     */
-    public function getAdmin()
-    {
-        return $this->admin;
-    }
-
     /**
      * @param string $name
      * @param mixed  $type

+ 45 - 0
Mapper/BaseMapper.php

@@ -0,0 +1,45 @@
+<?php
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+namespace Sonata\AdminBundle\Mapper;
+
+use Sonata\AdminBundle\Admin\AdminInterface;
+use Sonata\AdminBundle\Builder\BuilderInterface;
+
+/**
+ * This class is used to simulate the Form API
+ *
+ */
+class BaseMapper
+{
+
+    protected $admin;
+    
+    protected $builder;
+    
+    /**
+     * @param \Sonata\AdminBundle\Builder\BuilderInterface $builder
+     * @param \Sonata\AdminBundle\Admin\AdminInterface     $admin
+     */
+    public function __construct(BuilderInterface $builder, AdminInterface $admin)
+    {
+        $this->builder = $builder;
+        $this->admin   = $admin;
+    }
+    
+    /**
+     * @return \Sonata\AdminBundle\Admin\AdminInterface
+     */
+    public function getAdmin()
+    {
+        return $this->admin;
+    }
+    
+}

+ 4 - 8
Show/ShowMapper.php

@@ -14,19 +14,16 @@ use Sonata\AdminBundle\Admin\AdminInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
 use Sonata\AdminBundle\Builder\ShowBuilderInterface;
+use Sonata\AdminBundle\Mapper\BaseMapper;
 
 /**
  * This class is used to simulate the Form API
  *
  */
-class ShowMapper
+class ShowMapper extends BaseMapper
 {
-    protected $showBuilder;
-
     protected $list;
 
-    protected $admin;
-
     protected $currentGroup;
 
     /**
@@ -36,9 +33,8 @@ class ShowMapper
      */
     public function __construct(ShowBuilderInterface $showBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
     {
-        $this->showBuilder = $showBuilder;
+        parent::__construct($showBuilder, $admin);
         $this->list        = $list;
-        $this->admin       = $admin;
     }
 
     /**
@@ -82,7 +78,7 @@ class ShowMapper
         $fieldDescription->setOption('safe', $fieldDescription->getOption('safe', false));
 
         // add the field with the FormBuilder
-        $this->showBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
+        $this->builder->addField($this->list, $type, $fieldDescription, $this->admin);
 
         return $this;
     }