1 | <?php |
||
24 | class HTMLConverter |
||
25 | { |
||
26 | /** |
||
27 | * table class attribute value. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $class_name = 'table-csv-data'; |
||
32 | |||
33 | /** |
||
34 | * table id attribute value. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $id_value = ''; |
||
39 | |||
40 | /** |
||
41 | * @var XMLConverter |
||
42 | */ |
||
43 | protected $xml_converter; |
||
44 | |||
45 | /** |
||
46 | * New Instance. |
||
47 | */ |
||
48 | 6 | public function __construct() |
|
49 | { |
||
50 | 6 | $this->xml_converter = (new XMLConverter()) |
|
51 | 6 | ->rootElement('table') |
|
52 | 6 | ->recordElement('tr') |
|
53 | 6 | ->fieldElement('td') |
|
54 | ; |
||
55 | 6 | } |
|
56 | |||
57 | /** |
||
58 | * Converts a tabular data collection into a HTML table string. |
||
59 | * |
||
60 | * @param string[] $header_record An optional array of headers outputted using the`<thead>` section |
||
61 | * @param string[] $footer_record An optional array of footers to output to the table using `<tfoot>` and `<th>` elements |
||
62 | */ |
||
63 | 12 | public function convert(iterable $records, array $header_record = [], array $footer_record = []): string |
|
64 | { |
||
65 | 12 | $doc = new DOMDocument('1.0'); |
|
66 | 12 | if ([] === $header_record && [] === $footer_record) { |
|
67 | 3 | $table = $this->xml_converter->import($records, $doc); |
|
68 | 3 | $this->addHTMLAttributes($table); |
|
69 | 3 | $doc->appendChild($table); |
|
70 | |||
71 | /** @var string $content */ |
||
72 | 3 | $content = $doc->saveHTML(); |
|
73 | |||
74 | 3 | return $content; |
|
75 | } |
||
76 | |||
77 | 9 | $table = $doc->createElement('table'); |
|
78 | 9 | $this->addHTMLAttributes($table); |
|
79 | 9 | $this->appendHeaderSection('thead', $header_record, $table); |
|
80 | 9 | $this->appendHeaderSection('tfoot', $footer_record, $table); |
|
81 | 9 | $table->appendChild($this->xml_converter->rootElement('tbody')->import($records, $doc)); |
|
82 | 9 | $doc->appendChild($table); |
|
83 | |||
84 | /** @var string $content */ |
||
85 | 9 | $content = $doc->saveHTML(); |
|
86 | |||
87 | 9 | return $content; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Creates a DOMElement representing a HTML table heading section. |
||
92 | */ |
||
93 | 9 | protected function appendHeaderSection(string $node_name, array $record, DOMElement $table): void |
|
94 | { |
||
95 | 9 | if ([] === $record) { |
|
96 | 6 | return; |
|
97 | } |
||
98 | |||
99 | /** @var DOMDocument $ownerDocument */ |
||
100 | 9 | $ownerDocument = $table->ownerDocument; |
|
101 | 9 | $node = $this->xml_converter |
|
102 | 9 | ->rootElement($node_name) |
|
103 | 9 | ->recordElement('tr') |
|
104 | 9 | ->fieldElement('th') |
|
105 | 9 | ->import([$record], $ownerDocument) |
|
106 | ; |
||
107 | |||
108 | /** @var DOMElement $element */ |
||
109 | 9 | foreach ($node->getElementsByTagName('th') as $element) { |
|
110 | 9 | $element->setAttribute('scope', 'col'); |
|
111 | } |
||
112 | |||
113 | 9 | $table->appendChild($node); |
|
114 | 9 | } |
|
115 | |||
116 | /** |
||
117 | * Adds class and id attributes to an HTML tag. |
||
118 | */ |
||
119 | 9 | protected function addHTMLAttributes(DOMElement $node): void |
|
124 | |||
125 | /** |
||
126 | * HTML table class name setter. |
||
127 | * |
||
128 | * @throws DOMException if the id_value contains any type of whitespace |
||
129 | */ |
||
130 | 6 | public function table(string $class_name, string $id_value = ''): self |
|
141 | |||
142 | /** |
||
143 | * HTML tr record offset attribute setter. |
||
144 | */ |
||
145 | 3 | public function tr(string $record_offset_attribute_name): self |
|
152 | |||
153 | /** |
||
154 | * HTML td field name attribute setter. |
||
155 | */ |
||
156 | 3 | public function td(string $fieldname_attribute_name): self |
|
163 | } |
||
164 |