54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Support\Legacy\Reports;
|
|
|
|
/** Hasil satu laporan: kolom, baris (nilai skalar), keterangan di atas tabel, dan jumlah baris sebenarnya. */
|
|
final readonly class ReportResult
|
|
{
|
|
/** Penanda baris penjumlahan pada baris hasil markedRows(); bukan kolom, jadi tidak ikut diunduh atau dicari. */
|
|
public const string TOTAL_KEY = '_total';
|
|
|
|
/**
|
|
* @param list<array{key: string, label: string, num?: bool}> $columns
|
|
* @param list<array<string, scalar|null>> $rows kunci = 'key' kolom
|
|
* @param int|null $total jumlah baris sebenarnya (bila lebih banyak dari $rows)
|
|
* @param list<string> $summary baris keterangan (mis. "Periode: 2020/2021 Ganjil", "Jumlah: 120 mahasiswa")
|
|
* @param list<int> $totals indeks $rows yang merupakan baris penjumlahan (mis. "Jumlah" di akhir): diberi arsir di tabel, cetak, dan Excel
|
|
*/
|
|
public function __construct(
|
|
public array $columns,
|
|
public array $rows,
|
|
public ?int $total = null,
|
|
public array $summary = [],
|
|
public ?string $note = null,
|
|
public array $totals = [],
|
|
) {}
|
|
|
|
/**
|
|
* Baris hasil dengan penanda TOTAL_KEY pada baris penjumlahan, supaya penandanya ikut saat baris dipotong per halaman atau disaring.
|
|
*
|
|
* @return list<array<string, mixed>>
|
|
*/
|
|
public function markedRows(): array
|
|
{
|
|
$rows = $this->rows;
|
|
foreach ($this->totals as $index) {
|
|
if (isset($rows[$index])) {
|
|
$rows[$index][self::TOTAL_KEY] = true;
|
|
}
|
|
}
|
|
|
|
return $rows;
|
|
}
|
|
|
|
public function count(): int
|
|
{
|
|
return $this->total ?? count($this->rows);
|
|
}
|
|
|
|
public function truncated(): bool
|
|
{
|
|
return $this->total !== null && $this->total > count($this->rows);
|
|
}
|
|
}
|