$payload
*/
public function render(array $payload, string $correlationId): PdfRenderResult
{
return $this->pdfRenderingGateway->renderHtml(new PdfRenderRequest(
html: $this->html($payload),
options: [
'printBackground' => true,
'preferCssPageSize' => true,
'displayHeaderFooter' => true,
'headerTemplate' => '
TenantPilot Management Report
',
'footerTemplate' => '/
',
],
correlationId: $correlationId,
outputFilename: 'tenantpilot-management-report',
));
}
/**
* @param array $payload
*/
private function html(array $payload): string
{
$title = e((string) ($payload['title'] ?? 'TenantPilot Management Report'));
$environment = e((string) data_get($payload, 'managed_environment.name', 'Managed environment'));
$profile = e((string) ($payload['profile_label'] ?? 'Customer executive'));
$classification = e((string) ($payload['classification'] ?? 'Customer-facing management report'));
$generatedAt = e((string) data_get($payload, 'provenance.generated_at', now()->toIso8601String()));
$chapters = is_array($payload['chapters'] ?? null) ? $payload['chapters'] : [];
$chapterHtml = collect($chapters)
->filter(static fn (mixed $chapter): bool => is_array($chapter))
->map(fn (array $chapter): string => $this->chapterHtml($chapter))
->implode("\n");
return <<
{$title}
{$title}
{$environment}
Profile: {$profile} · Classification: {$classification} · Generated: {$generatedAt}
{$chapterHtml}
HTML;
}
/**
* @param array $chapter
*/
private function chapterHtml(array $chapter): string
{
$title = e((string) ($chapter['title'] ?? 'Chapter'));
$content = is_array($chapter['content'] ?? null) ? $chapter['content'] : [];
$contentHtml = $this->contentHtml($content);
return <<
{$title}
{$contentHtml}
HTML;
}
/**
* @param array $content
*/
private function contentHtml(array $content): string
{
if ($content === []) {
return 'No items recorded.
';
}
$html = '';
foreach ($content as $key => $value) {
if (is_array($value)) {
$html .= ''.e($this->humanize((string) $key)).'
'.$this->arrayHtml($value);
} elseif ($value !== null && trim((string) $value) !== '') {
$html .= ''.e($this->humanize((string) $key)).'
'.e((string) $value).'
';
}
}
return $html !== '' ? $html : 'No items recorded.
';
}
/**
* @param array $items
*/
private function arrayHtml(array $items): string
{
if ($items === []) {
return 'No items recorded.
';
}
$listItems = collect($items)
->map(function (mixed $item): string {
if (is_array($item)) {
$parts = collect($item)
->map(fn (mixed $value, string|int $key): string => ''.e($this->humanize((string) $key)).': '.e((string) $value))
->implode('
');
return ''.$parts.'';
}
return ''.e((string) $item).'';
})
->implode('');
return '';
}
private function humanize(string $value): string
{
return ucfirst(str_replace('_', ' ', $value));
}
}