Merging report to main
[keruald.git] / report / src / Output / HTMLOutput.php
bloba722205c9e853358353487f90f556b4adf640e2b
1 <?php
3 namespace Keruald\Reporting\Output;
5 class HTMLOutput extends Output {
7 private function makeId ($name) : string {
8 return urlencode(strtolower(str_replace(' ', '-', $name)));
11 private static function encode (string $text) : string {
12 return htmlspecialchars($text);
15 public function render () : string {
17 $send = [];
18 $title = $this->report->title;
19 $send[] =
20 '<h1 id="' . $this->makeId($title) . '">' . self::encode($title)
21 . '</h1>';
23 foreach ($this->report->sections as $section) {
24 $title = $section->title;
25 $send[] =
26 '<h2 id="' . $this->makeId($title) . '">' .
27 self::encode($title) . '</h2>';
28 foreach ($section->entries as $entry) {
29 $title = $entry->title;
30 $send[] = '<h3 id="' . $this->makeId($title) . '">' .
31 self::encode($title) . '</h3>';
33 $text = explode("\n\n", $entry->text);
35 foreach ($text as $value) {
36 $send[] = '<p>' . self::encode($value) . '</p>';
40 $send[] = '<hr>';
42 $send[] = '<h2 id="report-properties">Report properties</h2>';
43 $send[] = '<table>';
44 $send[] = str_repeat(" ", 4) . '<tbody>';
46 $properties = $this->report->properties;
48 foreach ($properties as $key => $value) {
49 $send[] = str_repeat(" ", 4) . '<tr>';
50 $send[] = str_repeat(" ", 8) .
52 '<th>' . self::encode($key) . '</th>';
53 $send[] = str_repeat(" ", 8) .
54 '<td>' . self::encode($value) . '</td>';
56 $send[] = str_repeat(" ", 4) . '</tr>';
58 $send[] = str_repeat(" ", 4) . '</tbody>';
59 $send[] = '</table>';
60 $send[] = '';
62 return implode("\n", $send);