1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Processors; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Yajra\DataTables\Utilities\Helper; |
7
|
|
|
|
8
|
|
|
class DataProcessor |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var int |
12
|
|
|
*/ |
13
|
|
|
protected $start; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Columns to escape value. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $escapeColumns = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Processed data output. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $output = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $appendColumns = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $editColumns = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $excessColumns = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var mixed |
46
|
|
|
*/ |
47
|
|
|
protected $results; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $templates; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $includeIndex; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var array |
61
|
|
|
*/ |
62
|
|
|
protected $rawColumns; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $exceptions = ['DT_RowId', 'DT_RowClass', 'DT_RowData', 'DT_RowAttr']; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param mixed $results |
71
|
|
|
* @param array $columnDef |
72
|
|
|
* @param array $templates |
73
|
|
|
* @param int $start |
74
|
|
|
*/ |
75
|
|
|
public function __construct($results, array $columnDef, array $templates, $start) |
76
|
|
|
{ |
77
|
|
|
$this->results = $results; |
78
|
|
|
$this->appendColumns = $columnDef['append']; |
79
|
|
|
$this->editColumns = $columnDef['edit']; |
80
|
|
|
$this->excessColumns = $columnDef['excess']; |
81
|
|
|
$this->onlyColumns = $columnDef['only']; |
|
|
|
|
82
|
|
|
$this->escapeColumns = $columnDef['escape']; |
83
|
|
|
$this->includeIndex = $columnDef['index']; |
84
|
|
|
$this->rawColumns = $columnDef['raw']; |
85
|
|
|
$this->templates = $templates; |
86
|
|
|
$this->start = $start; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Process data to output on browser. |
91
|
|
|
* |
92
|
|
|
* @param bool $object |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function process($object = false) |
96
|
|
|
{ |
97
|
|
|
$this->output = []; |
98
|
|
|
$indexColumn = config('datatables.index_column', 'DT_RowIndex'); |
99
|
|
|
|
100
|
|
|
foreach ($this->results as $row) { |
101
|
|
|
$data = Helper::convertToArray($row); |
102
|
|
|
$value = $this->addColumns($data, $row); |
103
|
|
|
$value = $this->editColumns($value, $row); |
104
|
|
|
$value = $this->setupRowVariables($value, $row); |
105
|
|
|
$value = $this->selectOnlyNeededColumns($value); |
106
|
|
|
$value = $this->removeExcessColumns($value); |
107
|
|
|
|
108
|
|
|
if ($this->includeIndex) { |
109
|
|
|
$value[$indexColumn] = ++$this->start; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->output[] = $object ? $value : $this->flatten($value); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->escapeColumns($this->output); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Process add columns. |
120
|
|
|
* |
121
|
|
|
* @param mixed $data |
122
|
|
|
* @param mixed $row |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
protected function addColumns($data, $row) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
foreach ($this->appendColumns as $key => $value) { |
128
|
|
|
$value['content'] = Helper::compileContent($value['content'], $data, $row); |
129
|
|
|
$data = Helper::includeInArray($value, $data); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $data; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Process edit columns. |
137
|
|
|
* |
138
|
|
|
* @param mixed $data |
139
|
|
|
* @param mixed $row |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
protected function editColumns($data, $row) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
foreach ($this->editColumns as $key => $value) { |
145
|
|
|
$value['content'] = Helper::compileContent($value['content'], $data, $row); |
146
|
|
|
Arr::set($data, $value['name'], $value['content']); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $data; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Setup additional DT row variables. |
154
|
|
|
* |
155
|
|
|
* @param mixed $data |
156
|
|
|
* @param mixed $row |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
protected function setupRowVariables($data, $row) |
160
|
|
|
{ |
161
|
|
|
$processor = new RowProcessor($data, $row); |
162
|
|
|
|
163
|
|
|
return $processor |
164
|
|
|
->rowValue('DT_RowId', $this->templates['DT_RowId']) |
165
|
|
|
->rowValue('DT_RowClass', $this->templates['DT_RowClass']) |
166
|
|
|
->rowData('DT_RowData', $this->templates['DT_RowData']) |
167
|
|
|
->rowData('DT_RowAttr', $this->templates['DT_RowAttr']) |
168
|
|
|
->getData(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get only needed columns. |
173
|
|
|
* |
174
|
|
|
* @param array $data |
175
|
|
|
* @return array |
176
|
|
|
*/ |
177
|
|
|
protected function selectOnlyNeededColumns(array $data) |
178
|
|
|
{ |
179
|
|
|
if (is_null($this->onlyColumns)) { |
180
|
|
|
return $data; |
181
|
|
|
} else { |
182
|
|
|
return array_intersect_key($data, array_flip(array_merge($this->onlyColumns, $this->exceptions))); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Remove declared hidden columns. |
188
|
|
|
* |
189
|
|
|
* @param array $data |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
protected function removeExcessColumns(array $data) |
193
|
|
|
{ |
194
|
|
|
foreach ($this->excessColumns as $value) { |
195
|
|
|
unset($data[$value]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $data; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Flatten array with exceptions. |
203
|
|
|
* |
204
|
|
|
* @param array $array |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function flatten(array $array) |
208
|
|
|
{ |
209
|
|
|
$return = []; |
210
|
|
|
foreach ($array as $key => $value) { |
211
|
|
|
if (in_array($key, $this->exceptions)) { |
212
|
|
|
$return[$key] = $value; |
213
|
|
|
} else { |
214
|
|
|
$return[] = $value; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return $return; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Escape column values as declared. |
223
|
|
|
* |
224
|
|
|
* @param array $output |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
protected function escapeColumns(array $output) |
228
|
|
|
{ |
229
|
|
|
return array_map(function ($row) { |
230
|
|
|
if ($this->escapeColumns == '*') { |
231
|
|
|
$row = $this->escapeRow($row); |
232
|
|
|
} elseif (is_array($this->escapeColumns)) { |
233
|
|
|
$columns = array_diff($this->escapeColumns, $this->rawColumns); |
234
|
|
|
foreach ($columns as $key) { |
235
|
|
|
array_set($row, $key, e(array_get($row, $key))); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $row; |
240
|
|
|
}, $output); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Escape all values of row. |
245
|
|
|
* |
246
|
|
|
* @param array $row |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
|
protected function escapeRow(array $row) |
250
|
|
|
{ |
251
|
|
|
$arrayDot = array_filter(array_dot($row)); |
252
|
|
|
foreach ($arrayDot as $key => $value) { |
253
|
|
|
if (! in_array($key, $this->rawColumns)) { |
254
|
|
|
$arrayDot[$key] = e($value); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
foreach ($arrayDot as $key => $value) { |
259
|
|
|
array_set($row, $key, $value); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $row; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: