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