UI коструктора отчетов
This commit is contained in:
40
app/Services/Analytics/Export/AnalyticsExcelExport.php
Normal file
40
app/Services/Analytics/Export/AnalyticsExcelExport.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Analytics\Export;
|
||||
|
||||
use App\Exports\Sheets\ArraySheetExport;
|
||||
use App\Services\Analytics\AnalyticsResult;
|
||||
use App\Services\Analytics\Column;
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
|
||||
class AnalyticsExcelExport implements WithMultipleSheets
|
||||
{
|
||||
public function __construct(
|
||||
private readonly AnalyticsResult $result,
|
||||
private readonly string $title,
|
||||
) {}
|
||||
|
||||
public function sheets(): array
|
||||
{
|
||||
$rows = [array_map(fn (Column $c) => $c->label, $this->result->columns)];
|
||||
|
||||
if ($this->result->rows === []) {
|
||||
$rows[] = ['Нет данных за выбранный период'];
|
||||
}
|
||||
|
||||
foreach ($this->result->rows as $row) {
|
||||
$line = [];
|
||||
foreach ($this->result->columns as $column) {
|
||||
$line[] = $row[$column->key] ?? '';
|
||||
}
|
||||
$rows[] = $line;
|
||||
}
|
||||
|
||||
return [new ArraySheetExport($this->sheetTitle(), $rows)];
|
||||
}
|
||||
|
||||
private function sheetTitle(): string
|
||||
{
|
||||
return mb_substr(preg_replace('/[\\\\\/\?\*\[\]:]/', ' ', $this->title), 0, 31) ?: 'Отчёт';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user