Parcourir la source

增加Grid\Filter::whereBetween

jqh il y a 4 ans
Parent
commit
8e3d40b75f
2 fichiers modifiés avec 109 ajouts et 42 suppressions
  1. 45 42
      src/Grid/Filter.php
  2. 64 0
      src/Grid/Filter/WhereBetween.php

+ 45 - 42
src/Grid/Filter.php

@@ -26,6 +26,7 @@ use Dcat\Admin\Grid\Filter\NotIn;
 use Dcat\Admin\Grid\Filter\Scope;
 use Dcat\Admin\Grid\Filter\StartWith;
 use Dcat\Admin\Grid\Filter\Where;
+use Dcat\Admin\Grid\Filter\WhereBetween;
 use Dcat\Admin\Grid\Filter\Year;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Traits\HasBuilderEvents;
@@ -37,27 +38,28 @@ use Illuminate\Support\Str;
 /**
  * Class Filter.
  *
- * @method Equal     equal($column, $label = '')
- * @method NotEqual  notEqual($column, $label = '')
- * @method Like      like($column, $label = '')
- * @method Ilike     ilike($column, $label = '')
- * @method StartWith startWith($column, $label = '')
- * @method EndWith   endWith($column, $label = '')
- * @method Gt        gt($column, $label = '')
- * @method Lt        lt($column, $label = '')
- * @method Ngt       ngt($column, $label = '')
- * @method Nlt       nlt($column, $label = '')
- * @method Between   between($column, $label = '')
- * @method In        in($column, $label = '')
- * @method NotIn     notIn($column, $label = '')
- * @method Where     where($colum, $callback, $label = '')
- * @method Date      date($column, $label = '')
- * @method Day       day($column, $label = '')
- * @method Month     month($column, $label = '')
- * @method Year      year($column, $label = '')
- * @method Hidden    hidden($name, $value)
- * @method Group     group($column, $builder = null, $label = '')
- * @method Newline   newline()
+ * @method Equal        equal($column, $label = '')
+ * @method NotEqual     notEqual($column, $label = '')
+ * @method Like         like($column, $label = '')
+ * @method Ilike        ilike($column, $label = '')
+ * @method StartWith    startWith($column, $label = '')
+ * @method EndWith      endWith($column, $label = '')
+ * @method Gt           gt($column, $label = '')
+ * @method Lt           lt($column, $label = '')
+ * @method Ngt          ngt($column, $label = '')
+ * @method Nlt          nlt($column, $label = '')
+ * @method Between      between($column, $label = '')
+ * @method In           in($column, $label = '')
+ * @method NotIn        notIn($column, $label = '')
+ * @method Where        where($colum, $callback, $label = '')
+ * @method WhereBetween whereBetween($colum, $callback, $label = '')
+ * @method Date         date($column, $label = '')
+ * @method Day          day($column, $label = '')
+ * @method Month        month($column, $label = '')
+ * @method Year         year($column, $label = '')
+ * @method Hidden       hidden($name, $value)
+ * @method Group        group($column, $builder = null, $label = '')
+ * @method Newline      newline()
  */
 class Filter implements Renderable
 {
@@ -75,27 +77,28 @@ class Filter implements Renderable
      * @var array
      */
     protected static $defaultFilters = [
-        'equal'     => Equal::class,
-        'notEqual'  => NotEqual::class,
-        'ilike'     => Ilike::class,
-        'like'      => Like::class,
-        'startWith' => StartWith::class,
-        'endWith'   => EndWith::class,
-        'gt'        => Gt::class,
-        'lt'        => Lt::class,
-        'ngt'       => Ngt::class,
-        'nlt'       => Nlt::class,
-        'between'   => Between::class,
-        'group'     => Group::class,
-        'where'     => Where::class,
-        'in'        => In::class,
-        'notIn'     => NotIn::class,
-        'date'      => Date::class,
-        'day'       => Day::class,
-        'month'     => Month::class,
-        'year'      => Year::class,
-        'hidden'    => Hidden::class,
-        'newline'   => Newline::class,
+        'equal'        => Equal::class,
+        'notEqual'     => NotEqual::class,
+        'ilike'        => Ilike::class,
+        'like'         => Like::class,
+        'startWith'    => StartWith::class,
+        'endWith'      => EndWith::class,
+        'gt'           => Gt::class,
+        'lt'           => Lt::class,
+        'ngt'          => Ngt::class,
+        'nlt'          => Nlt::class,
+        'between'      => Between::class,
+        'group'        => Group::class,
+        'where'        => Where::class,
+        'whereBetween' => WhereBetween::class,
+        'in'           => In::class,
+        'notIn'        => NotIn::class,
+        'date'         => Date::class,
+        'day'          => Day::class,
+        'month'        => Month::class,
+        'year'         => Year::class,
+        'hidden'       => Hidden::class,
+        'newline'      => Newline::class,
     ];
 
     /**

+ 64 - 0
src/Grid/Filter/WhereBetween.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace Dcat\Admin\Grid\Filter;
+
+use Illuminate\Support\Arr;
+
+class WhereBetween extends Between
+{
+    /**
+     * {@inheritdoc}
+     */
+    protected $view = 'admin::filter.between';
+
+    /**
+     * Query closure.
+     *
+     * @var \Closure
+     */
+    protected $where;
+
+    /**
+     * Input value from presenter.
+     *
+     * @var mixed
+     */
+    public $input;
+
+    /**
+     * Where constructor.
+     *
+     * @param string   $column
+     * @param \Closure $query
+     * @param string   $label
+     */
+    public function __construct($column, \Closure $query, $label = '')
+    {
+        $this->where = $query;
+        $this->column = $column;
+        $this->label = $this->formatLabel($label);
+    }
+
+    /**
+     * Get condition of this filter.
+     *
+     * @param array $inputs
+     *
+     * @return array|mixed|void
+     */
+    public function condition($inputs)
+    {
+        $value = Arr::get($inputs, $this->column) ?: [];
+
+        if (
+            ! $value
+            || (! isset($value['start']) && ! isset($value['end']))
+        ) {
+            return;
+        }
+
+        $this->input = $this->value = $value;
+
+        return $this->buildCondition($this->where->bindTo($this));
+    }
+}