Completed
Push — master ( 7e212d...676b8c )
by wen
12:32
created

Filter::setValueFromRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
namespace Sco\Admin\Display\Filters;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Sco\Admin\Contracts\Display\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
    public function __construct($name, $title)
23
    {
24
        $this->setName($name)->setTitle($title);
25
        $this->setValueFromRequest();
26
    }
27
28
    protected function setValueFromRequest()
29
    {
30
        if (!is_null($value = $this->getRequestInputValue())) {
31
            $this->setValue($value);
32
        }
33
    }
34
35
    protected function getRequestInputValue()
36
    {
37
        $name = $this->getRequestName();
38
39
        return request()->input($name);
40
    }
41
42
    /**
43
     * Dots and spaces in variable names are converted to underscores.
44
     * For example <input name="a.b" /> becomes $_REQUEST["a_b"].
45
     *
46
     * @see http://php.net/manual/en/language.variables.external.php
47
     *
48
     * @return mixed
49
     */
50
    protected function getRequestName()
51
    {
52
        return str_replace('.', '_', $this->getName());
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function apply(Builder $query)
59
    {
60
        $name = $this->getName();
61
        if (strpos($name, '.') !== false) {
62
            list($relation, $name) = explode('.', $name, 2);
63
            $query->whereHas($relation, function ($q) use ($name) {
64
                $this->buildQuery($q, $name);
65
            });
66
        } else {
67
            $this->buildQuery($query, $this->getName());
68
        }
69
    }
70
71
    /**
72
     * Build the filter query.
73
     *
74
     * @param \Illuminate\Database\Eloquent\Builder $query
75
     * @param string $name
76
     */
77
    protected function buildQuery(Builder $query, $name)
78
    {
79
        $op = $this->getOperator();
80
        $value = $this->getValue();
81
82
        switch ($op) {
83
            case 'in':
84
                $query->whereIn($name, (array) $value);
85
                break;
86
            case 'between':
87
                $query->whereBetween($name, (array) $value);
88
                break;
89
            case 'like':
90
                $value .= '%';
91
                $query->where($name, $op, $value);
92
                break;
93
            default:
94
                $query->where($name, $op, $value);
95
        }
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getValue()
102
    {
103
        return $this->value;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getDisplayValue()
110
    {
111
        $value = $this->getValue();
112
        if (is_null($value)) {
113
            return $this->getDefaultValue();
114
        }
115
116
        return $value;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function setValue($value)
123
    {
124
        $this->value = $value;
125
126
        return $this;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function getName()
133
    {
134
        return $this->name;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function setName($name)
141
    {
142
        $this->name = $name;
143
144
        return $this;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getTitle()
151
    {
152
        return $this->title;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function setTitle($title)
159
    {
160
        $this->title = $title;
161
162
        return $this;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getDefaultValue()
169
    {
170
        return $this->defaultValue;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function setDefaultValue($defaultValue)
177
    {
178
        $this->defaultValue = $defaultValue;
179
180
        return $this;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function getOperator()
187
    {
188
        return $this->operator;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function setOperator(string $operator)
195
    {
196
        $this->operator = $operator;
197
198
        return $this;
199
    }
200
201
    public function isActive()
202
    {
203
        return ! is_null($this->value);
204
    }
205
206
    public function toArray()
207
    {
208
        return [
209
            'name'  => $this->getName(),
210
            'title' => $this->getTitle(),
211
            'type'  => $this->type,
212
        ];
213
    }
214
215
    public function jsonSerialize()
216
    {
217
        return $this->toArray();
218
    }
219
220
    public function toJson($options = 0)
221
    {
222
        return json_encode($this->jsonSerialize(), $options);
223
    }
224
225
    public function __toString()
226
    {
227
        return $this->toJson();
228
    }
229
}
230