瀏覽代碼

Merge pull request #1802 from pianzhou2021/upgrade-has-fields

剥离hasFields
Jiang Qinghua 2 年之前
父節點
當前提交
7f73ac2e13

+ 24 - 0
src/Contracts/FieldsCollection.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Dcat\Admin\Contracts;
+
+use Dcat\Admin\Form\Field;
+use Illuminate\Support\Collection;
+
+interface FieldsCollection
+{
+    /**
+     * Get fields of this builder.
+     *
+     * @return Collection
+     */
+    public function fields();
+
+    /**
+     * Get specify field.
+     *
+     * @param  string|Field  $name
+     * @return Field|null
+     */
+    public function field($name);
+}

+ 8 - 61
src/Form/Builder.php

@@ -4,24 +4,26 @@ namespace Dcat\Admin\Form;
 
 use Closure;
 use Dcat\Admin\Admin;
+use Dcat\Admin\Contracts\FieldsCollection;
 use Dcat\Admin\Contracts\UploadField;
 use Dcat\Admin\Form;
+use Dcat\Admin\Form\Concerns\HasFields;
 use Dcat\Admin\Form\Field\Hidden;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasVariables;
 use Dcat\Admin\Widgets\DialogForm;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Arr;
-use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\URL;
 use Illuminate\Support\Str;
 
 /**
  * Class Builder.
  */
-class Builder
+class Builder implements FieldsCollection
 {
     use HasVariables;
+    use HasFields;
 
     /**
      *  上个页面URL保存的key.
@@ -50,11 +52,6 @@ class Builder
      */
     protected $action;
 
-    /**
-     * @var Collection|Field[]
-     */
-    protected $fields;
-
     /**
      * @var array
      */
@@ -144,7 +141,6 @@ class Builder
     public function __construct(Form $form)
     {
         $this->form = $form;
-        $this->fields = new Collection();
         $this->layout = new Layout($form);
         $this->tools = new Tools($this);
         $this->footer = new Footer($this);
@@ -400,48 +396,6 @@ class Builder
         return '';
     }
 
-    /**
-     * Get fields of this builder.
-     *
-     * @return Collection
-     */
-    public function fields()
-    {
-        return $this->fields;
-    }
-
-    /**
-     * Get specify field.
-     *
-     * @param  string|Field  $name
-     * @return Field|null
-     */
-    public function field($name)
-    {
-        return $this->fields->first(function (Field $field) use ($name) {
-            if (is_array($field->column())) {
-                $result = in_array($name, $field->column(), true) || $field->column() === $name ? $field : null;
-
-                if ($result) {
-                    return $result;
-                }
-            }
-
-            return $field === $name || $field->column() === $name;
-        });
-    }
-
-    /**
-     * @param $column
-     * @return void
-     */
-    public function removeField($column)
-    {
-        $this->fields = $this->fields->filter(function (Field $field) use ($column) {
-            return $field->column() != $column;
-        });
-    }
-
     /**
      * If the parant form has rows.
      *
@@ -553,13 +507,6 @@ class Builder
         return $this->elementId ?: ($this->elementId = 'form-'.Str::random(8));
     }
 
-    public function pushField(Field $field)
-    {
-        $this->fields->push($field);
-
-        return $this;
-    }
-
     /**
      * Determine if form fields has files.
      *
@@ -642,7 +589,7 @@ class Builder
     public function close()
     {
         $this->form = null;
-        $this->fields = null;
+        $this->resetFields();
 
         return '</form>';
     }
@@ -654,7 +601,7 @@ class Builder
      */
     protected function removeIgnoreFields()
     {
-        $this->fields = $this->fields()->reject(function (Field $field) {
+        $this->rejectFields(function (Field $field) {
             return $field->hasAttribute(Field::BUILD_IGNORE);
         });
     }
@@ -692,7 +639,7 @@ class Builder
             }
         };
 
-        $this->fields = $this->fields()->reject($reject);
+        $this->rejectFields($reject);
 
         if ($this->form->hasTab()) {
             $this->form->getTab()->getTabs()->transform(function ($item) use ($reject) {
@@ -737,7 +684,7 @@ class Builder
             'width'      => $this->width,
             'elementId'  => $this->getElementId(),
             'showHeader' => $this->showHeader,
-            'fields'     => $this->fields,
+            'fields'     => $this->fields(),
             'rows'       => $this->rows(),
             'layout'     => $this->layout(),
         ];

+ 134 - 0
src/Form/Concerns/HasFields.php

@@ -0,0 +1,134 @@
+<?php
+/*
+ * This file is part of the Dcat package.
+ *
+ * (c) Pian Zhou <pianzhou2021@163.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Dcat\Admin\Form\Concerns;
+
+use Dcat\Admin\Contracts\FieldsCollection;
+use Dcat\Admin\Form\Field;
+use Illuminate\Support\Collection;
+
+trait HasFields
+{
+    /**
+     * @var Collection|Field[]
+     */
+    private $fields;
+
+    /**
+     * Get fields of this builder.
+     *
+     * @return Collection
+     */
+    public function fields()
+    {
+        if (! $this->fields) {
+            $this->resetFields();
+        }
+
+        return $this->fields;
+    }
+
+    /**
+     * Get specify field.
+     *
+     * @param  string|Field  $name
+     * @return Field|null
+     */
+    public function field($name)
+    {
+        return $this->fields()->first(function (Field $field) use ($name) {
+            if (is_array($field->column())) {
+                $result = in_array($name, $field->column(), true) || $field->column() === $name ? $field : null;
+
+                if ($result) {
+                    return $result;
+                }
+            }
+
+            return $field === $name || $field->column() === $name;
+        });
+    }
+
+    /**
+     * Remove Field.
+     *
+     * @param $column
+     * @return void
+     */
+    public function removeField($column)
+    {
+        $this->fields = $this->fields()->filter(function (Field $field) use ($column) {
+            return $field->column() != $column;
+        });
+    }
+
+    /**
+     * Push Field.
+     *
+     * @param  Field  $field
+     * @return Collection
+     */
+    public function pushField(Field $field)
+    {
+        $this->fields()->push($field);
+    }
+
+    /**
+     * Reset Fields.
+     *
+     * @return void
+     */
+    public function resetFields()
+    {
+        $this->fields = new Collection();
+    }
+
+    /**
+     * Reject Fields.
+     *
+     * @param [type] $reject
+     * @return void
+     */
+    public function rejectFields($reject)
+    {
+        $this->fields = $this->fields()->reject($reject);
+    }
+
+    /**
+     * Set Fields.
+     *
+     * @param  Collection  $fields
+     * @return void
+     */
+    public function setFields(Collection $fields)
+    {
+        $this->fields = $fields;
+    }
+
+    /**
+     * Get all merged fields.
+     *
+     * @return array
+     */
+    protected function mergedFields()
+    {
+        $fields = [];
+        foreach ($this->fields() as $field) {
+            if ($field instanceof FieldsCollection) {
+                /** @var Field $field */
+                $fields = array_merge($fields, $field->mergedFields());
+            } else {
+                $fields[] = $field;
+            }
+        }
+
+        return $fields;
+    }
+}

+ 4 - 5
src/Form/Concerns/HasFiles.php

@@ -2,10 +2,10 @@
 
 namespace Dcat\Admin\Form\Concerns;
 
+use Dcat\Admin\Contracts\FieldsCollection;
 use Dcat\Admin\Contracts\UploadField as UploadFieldInterface;
 use Dcat\Admin\Form\Builder;
 use Dcat\Admin\Form\Field;
-use Dcat\Admin\Form\Field\Embeds;
 use Dcat\Admin\Form\NestedForm;
 use Dcat\Admin\Support\WebUploader;
 use Illuminate\Support\Arr;
@@ -71,11 +71,10 @@ trait HasFiles
         }
 
         $columns = explode('.', $column);
-        $field = $this->builder->field($columns[0]);
-        unset($columns[0]);
+        $field = $this->builder;
         foreach ($columns as $column) {
-            if ($field instanceof Embeds) {
-                $field = $field->findFieldByName($column);
+            if ($field instanceof FieldsCollection) {
+                $field = $field->field($column);
             }
         }
 

+ 1 - 1
src/Form/Field.php

@@ -307,7 +307,7 @@ class Field implements Renderable
      * @param  array  $data
      * @return void
      */
-    final public function fill($data)
+    public function fill($data)
     {
         $data = Helper::array($data);
 

+ 13 - 2
src/Form/Field/Embeds.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Form\Field;
 
+use Dcat\Admin\Contracts\FieldsCollection;
 use Dcat\Admin\Form\EmbeddedForm;
 use Dcat\Admin\Form\Field;
 use Dcat\Admin\Form\ResolveField;
@@ -10,7 +11,7 @@ use Illuminate\Support\Arr;
 use Illuminate\Support\Facades\Validator;
 use Illuminate\Support\Str;
 
-class Embeds extends Field
+class Embeds extends Field implements FieldsCollection
 {
     use ResolveField;
 
@@ -275,10 +276,20 @@ class Embeds extends Field
      * @param  string  $column
      * @return Field|null
      */
-    public function findFieldByName($name)
+    public function field($name)
     {
         return $this->buildEmbeddedForm()->fields()->first(function (Field $field) use ($name) {
             return $field->column() == $name;
         });
     }
+
+    /**
+     * 获取所有字段.
+     *
+     * @return void
+     */
+    public function fields()
+    {
+        return $this->buildEmbeddedForm()->fields();
+    }
 }