|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* soluble-flexstore library |
|
5
|
|
|
* |
|
6
|
|
|
* @author Vanvelthem Sébastien |
|
7
|
|
|
* @link https://github.com/belgattitude/soluble-flexstore |
|
8
|
|
|
* @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien |
|
9
|
|
|
* @license MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Soluble\FlexStore\Column; |
|
14
|
|
|
|
|
15
|
|
|
use Soluble\FlexStore\Column\Type\AbstractType; |
|
16
|
|
|
use Soluble\FlexStore\Formatter\FormatterInterface; |
|
17
|
|
|
|
|
18
|
|
|
class Column implements ColumnSettableInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $name; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $properties = [ |
|
29
|
|
|
'type' => null, // will be defaulted to string |
|
30
|
|
|
'formatter' => null, |
|
31
|
|
|
'excluded' => false, |
|
32
|
|
|
'hidden' => false, |
|
33
|
|
|
'width' => null, |
|
34
|
|
|
'header' => null, // will be defaulted to name |
|
35
|
|
|
'filterable' => true, |
|
36
|
|
|
'groupable' => true, |
|
37
|
|
|
'sortable' => true, |
|
38
|
|
|
'editable' => false, |
|
39
|
|
|
'virtual' => true, |
|
40
|
|
|
'align' => null, |
|
41
|
|
|
'class' => null |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Constructor. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $name unique identifier name for the column |
|
48
|
|
|
* @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...) |
|
49
|
|
|
* |
|
50
|
|
|
* @throws Exception\InvalidArgumentException |
|
51
|
|
|
*/ |
|
52
|
40 |
|
public function __construct($name, array $properties = null) |
|
53
|
|
|
{ |
|
54
|
40 |
|
if (!is_string($name)) { |
|
55
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' Column name must be a string'); |
|
56
|
|
|
} |
|
57
|
39 |
|
if (trim($name) === '') { |
|
58
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' Column name cannot be empty'); |
|
59
|
|
|
} |
|
60
|
38 |
|
$this->name = $name; |
|
61
|
38 |
|
if ($properties !== null) { |
|
62
|
7 |
|
$this->setProperties($properties); |
|
63
|
|
|
} |
|
64
|
37 |
|
$this->initDefaults(); |
|
65
|
37 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* This method ensure some properties are defaulted. |
|
69
|
|
|
* For example header with name and type is string. |
|
70
|
|
|
*/ |
|
71
|
37 |
|
protected function initDefaults() |
|
72
|
|
|
{ |
|
73
|
37 |
|
if ($this->properties['header'] === null) { |
|
74
|
35 |
|
$this->setHeader($this->name); |
|
75
|
|
|
} |
|
76
|
37 |
|
if ($this->properties['type'] === null) { |
|
77
|
35 |
|
$this->setType(ColumnType::createType(ColumnType::TYPE_STRING)); |
|
78
|
|
|
} |
|
79
|
37 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the name of the column. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
33 |
|
public function getName() |
|
87
|
|
|
{ |
|
88
|
33 |
|
return $this->name; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set column datatype. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string|AbstractType $type |
|
95
|
|
|
* |
|
96
|
|
|
* @throws Exception\InvalidArgumentException when the type is not supported |
|
97
|
|
|
* |
|
98
|
|
|
* @return Column |
|
99
|
|
|
*/ |
|
100
|
37 |
|
public function setType($type) |
|
101
|
|
|
{ |
|
102
|
37 |
|
if (is_string($type)) { |
|
103
|
10 |
|
$type = ColumnType::createType($type); |
|
104
|
35 |
|
} elseif (!$type instanceof AbstractType) { |
|
105
|
|
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' setType() accepts only AbstractType or string.'); |
|
106
|
|
|
} |
|
107
|
37 |
|
$this->properties['type'] = $type; |
|
108
|
|
|
|
|
109
|
37 |
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return AbstractType |
|
114
|
|
|
*/ |
|
115
|
4 |
|
public function getType() |
|
116
|
|
|
{ |
|
117
|
4 |
|
return $this->properties['type']; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param FormatterInterface $formatter |
|
122
|
|
|
* |
|
123
|
|
|
* @return Column |
|
124
|
|
|
*/ |
|
125
|
4 |
|
public function setFormatter(FormatterInterface $formatter) |
|
126
|
|
|
{ |
|
127
|
4 |
|
$this->properties['formatter'] = $formatter; |
|
128
|
|
|
|
|
129
|
4 |
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return FormatterInterface|null |
|
134
|
|
|
*/ |
|
135
|
11 |
|
public function getFormatter() |
|
136
|
|
|
{ |
|
137
|
11 |
|
return $this->properties['formatter']; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param bool $virtual |
|
142
|
|
|
* |
|
143
|
|
|
* @return Column |
|
144
|
|
|
*/ |
|
145
|
33 |
|
public function setVirtual($virtual = true) |
|
146
|
|
|
{ |
|
147
|
33 |
|
$this->properties['virtual'] = $virtual; |
|
148
|
|
|
|
|
149
|
33 |
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return bool |
|
154
|
|
|
*/ |
|
155
|
6 |
|
public function isVirtual() |
|
156
|
|
|
{ |
|
157
|
6 |
|
return $this->properties['virtual']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param bool $excluded |
|
162
|
|
|
* |
|
163
|
|
|
* @return Column |
|
164
|
|
|
*/ |
|
165
|
11 |
|
public function setExcluded($excluded = true) |
|
166
|
|
|
{ |
|
167
|
11 |
|
$this->properties['excluded'] = $excluded; |
|
168
|
|
|
|
|
169
|
11 |
|
return $this; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
23 |
|
public function isExcluded() |
|
176
|
|
|
{ |
|
177
|
23 |
|
return $this->properties['excluded']; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param bool $editable |
|
182
|
|
|
* |
|
183
|
|
|
* @return Column |
|
184
|
|
|
*/ |
|
185
|
4 |
|
public function setEditable($editable = true) |
|
186
|
|
|
{ |
|
187
|
4 |
|
$this->properties['editable'] = $editable; |
|
188
|
|
|
|
|
189
|
4 |
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @return bool |
|
194
|
|
|
*/ |
|
195
|
4 |
|
public function isEditable() |
|
196
|
|
|
{ |
|
197
|
4 |
|
return $this->properties['editable']; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param bool $hidden |
|
202
|
|
|
* |
|
203
|
|
|
* @return Column |
|
204
|
|
|
*/ |
|
205
|
4 |
|
public function setHidden($hidden = true) |
|
206
|
|
|
{ |
|
207
|
4 |
|
$this->properties['hidden'] = $hidden; |
|
208
|
|
|
|
|
209
|
4 |
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @return bool |
|
214
|
|
|
*/ |
|
215
|
3 |
|
public function isHidden() |
|
216
|
|
|
{ |
|
217
|
3 |
|
return (bool) $this->properties['hidden']; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param bool $sortable |
|
222
|
|
|
* |
|
223
|
|
|
* @return Column |
|
224
|
|
|
*/ |
|
225
|
4 |
|
public function setSortable($sortable = true) |
|
226
|
|
|
{ |
|
227
|
4 |
|
$this->properties['sortable'] = $sortable; |
|
228
|
|
|
|
|
229
|
4 |
|
return $this; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* @return bool |
|
234
|
|
|
*/ |
|
235
|
4 |
|
public function isSortable() |
|
236
|
|
|
{ |
|
237
|
4 |
|
return (bool) $this->properties['sortable']; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param bool $groupable |
|
242
|
|
|
* |
|
243
|
|
|
* @return Column |
|
244
|
|
|
*/ |
|
245
|
4 |
|
public function setGroupable($groupable = true) |
|
246
|
|
|
{ |
|
247
|
4 |
|
$this->properties['groupable'] = $groupable; |
|
248
|
|
|
|
|
249
|
4 |
|
return $this; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return bool |
|
254
|
|
|
*/ |
|
255
|
4 |
|
public function isGroupable() |
|
256
|
|
|
{ |
|
257
|
4 |
|
return (bool) $this->properties['groupable']; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @param bool $filterable |
|
262
|
|
|
* |
|
263
|
|
|
* @return Column |
|
264
|
|
|
*/ |
|
265
|
4 |
|
public function setFilterable($filterable = true) |
|
266
|
|
|
{ |
|
267
|
4 |
|
$this->properties['filterable'] = $filterable; |
|
268
|
|
|
|
|
269
|
4 |
|
return $this; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @return bool |
|
274
|
|
|
*/ |
|
275
|
4 |
|
public function isFilterable() |
|
276
|
|
|
{ |
|
277
|
4 |
|
return (bool) $this->properties['filterable']; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Set recommended width for the column. |
|
282
|
|
|
* |
|
283
|
|
|
* @throws Exception\InvalidArgumentException |
|
284
|
|
|
* |
|
285
|
|
|
* @param float|int|string $width |
|
286
|
|
|
* |
|
287
|
|
|
* @return Column |
|
288
|
|
|
*/ |
|
289
|
5 |
|
public function setWidth($width) |
|
290
|
|
|
{ |
|
291
|
5 |
|
if (!is_scalar($width)) { |
|
292
|
1 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' Width parameter must be scalar.'); |
|
293
|
|
|
} |
|
294
|
4 |
|
$this->properties['width'] = $width; |
|
295
|
|
|
|
|
296
|
4 |
|
return $this; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @return float|int|string |
|
301
|
|
|
*/ |
|
302
|
4 |
|
public function getWidth() |
|
303
|
|
|
{ |
|
304
|
4 |
|
return $this->properties['width']; |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Set table header for this column. |
|
309
|
|
|
* |
|
310
|
|
|
* @throws Exception\InvalidArgumentException |
|
311
|
|
|
* |
|
312
|
|
|
* @param string|null $header |
|
313
|
|
|
* |
|
314
|
|
|
* @return Column |
|
315
|
|
|
*/ |
|
316
|
38 |
|
public function setHeader($header) |
|
317
|
|
|
{ |
|
318
|
38 |
|
$this->properties['header'] = $header; |
|
319
|
|
|
|
|
320
|
38 |
|
return $this; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @return string|null |
|
325
|
|
|
*/ |
|
326
|
4 |
|
public function getHeader() |
|
327
|
|
|
{ |
|
328
|
4 |
|
return $this->properties['header']; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Set recommended horizontal align. |
|
333
|
|
|
* |
|
334
|
|
|
* @throws Exception\InvalidArgumentException |
|
335
|
|
|
* |
|
336
|
|
|
* @param string $align can be left|center|right |
|
337
|
|
|
* |
|
338
|
|
|
* @return Column |
|
339
|
|
|
*/ |
|
340
|
1 |
|
public function setAlign($align) |
|
341
|
|
|
{ |
|
342
|
1 |
|
if (!is_string($align) || !in_array(strtolower($align), ['left', 'right', 'center'], true)) { |
|
343
|
|
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' Align param must be a string : right, left, center.'); |
|
344
|
|
|
} |
|
345
|
1 |
|
$this->properties['align'] = $align; |
|
346
|
|
|
|
|
347
|
1 |
|
return $this; |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* Return recommended horizontal alignment. |
|
352
|
|
|
* |
|
353
|
|
|
* @return string|null |
|
354
|
|
|
*/ |
|
355
|
1 |
|
public function getAlign() |
|
356
|
|
|
{ |
|
357
|
1 |
|
return $this->properties['align']; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Set recommended css class. |
|
362
|
|
|
* |
|
363
|
|
|
* @throws Exception\InvalidArgumentException |
|
364
|
|
|
* |
|
365
|
|
|
* @param string $class css class |
|
366
|
|
|
* |
|
367
|
|
|
* @return Column |
|
368
|
|
|
*/ |
|
369
|
1 |
|
public function setClass($class) |
|
370
|
|
|
{ |
|
371
|
1 |
|
if (!is_string($class)) { |
|
372
|
|
|
throw new Exception\InvalidArgumentException(__METHOD__ . ' Class param must be a string'); |
|
373
|
|
|
} |
|
374
|
1 |
|
$this->properties['class'] = $class; |
|
375
|
|
|
|
|
376
|
1 |
|
return $this; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* Return recommended css class. |
|
381
|
|
|
* |
|
382
|
|
|
* @return string|null |
|
383
|
|
|
*/ |
|
384
|
1 |
|
public function getClass() |
|
385
|
|
|
{ |
|
386
|
1 |
|
return $this->properties['class']; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* @return array |
|
391
|
|
|
*/ |
|
392
|
2 |
|
public function getProperties() |
|
393
|
|
|
{ |
|
394
|
2 |
|
return $this->properties; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Set properties for the column. |
|
399
|
|
|
* |
|
400
|
|
|
* @throws Exception\InvalidArgumentException |
|
401
|
|
|
* |
|
402
|
|
|
* @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...) |
|
403
|
|
|
* |
|
404
|
|
|
* @return Column |
|
405
|
|
|
*/ |
|
406
|
8 |
|
public function setProperties(array $properties) |
|
407
|
|
|
{ |
|
408
|
8 |
|
foreach ($properties as $key => $value) { |
|
409
|
8 |
|
if (array_key_exists($key, $this->properties)) { |
|
410
|
8 |
|
$method = 'set' . ucfirst($key); |
|
411
|
8 |
|
$this->$method($value); |
|
412
|
|
|
} else { |
|
413
|
8 |
|
throw new Exception\InvalidArgumentException(__METHOD__ . " property '$key' is not supported in column."); |
|
414
|
|
|
} |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
6 |
|
return $this; |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
/** |
|
421
|
|
|
* @return string |
|
422
|
|
|
*/ |
|
423
|
5 |
|
public function __toString() |
|
424
|
|
|
{ |
|
425
|
5 |
|
return $this->name; |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
|