1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Boduch\Grid; |
4
|
|
|
|
5
|
|
|
use Boduch\Grid\Decorators\DecoratorInterface; |
6
|
|
|
use Boduch\Grid\Decorators\Link; |
7
|
|
|
use Boduch\Grid\Decorators\Html; |
8
|
|
|
use Boduch\Grid\Decorators\Placeholder; |
9
|
|
|
use Boduch\Grid\Filters\FilterInterface; |
10
|
|
|
|
11
|
|
|
class Column |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Grid |
15
|
|
|
*/ |
16
|
|
|
protected $grid; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $title; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $sortable = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DecoratorInterface[] |
35
|
|
|
*/ |
36
|
|
|
protected $decorators = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var FilterInterface |
40
|
|
|
*/ |
41
|
|
|
protected $filter; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $placeholder; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Escape value to prevent XSS. |
50
|
|
|
* |
51
|
|
|
* @var bool |
52
|
|
|
*/ |
53
|
|
|
protected $autoescape = true; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $options |
57
|
|
|
*/ |
58
|
|
|
public function __construct(array $options = []) |
59
|
|
|
{ |
60
|
|
|
$this->setDefaultOptions($options); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Grid $grid |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setGrid($grid) |
68
|
|
|
{ |
69
|
|
|
$this->grid = $grid; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return Grid |
76
|
|
|
*/ |
77
|
|
|
public function getGrid() |
78
|
|
|
{ |
79
|
|
|
return $this->grid; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getTitle() |
86
|
|
|
{ |
87
|
|
|
return $this->title; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $title |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setTitle($title) |
95
|
|
|
{ |
96
|
|
|
$this->title = $title; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getName() |
105
|
|
|
{ |
106
|
|
|
return $this->name; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $name |
111
|
|
|
* @return $this |
112
|
|
|
*/ |
113
|
|
|
public function setName($name) |
114
|
|
|
{ |
115
|
|
|
$this->name = $name; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return boolean |
122
|
|
|
*/ |
123
|
|
|
public function isSortable() |
124
|
|
|
{ |
125
|
|
|
return $this->sortable; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param boolean $flag |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
public function setSortable($flag) |
133
|
|
|
{ |
134
|
|
|
$this->sortable = (bool) $flag; |
135
|
|
|
|
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param \Closure $closure |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setClickable(\Closure $closure) |
144
|
|
|
{ |
145
|
|
|
$this->addDecorator((new Link())->render($closure)); |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param \Closure $closure |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function setRender(\Closure $closure) |
155
|
|
|
{ |
156
|
|
|
$this->addDecorator((new Html())->render($closure)); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $placeholder |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function setPlaceholder($placeholder) |
166
|
|
|
{ |
167
|
|
|
$this->addDecorator(new Placeholder($placeholder)); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param DecoratorInterface $decorator |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function addDecorator(DecoratorInterface $decorator) |
177
|
|
|
{ |
178
|
|
|
$this->decorators[] = $decorator; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param DecoratorInterface[] $decorators |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setDecorators(array $decorators) |
188
|
|
|
{ |
189
|
|
|
$this->decorators = $decorators; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return DecoratorInterface[] |
196
|
|
|
*/ |
197
|
|
|
public function getDecorators() |
198
|
|
|
{ |
199
|
|
|
return $this->decorators; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param FilterInterface $filter |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function setFilter(FilterInterface $filter) |
207
|
|
|
{ |
208
|
|
|
$filter->setColumn($this); |
209
|
|
|
$this->filter = $filter; |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return FilterInterface |
216
|
|
|
*/ |
217
|
|
|
public function getFilter() |
218
|
|
|
{ |
219
|
|
|
return $this->filter; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return bool |
224
|
|
|
*/ |
225
|
|
|
public function isFilterable() |
226
|
|
|
{ |
227
|
|
|
return $this->filter !== null; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return boolean |
232
|
|
|
*/ |
233
|
|
|
public function isAutoescape(): bool |
234
|
|
|
{ |
235
|
|
|
return $this->autoescape; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param boolean $flag |
240
|
|
|
* @return $this |
241
|
|
|
*/ |
242
|
|
|
public function setAutoescape(bool $flag) |
243
|
|
|
{ |
244
|
|
|
$this->autoescape = $flag; |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @param array $options |
251
|
|
|
*/ |
252
|
|
|
protected function setDefaultOptions(array $options) |
253
|
|
|
{ |
254
|
|
|
if (empty($options['name'])) { |
255
|
|
|
throw new \InvalidArgumentException(sprintf('Column MUST have name in %s class.', get_class($this))); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if (empty($options['label'])) { |
259
|
|
|
$options['label'] = camel_case($options['name']); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// placeholder MUST be the first element in options array. that's because "placeholder" decorator |
263
|
|
|
// can break further decorators. |
264
|
|
|
$placeholder = array_pull($options, 'placeholder'); |
265
|
|
|
if (!empty($placeholder)) { |
266
|
|
|
$options = array_merge(['placeholder' => $placeholder], $options); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
foreach ($options as $key => $values) { |
270
|
|
|
$methodName = 'set' . ucfirst(camel_case($key)); |
271
|
|
|
|
272
|
|
|
if (method_exists($this, $methodName)) { |
273
|
|
|
// setDecorators() method can overwrite previously set decorators. we don't want that to happen |
274
|
|
|
// because user could set other decorators via setRender() or setClickable() method. |
275
|
|
|
if ($methodName === 'setDecorators') { |
276
|
|
|
$values = array_merge($this->decorators, $values); |
277
|
|
|
} |
278
|
|
|
$this->$methodName($values); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param string $name |
285
|
|
|
* @return mixed |
286
|
|
|
*/ |
287
|
|
|
public function __get($name) |
288
|
|
|
{ |
289
|
|
|
$name = camel_case($name); |
290
|
|
|
|
291
|
|
|
if (!isset($this->$name)) { |
292
|
|
|
throw new \InvalidArgumentException( |
293
|
|
|
sprintf("Field %s does not exist in %s class", $name, get_class($this)) |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return $this->$name; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|