41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?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) ?: 'Отчёт';
|
|
}
|
|
}
|