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