Skip to content

Commit 1de334c

Browse files
committed
Extract abstract class DataTablesCollectionExport.
1 parent 45c7faf commit 1de334c

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Exports;
4+
5+
use Illuminate\Support\Collection;
6+
use Maatwebsite\Excel\Concerns\Exportable;
7+
use Maatwebsite\Excel\Concerns\FromCollection;
8+
use Maatwebsite\Excel\Concerns\WithHeadings;
9+
10+
abstract class DataTablesCollectionExport implements FromCollection, WithHeadings
11+
{
12+
use Exportable;
13+
14+
/**
15+
* @var Collection
16+
*/
17+
protected $collection;
18+
19+
/**
20+
* @param Collection $collection
21+
*/
22+
public function __construct(Collection $collection)
23+
{
24+
$this->collection = $collection;
25+
}
26+
27+
/**
28+
* @return Collection
29+
*/
30+
public function collection()
31+
{
32+
return $this->collection;
33+
}
34+
35+
/**
36+
* @return array
37+
*/
38+
public function headings(): array
39+
{
40+
$first = $this->collection->first();
41+
if ($first) {
42+
return array_keys($first);
43+
}
44+
45+
return [];
46+
}
47+
}

src/Services/DataTablesExportHandler.php

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,9 @@
66
use Maatwebsite\Excel\Concerns\Exportable;
77
use Maatwebsite\Excel\Concerns\FromCollection;
88
use Maatwebsite\Excel\Concerns\WithHeadings;
9+
use Yajra\DataTables\Exports\DataTablesCollectionExport;
910

10-
class DataTablesExportHandler implements FromCollection, WithHeadings
11+
class DataTablesExportHandler extends DataTablesCollectionExport
1112
{
12-
use Exportable;
1313

14-
/**
15-
* @var Collection
16-
*/
17-
protected $collection;
18-
19-
/**
20-
* DataTablesExportHandler constructor.
21-
*
22-
* @param Collection $collection
23-
*/
24-
public function __construct(Collection $collection)
25-
{
26-
$this->collection = $collection;
27-
}
28-
29-
/**
30-
* @return Collection
31-
*/
32-
public function collection()
33-
{
34-
return $this->collection;
35-
}
36-
37-
/**
38-
* @return array
39-
*/
40-
public function headings(): array
41-
{
42-
$first = $this->collection->first();
43-
if ($first) {
44-
return array_keys($first);
45-
}
46-
47-
return [];
48-
}
4914
}

0 commit comments

Comments
 (0)