1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* League.Csv (https://csv.thephpleague.com) |
5
|
|
|
* |
6
|
|
|
* (c) Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace League\Csv; |
15
|
|
|
|
16
|
|
|
use DOMDocument; |
17
|
|
|
use DOMElement; |
18
|
|
|
use DOMException; |
19
|
|
|
use function preg_match; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Converts tabular data into an HTML Table string. |
23
|
|
|
*/ |
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 |
120
|
|
|
{ |
121
|
9 |
|
$node->setAttribute('class', $this->class_name); |
122
|
9 |
|
$node->setAttribute('id', $this->id_value); |
123
|
9 |
|
} |
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 |
131
|
|
|
{ |
132
|
6 |
|
if (1 === preg_match(",\s,", $id_value)) { |
133
|
3 |
|
throw new DOMException("the id attribute's value must not contain whitespace (spaces, tabs etc.)"); |
134
|
|
|
} |
135
|
3 |
|
$clone = clone $this; |
136
|
3 |
|
$clone->class_name = $class_name; |
137
|
3 |
|
$clone->id_value = $id_value; |
138
|
|
|
|
139
|
3 |
|
return $clone; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* HTML tr record offset attribute setter. |
144
|
|
|
*/ |
145
|
3 |
|
public function tr(string $record_offset_attribute_name): self |
146
|
|
|
{ |
147
|
3 |
|
$clone = clone $this; |
148
|
3 |
|
$clone->xml_converter = $this->xml_converter->recordElement('tr', $record_offset_attribute_name); |
149
|
|
|
|
150
|
3 |
|
return $clone; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* HTML td field name attribute setter. |
155
|
|
|
*/ |
156
|
3 |
|
public function td(string $fieldname_attribute_name): self |
157
|
|
|
{ |
158
|
3 |
|
$clone = clone $this; |
159
|
3 |
|
$clone->xml_converter = $this->xml_converter->fieldElement('td', $fieldname_attribute_name); |
160
|
|
|
|
161
|
3 |
|
return $clone; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|