Completed
Push — master ( 3c1bd9...d781ee )
by wen
12:17
created

Filter::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\View\Filters;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sco\Admin\Contracts\View\Filters\FilterInterface;
7
8
abstract class Filter implements FilterInterface
9
{
10
    protected $type;
11
12
    protected $value;
13
14
    protected $name;
15
16
    protected $title;
17
18
    protected $defaultValue;
19
20
    protected $operator = '=';
21
22
    /**
23
     * Apply filter to the query.
24
     *
25
     * @param Builder $query
26
     */
27
    public function apply(Builder $query)
28
    {
29
        $query->where($this->getName(), $this->getOperator(), $this->getValue());
30
    }
31
32
    public function __construct($name, $title)
33
    {
34
        $this->setName($name)->setTitle($title);
35
    }
36
37
    public function initialize()
38
    {
39
        if (is_null($value = $this->getValue())) {
40
            $value = request()->input($this->getName());
41
        }
42
43
        $this->setValue($value);
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getValue()
50
    {
51
        if (is_null($this->value)) {
52
            return $this->getDefaultValue();
53
        }
54
        return $this->value;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function setValue($value)
61
    {
62
        $this->value = $value;
63
64
        return $this;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getName()
71
    {
72
        return $this->name;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function setName($name)
79
    {
80
        $this->name = $name;
81
82
        return $this;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function getTitle()
89
    {
90
        return $this->title;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function setTitle($title)
97
    {
98
        $this->title = $title;
99
100
        return $this;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getDefaultValue()
107
    {
108
        return $this->defaultValue;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function setDefaultValue($defaultValue)
115
    {
116
        $this->defaultValue = $defaultValue;
117
118
        return $this;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getOperator()
125
    {
126
        return $this->operator;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function setOperator(string $operator)
133
    {
134
        $this->operator = $operator;
135
136
        return $this;
137
    }
138
139
    public function isActive()
140
    {
141
        return !is_null($this->value);
142
    }
143
144
    public function toArray()
145
    {
146
        return [
147
            'name'  => $this->getName(),
148
            'title' => $this->getTitle(),
149
            'type'  => $this->type,
150
        ];
151
    }
152
153
    public function jsonSerialize()
154
    {
155
        return $this->toArray();
156
    }
157
158
    public function toJson($options = 0)
159
    {
160
        return json_encode($this->jsonSerialize(), $options);
161
    }
162
163
    public function __toString()
164
    {
165
        return $this->toJson();
166
    }
167
}
168