Files
siakad/app/Exports/ReportExport.php
T
2026-11-14 07:51:17 +07:00

106 lines
4.2 KiB
PHP

<?php
namespace App\Exports;
use App\Support\Legacy\Reports\ReportResult;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\WithFreezePane;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\DefaultValueBinder;
use Override;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* Hasil laporan operator (kolom + baris ReportResult) sebagai satu lembar Excel: baris kepala tebal dan dibekukan,
* lebar kolom mengikuti isi, baris penjumlahan berarsir. Teks ditulis apa adanya agar NIP/NIDN panjang dan nol di depan tidak diubah Excel.
*/
final class ReportExport extends DefaultValueBinder implements FromArray, ShouldAutoSize, WithCustomValueBinder, WithFreezePane, WithHeadings, WithStrictNullComparison, WithStyles, WithTitle
{
/** Batas Excel untuk nama lembar. */
private const int TITLE_MAX = 31;
/** Arsir baris penjumlahan: emas lembut, sepadan dengan --color-gold-soft di tabel. */
private const string TOTAL_FILL = 'FFF2CC';
/**
* @param list<array{key: string, label: string, num?: bool}> $columns
* @param list<array<string, mixed>> $rows
*/
public function __construct(private readonly string $title, private readonly array $columns, private readonly array $rows) {}
public function headings(): array
{
return array_column($this->columns, 'label');
}
/** Nilai tiap baris menurut urutan kolom; kosong dan nilai bukan skalar menjadi sel kosong (0 tetap ditulis). */
public function array(): array
{
return array_map(fn (array $row): array => array_map(function (array $column) use ($row): mixed {
$value = $row[$column['key']] ?? null;
return is_scalar($value) && $value !== '' ? $value : null;
}, $this->columns), $this->rows);
}
/** Nama lembar yang diterima Excel: tanpa [ ] : * ? / \ dan paling banyak 31 karakter. */
public function title(): string
{
$title = trim(mb_substr(preg_replace('/[\[\]:*?\/\\\\]+/u', ' ', $this->title) ?? '', 0, self::TITLE_MAX), " '");
return $title === '' ? 'Data' : $title;
}
public function freezePane(): string
{
return 'A2';
}
/** Baris kepala tebal; baris penjumlahan (penanda ReportResult::TOTAL_KEY) tebal, berarsir, dan bergaris atas. */
public function styles(Worksheet $sheet): array
{
$styles = [1 => ['font' => ['bold' => true]]];
$lastColumn = Coordinate::stringFromColumnIndex(max(1, count($this->columns)));
foreach (array_values($this->rows) as $index => $row) {
if ($row[ReportResult::TOTAL_KEY] ?? false) {
// Baris 1 = kepala, jadi baris data ke-n ada di baris n + 2; arsir selebar tabel walau sel terakhirnya kosong
$n = $index + 2;
$styles["A{$n}:{$lastColumn}{$n}"] = [
'font' => ['bold' => true],
'fill' => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => self::TOTAL_FILL]],
'borders' => ['top' => ['borderStyle' => Border::BORDER_THIN]],
];
}
}
return $styles;
}
/** Teks tetap teks: tidak ditebak sebagai angka (NIP 18 digit, nol di depan) maupun rumus (awalan "="). */
#[Override]
public function bindValue(Cell $cell, mixed $value): bool
{
if (is_string($value)) {
$cell->setValueExplicit(StringHelper::sanitizeUTF8($value), DataType::TYPE_STRING);
// Tanpa segitiga peringatan "angka disimpan sebagai teks" di Excel
$cell->getIgnoredErrors()->setNumberStoredAsText(is_numeric($value));
return true;
}
return parent::bindValue($cell, $value);
}
}