Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Exporter | |
88.89% |
8 / 9 |
|
75.00% |
3 / 4 |
6.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| query | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| headings | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| map | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\ImportExport; |
| 4 | |
| 5 | use Illuminate\Database\Eloquent\Model; |
| 6 | use Maatwebsite\Excel\Concerns\FromQuery; |
| 7 | use Maatwebsite\Excel\Concerns\WithHeadings; |
| 8 | use Maatwebsite\Excel\Concerns\WithMapping; |
| 9 | |
| 10 | /** |
| 11 | * A generic spreadsheet export driven by a Definition. Streams straight from |
| 12 | * the query, so large tables don't blow memory. |
| 13 | */ |
| 14 | class Exporter implements FromQuery, WithHeadings, WithMapping |
| 15 | { |
| 16 | public function __construct( |
| 17 | private readonly Definition $definition, |
| 18 | private readonly array $filters = [], |
| 19 | ) {} |
| 20 | |
| 21 | public function query() |
| 22 | { |
| 23 | return $this->definition->applyFilters($this->definition->query(), $this->filters); |
| 24 | } |
| 25 | |
| 26 | public function headings(): array |
| 27 | { |
| 28 | return $this->definition->headings(); |
| 29 | } |
| 30 | |
| 31 | /** @param Model $row */ |
| 32 | public function map($row): array |
| 33 | { |
| 34 | $out = []; |
| 35 | foreach ($this->definition->resolvedColumns() as $key => $col) { |
| 36 | $out[] = isset($col['export']) |
| 37 | ? $col['export']($row) |
| 38 | : data_get($row, $key); |
| 39 | } |
| 40 | |
| 41 | return $out; |
| 42 | } |
| 43 | } |