Completed
Push — master ( 5fc65a...affc4f )
by Song
02:25
created

RangeFilter::addBinding()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Grid\Column;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Model;
7
8
class RangeFilter extends Filter
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $type;
14
15
    /**
16
     * RangeFilter constructor.
17
     *
18
     * @param string $type
19
     */
20
    public function __construct($type)
21
    {
22
        $this->type  = $type;
23
        $this->class = [
24
            'start' => uniqid('column-filter-start-'),
25
            'end'   => uniqid('column-filter-end-'),
26
        ];
27
    }
28
29
    /**
30
     * Add a binding to the query.
31
     *
32
     * @param mixed $value
33
     *
34
     * @param Model $model
35
     */
36
    public function addBinding($value, Model $model)
37
    {
38
        $value = array_filter((array)$value);
39
40
        if (empty($value)) {
41
            return;
42
        }
43
44
        if (!isset($value['start'])) {
45
            return $model->where($this->getColumnName(), '<', $value['end']);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
46
        } elseif (!isset($value['end'])) {
47
            return $model->where($this->getColumnName(), '>', $value['start']);
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
        } else {
49
            return $model->whereBetween($this->getColumnName(), array_values($value));
0 ignored issues
show
Documentation Bug introduced by
The method whereBetween does not exist on object<Encore\Admin\Grid\Model>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
        }
51
    }
52
53
    protected function addScript()
54
    {
55
        $options = [
56
            'locale'           => config('app.locale'),
57
            'allowInputToggle' => true,
58
        ];
59
60 View Code Duplication
        if ($this->type == 'date') {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            $options['format'] = 'YYYY-MM-DD';
62
        } elseif ($this->type == 'time') {
63
            $options['format'] = 'HH:mm:ss';
64
        } elseif ($this->type == 'datetime') {
65
            $options['format'] = 'YYYY-MM-DD HH:mm:ss';
66
        } else {
67
            return;
68
        }
69
70
        $options = json_encode($options);
71
72
        Admin::script("$('.{$this->class['start']},.{$this->class['end']}').datetimepicker($options);");
73
    }
74
75
    /**
76
     * Render this filter.
77
     *
78
     * @return string
79
     */
80
    public function render()
81
    {
82
        $script = <<<SCRIPT
83
$('.dropdown-menu input').click(function(e) {
84
    e.stopPropagation();
85
});
86
SCRIPT;
87
88
        Admin::script($script);
89
90
        $this->addScript();
91
92
        $value  = $this->getFilterValue(['start' => '', 'end' => '']);
0 ignored issues
show
Documentation introduced by
array('start' => '', 'end' => '') is of type array<string,string,{"st...tring","end":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
93
        $active = empty(array_filter($value)) ? '' : 'text-yellow';
94
95
        return <<<EOT
96
<span class="dropdown">
97
<form action="{$this->getFormAction()}" pjax-container style="display: inline-block;">
98
    <a href="javascript:void(0);" class="dropdown-toggle {$active}" data-toggle="dropdown">
99
        <i class="fa fa-filter"></i>
100
    </a>
101
    <ul class="dropdown-menu" role="menu" style="padding: 10px;box-shadow: 0 2px 3px 0 rgba(0,0,0,.2);left: -70px;border-radius: 0;">
102
        <li>
103
            <input type="text" class="form-control input-sm {$this->class['start']}" name="{$this->getColumnName()}[start]" value="{$value['start']}" autocomplete="off"/>
104
        </li>
105
        <li style="margin: 5px;"></li>
106
        <li>
107
            <input type="text" class="form-control input-sm {$this->class['start']}" name="{$this->getColumnName()}[end]"  value="{$value['end']}" autocomplete="off"/>
108
        </li>
109
        <li class="divider"></li>
110
        <li class="text-right">
111
            <button class="btn btn-sm btn-primary btn-flat column-filter-submit pull-left">{$this->trans('submit')}</button>
112
            <button class="btn btn-sm btn-default btn-flat column-filter-all">{$this->trans('reset')}</button>
113
        </li>
114
    </ul>
115
    </form>
116
</span>
117
EOT;
118
    }
119
}