1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CrossKnowledge\DataTableBundle\DataTable\Table\Element\Column; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
6
|
|
|
|
7
|
|
|
class Column implements ColumnInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Column field info that might be used client side |
11
|
|
|
* |
12
|
|
|
* https://datatables.net/reference/option/columns |
13
|
|
|
* |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
public static $clientSideColumnOptions = [ |
17
|
|
|
'cellType','className','contentPadding', 'createdCell', 'data', 'defaultContent', 'name', 'orderable', |
18
|
|
|
'orderData', 'orderDataType', 'render', 'searchable', 'title', 'type', 'visible', 'width' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string key/value of options |
23
|
|
|
*/ |
24
|
|
|
protected $options; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var OptionsResolver |
28
|
|
|
*/ |
29
|
|
|
protected $optionsResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Closure callback that will be used to format this cell values |
33
|
|
|
*/ |
34
|
|
|
protected $formatValueCallback; |
35
|
|
|
|
36
|
14 |
|
public function __construct($title='', $options=[]) |
|
|
|
|
37
|
|
|
{ |
38
|
14 |
|
$this->optionsResolver = new OptionsResolver(); |
39
|
14 |
|
$this->configureOptions($this->optionsResolver); |
40
|
14 |
|
$this->setOptions(array_merge($options, ['title' => $title])); |
41
|
13 |
|
} |
42
|
|
|
/** |
43
|
|
|
* Column unique identifier |
44
|
|
|
* @param string $identifier |
45
|
|
|
* @return Column |
46
|
|
|
*/ |
47
|
2 |
|
public function setIdentifier($identifier) |
48
|
|
|
{ |
49
|
2 |
|
$this->identifier = $identifier; |
|
|
|
|
50
|
2 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
/** |
53
|
|
|
* @param OptionsResolver $resolver |
54
|
|
|
*/ |
55
|
14 |
|
public function configureOptions(OptionsResolver $resolver) |
56
|
|
|
{ |
57
|
14 |
|
$resolver->setDefined('title'); |
58
|
14 |
|
$resolver->setDefault('auto_escape', true); |
59
|
14 |
|
$resolver->setDefined(static::$clientSideColumnOptions); |
60
|
14 |
|
} |
61
|
|
|
/** |
62
|
|
|
* @param array $options one within static::$clientSideColumnOptions |
63
|
|
|
*/ |
64
|
14 |
|
public function setOptions(array $options) |
65
|
|
|
{ |
66
|
14 |
|
$this->options = $this->optionsResolver->resolve($options); |
|
|
|
|
67
|
13 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
/** |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
8 |
|
public function getOptions() |
73
|
|
|
{ |
74
|
8 |
|
return $this->options; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
/** |
77
|
|
|
* @param string $title |
78
|
|
|
* @return Column |
79
|
|
|
*/ |
80
|
|
|
public function setTitle($title) |
81
|
|
|
{ |
82
|
|
|
$this->options['title'] = $title; |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Format a cell content for this column |
88
|
|
|
* @param $value |
89
|
|
|
* @param array $rowData |
90
|
|
|
* @param $context |
91
|
|
|
* @return mixed |
92
|
|
|
*/ |
93
|
8 |
|
public function formatCell($value, array $rowData, $context) |
94
|
|
|
{ |
95
|
8 |
|
if (is_callable($this->formatValueCallback)) { |
96
|
1 |
|
return call_user_func_array($this->formatValueCallback, [$value, $rowData]); |
97
|
|
|
} else{ |
|
|
|
|
98
|
8 |
|
return $value; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return \Closure |
104
|
|
|
*/ |
105
|
1 |
|
public function getFormatValueCallback() |
106
|
|
|
{ |
107
|
1 |
|
return $this->formatValueCallback; |
108
|
|
|
} |
109
|
|
|
/** |
110
|
|
|
* @param \Closure $callback |
111
|
|
|
*/ |
112
|
2 |
|
public function setFormatValueCallback(\Closure $callback=null) |
|
|
|
|
113
|
|
|
{ |
114
|
2 |
|
$this->formatValueCallback = $callback; |
115
|
2 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* @return string key/value filtered for client side API |
119
|
|
|
*/ |
120
|
2 |
|
public function getClientSideDefinition() |
121
|
|
|
{ |
122
|
2 |
|
$infos = []; |
123
|
|
|
|
124
|
|
|
array_walk($this->options, function($optval, $optname) use (&$infos) { |
|
|
|
|
125
|
2 |
|
if (in_array($optname, static::$clientSideColumnOptions)) { |
126
|
2 |
|
$infos[$optname] = $optval; |
127
|
2 |
|
} |
128
|
2 |
|
}); |
129
|
|
|
|
130
|
2 |
|
return $infos; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|