Browse Source

Form::json 改为 Form::array;增加 saveAsJson、saveAsString 等方法

jqh 5 years ago
parent
commit
8ffa36c753
6 changed files with 49 additions and 20 deletions
  1. 2 2
      src/Form.php
  2. 1 1
      src/Form/EmbeddedForm.php
  3. 43 7
      src/Form/Field.php
  4. 1 8
      src/Form/Field/ArrayField.php
  5. 1 1
      src/Form/Field/Table.php
  6. 1 1
      src/Widgets/Form.php

+ 2 - 2
src/Form.php

@@ -82,7 +82,7 @@ use Symfony\Component\HttpFoundation\Response;
  * @method Field\Markdown               markdown($column, $label = '')
  * @method Field\Range                  range($start, $end, $label = '')
  * @method Field\Color                  color($column, $label = '')
- * @method Field\Json                   json($column, $labelOrCallback, $callback = null)
+ * @method Field\ArrayField             array($column, $labelOrCallback, $callback = null)
  */
 class Form implements Renderable
 {
@@ -162,7 +162,7 @@ class Form implements Renderable
         'markdown'       => Field\Markdown::class,
         'range'          => Field\Range::class,
         'color'          => Field\Color::class,
-        'json'           => Field\Json::class,
+        'array'          => Field\ArrayField::class,
     ];
 
     /**

+ 1 - 1
src/Form/EmbeddedForm.php

@@ -60,7 +60,7 @@ use Illuminate\Support\Collection;
  * @method Field\Markdown               markdown($column, $label = '')
  * @method Field\Range                  range($start, $end, $label = '')
  * @method Field\Color                  color($column, $label = '')
- * @method Field\Json                   json($column, $labelOrCallback, $callback = null)
+ * @method Field\ArrayField             array($column, $labelOrCallback, $callback = null)
  */
 class EmbeddedForm
 {

+ 43 - 7
src/Form/Field.php

@@ -209,9 +209,9 @@ class Field implements Renderable
     protected $labelClass = [];
 
     /**
-     * @var \Closure
+     * @var \Closure[]
      */
-    protected $savingCallback;
+    protected $savingCallbacks = [];
 
     /**
      * Field constructor.
@@ -847,7 +847,7 @@ class Field implements Renderable
      */
     public function saving(\Closure $closure)
     {
-        $this->savingCallback = $closure;
+        $this->savingCallbacks[] = $closure;
 
         return $this;
     }
@@ -863,10 +863,10 @@ class Field implements Renderable
     {
         $value = $this->prepareInputValue($value);
 
-        if ($handler = $this->savingCallback) {
-            $handler->bindTo($this->data());
-
-            return $handler($value);
+        if ($this->savingCallbacks) {
+            foreach ($this->savingCallbacks as $callback) {
+                $value = $callback->call($this->data());
+            }
         }
 
         return $value;
@@ -1193,6 +1193,42 @@ class Field implements Renderable
         return $this->display;
     }
 
+    public function saveAsJson($option = 0)
+    {
+        return $this->saving(function ($value) use ($option) {
+            if (! $value || is_scalar($value)) {
+                return $value;
+            }
+
+            return json_encode($value, $option);
+        });
+    }
+
+    public function saveAsJoin(string $glue = ',')
+    {
+        return $this->saving(function ($value) use ($glue) {
+            if (! $value || is_scalar($value)) {
+                return $value;
+            }
+
+            return implode($glue, (array) $value);
+        });
+    }
+
+    public function saveAsString()
+    {
+        return $this->saving(function ($value) {
+            return (string) $value;
+        });
+    }
+
+    public function saveAsInteger()
+    {
+        return $this->saving(function ($value) {
+            return (int) $value;
+        });
+    }
+
     /**
      * Collect assets required by this field.
      */

+ 1 - 8
src/Form/Field/Json.php → src/Form/Field/ArrayField.php

@@ -5,7 +5,7 @@ namespace Dcat\Admin\Form\Field;
 use Dcat\Admin\Form\NestedForm;
 use Dcat\Admin\Support\Helper;
 
-class Json extends HasMany
+class ArrayField extends HasMany
 {
     public function __construct($column, $arguments = [])
     {
@@ -85,11 +85,4 @@ class Json extends HasMany
 
         return $form;
     }
-
-    public function saveAsJson()
-    {
-        return $this->saving(function ($v) {
-            return json_encode($v);
-        });
-    }
 }

+ 1 - 1
src/Form/Field/Table.php

@@ -4,7 +4,7 @@ namespace Dcat\Admin\Form\Field;
 
 use Dcat\Admin\Admin;
 
-class Table extends Json
+class Table extends ArrayField
 {
     /**
      * @var string

+ 1 - 1
src/Widgets/Form.php

@@ -79,7 +79,7 @@ use Illuminate\Validation\Validator;
  * @method Field\Markdown       markdown($column, $label = '')
  * @method Field\Range          range($start, $end, $label = '')
  * @method Field\Color          color($column, $label = '')
- * @method Field\Json           json($column, $labelOrCallback, $callback = null)
+ * @method Field\ArrayField     array($column, $labelOrCallback, $callback = null)
  */
 class Form implements Renderable
 {