Skip to content

Commit 4c4075c

Browse files
committed
Refactor duplicate mapping of response.
1 parent 3175f90 commit 4c4075c

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/Services/DataTable.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,9 @@ protected function filename()
144144
*/
145145
protected function getDataForExport()
146146
{
147-
return array_map(function ($row) {
148-
if ($columns = $this->exportColumns()) {
149-
return (new DataTransformer())->transform($row, $columns, 'exportable');
150-
}
147+
$columns = $this->exportColumns();
151148

152-
return $row;
153-
}, $this->getAjaxResponseData());
149+
return $this->mapResponseToColumns($columns, 'exportable');
154150
}
155151

156152
/**
@@ -193,6 +189,24 @@ public function builder()
193189
return $this->datatables->getHtmlBuilder();
194190
}
195191

192+
/**
193+
* Map ajax response to columns definition.
194+
*
195+
* @param mixed $columns
196+
* @param string $type
197+
* @return array
198+
*/
199+
protected function mapResponseToColumns($columns, $type)
200+
{
201+
return array_map(function ($row) use ($columns, $type) {
202+
if ($columns) {
203+
return (new DataTransformer())->transform($row, $columns, $type);
204+
}
205+
206+
return $row;
207+
}, $this->getAjaxResponseData());
208+
}
209+
196210
/**
197211
* Get decorated data as defined in datatables ajax response.
198212
*
@@ -248,13 +262,9 @@ public function printPreview()
248262
*/
249263
protected function getDataForPrint()
250264
{
251-
return array_map(function ($row) {
252-
if ($columns = $this->printColumns()) {
253-
return (new DataTransformer())->transform($row, $columns, 'printable');
254-
}
265+
$columns = $this->printColumns();
255266

256-
return $row;
257-
}, $this->getAjaxResponseData());
267+
return $this->mapResponseToColumns($columns, 'printable');
258268
}
259269

260270
/**

0 commit comments

Comments
 (0)