Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ReportExportService | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| csv | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| excel | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services; |
| 4 | |
| 5 | use Symfony\Component\HttpFoundation\StreamedResponse; |
| 6 | |
| 7 | class ReportExportService |
| 8 | { |
| 9 | public function csv(string $filename, array $headers, iterable $rows): StreamedResponse |
| 10 | { |
| 11 | return response()->streamDownload(function () use ($headers, $rows) { |
| 12 | $handle = fopen('php://output', 'w'); |
| 13 | fputcsv($handle, $headers, ';'); |
| 14 | foreach ($rows as $row) { |
| 15 | fputcsv($handle, $row, ';'); |
| 16 | } |
| 17 | fclose($handle); |
| 18 | }, $filename, ['Content-Type' => 'text/csv; charset=UTF-8']); |
| 19 | } |
| 20 | |
| 21 | public function excel(string $filename, array $headers, iterable $rows): StreamedResponse |
| 22 | { |
| 23 | return response()->streamDownload(function () use ($headers, $rows) { |
| 24 | echo "<html><head><meta charset='UTF-8'></head><body><table border='1'>"; |
| 25 | echo '<tr>'; |
| 26 | foreach ($headers as $header) { |
| 27 | echo '<th>'.e($header).'</th>'; |
| 28 | } |
| 29 | echo '</tr>'; |
| 30 | foreach ($rows as $row) { |
| 31 | echo '<tr>'; |
| 32 | foreach ($row as $cell) { |
| 33 | echo '<td>'.e((string) $cell).'</td>'; |
| 34 | } |
| 35 | echo '</tr>'; |
| 36 | } |
| 37 | echo '</table></body></html>'; |
| 38 | }, $filename, ['Content-Type' => 'application/vnd.ms-excel; charset=UTF-8']); |
| 39 | } |
| 40 | } |