|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Boduch\Grid\Filters; |
|
4
|
|
|
|
|
5
|
|
|
use Boduch\Grid\Column; |
|
6
|
|
|
use Illuminate\Foundation\Application; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Filter implements FilterInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $name; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $operator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Column |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $column; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $attr = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $options |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(array $options = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->setDefaultOptions($options); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @inheritdoc |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getColumn() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->column; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setColumn(Column $column) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->column = $column; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setOperator($operator) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->operator = $operator; |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritdoc |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getOperator() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->operator; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @inheritdoc |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setName(string $name) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->name = $name; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getName() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->name ?: $this->column->getName(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getAttr(): array |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->attr; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array $attr |
|
100
|
|
|
* @return $this |
|
101
|
|
|
*/ |
|
102
|
|
|
public function setAttr(array $attr) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->attr = $attr; |
|
105
|
|
|
|
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @inheritdoc |
|
111
|
|
|
*/ |
|
112
|
|
|
public function isEmpty() |
|
113
|
|
|
{ |
|
114
|
|
|
if (is_array($this->getInput())) { |
|
115
|
|
|
return empty(array_filter($this->getInput())); |
|
116
|
|
|
} else { |
|
117
|
|
|
return !$this->hasInput(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string|string[] |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getInput() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->getRequest()->input($this->normalizeName($this->getName())); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
abstract public function render(); |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param array $options |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function setDefaultOptions(array $options) |
|
135
|
|
|
{ |
|
136
|
|
|
foreach ($options as $key => $values) { |
|
137
|
|
|
$methodName = 'set' . ucfirst(camel_case($key)); |
|
138
|
|
|
|
|
139
|
|
|
if (method_exists($this, $methodName)) { |
|
140
|
|
|
$this->$methodName($values); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return \Collective\Html\HtmlBuilder |
|
147
|
|
|
*/ |
|
148
|
|
|
protected function getHtmlBuilder() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->column->getGrid()->getGridHelper()->getHtmlBuilder(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return \Collective\Html\FormBuilder |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getFormBuilder() |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->column->getGrid()->getGridHelper()->getFormBuilder(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return \Illuminate\Http\Request |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function getRequest() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->column->getGrid()->getGridHelper()->getRequest(); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return bool |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function hasInput() |
|
173
|
|
|
{ |
|
174
|
|
|
$method = version_compare(Application::VERSION, '5.5') === -1 ? 'has' : 'filled'; |
|
175
|
|
|
|
|
176
|
|
|
return $this->getRequest()->$method($this->normalizeName($this->getName())); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* HTTP POST or HTTP GET can't have dots in name. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $name |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function normalizeName($name) |
|
186
|
|
|
{ |
|
187
|
|
|
return str_replace('.', '_', $name); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|