-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTableBinder.php
363 lines (321 loc) · 10.4 KB
/
TableBinder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
namespace Gt\DomTemplate;
use Gt\Dom\Document;
use Gt\Dom\Element;
use Gt\Dom\ElementType;
use Traversable;
class TableBinder {
/**
* @noinspection PhpPropertyOnlyWrittenInspection
* @phpstan-ignore-next-line
*/
private ListBinder $listBinder;
private ListElementCollection $templateCollection;
private ElementBinder $elementBinder;
private HTMLAttributeBinder $htmlAttributeBinder;
private HTMLAttributeCollection $htmlAttributeCollection;
private PlaceholderBinder $placeholderBinder;
public function setDependencies(
ListBinder $listBinder,
ListElementCollection $listElementCollection,
ElementBinder $elementBinder,
HTMLAttributeBinder $htmlAttributeBinder,
HTMLAttributeCollection $htmlAttributeCollection,
PlaceholderBinder $placeholderBinder,
):void {
$this->listBinder = $listBinder;
$this->templateCollection = $listElementCollection;
$this->elementBinder = $elementBinder;
$this->htmlAttributeBinder = $htmlAttributeBinder;
$this->htmlAttributeCollection = $htmlAttributeCollection;
$this->placeholderBinder = $placeholderBinder;
}
/**
* @param array<int, array<int, string>>|array<int, array<int|string, string|array<int, mixed>>> $tableData
* @param Element $context
*/
public function bindTableData(
array $tableData,
Document|Element $context,
?string $bindKey = null
):void {
$tableData = $this->normaliseTableData($tableData);
if($context instanceof Document) {
$context = $context->documentElement;
}
$this->initBinders();
$tableArray = [$context];
if($context->elementType !== ElementType::HTMLTableElement) {
$tableArray = [];
foreach($context->querySelectorAll("table") as $table) {
array_push($tableArray, $table);
}
}
foreach($tableArray as $i => $table) {
$dataBindTableAttr = "data-bind:table";
$dataBindTableElement = $table;
if(!$dataBindTableElement->hasAttribute($dataBindTableAttr)) {
$dataBindTableElement = $table->closest("[data-bind:table]") ?? $table;
}
if(!$dataBindTableElement
|| $dataBindTableElement->getAttribute("data-bind:table") != $bindKey) {
unset($tableArray[$i]);
}
}
if(empty($tableArray)) {
throw new TableElementNotFoundInContextException();
}
$headerRow = array_shift($tableData);
foreach($tableArray as $table) {
$allowedHeaders = $headerRow;
$tHead = $table->tHead;
if($tHead) {
$allowedHeaders = [];
$tHeadRow = $tHead->rows[0];
foreach($tHeadRow->cells as $cell) {
$headerKey = $cell->hasAttribute("data-table-key")
? $cell->getAttribute("data-table-key")
: trim($cell->textContent);
array_push($allowedHeaders, $headerKey);
}
}
else {
$tHead = $table->createTHead();
$theadTr = $tHead->insertRow();
foreach($headerRow as $value) {
$th = $theadTr->insertCell();
$th->textContent = $value;
}
}
$tbody = $table->tBodies[0] ?? null;
if(!$tbody) {
$tbody = $table->createTBody();
}
$templateCollection = $this->templateCollection
?? new ListElementCollection($context->ownerDocument);
foreach($tableData as $rowData) {
try {
$trTemplate = $templateCollection->get($tbody);
$tr = $trTemplate->insertListItem();
}
catch(ListElementNotFoundInContextException) {
$tr = $tbody->insertRow();
}
/** @var int|string|null $firstKey */
$firstKey = key($rowData);
foreach($allowedHeaders as $headerIndex => $allowedHeader) {
$rowIndex = array_search($allowedHeader, $headerRow);
$cellTypeToCreate = "td";
if(is_string($firstKey)) {
if($rowIndex === 0) {
$columnValue = $firstKey;
$cellTypeToCreate = "th";
}
else {
$columnValue = $rowData[$firstKey][$rowIndex - 1];
}
}
else {
if(false === $rowIndex) {
$columnValue = "";
}
else {
$columnValue = $rowData[$rowIndex];
}
}
/** @var string|null $columnValue */
if($headerIndex < $tr->cells->length - 1) {
if(false === $rowIndex) {
continue;
}
$cellElement = $tr->cells[$headerIndex];
}
else {
$cellElement = $tr->ownerDocument->createElement($cellTypeToCreate);
}
$cellElement->textContent = $columnValue ?? "";
if(!$cellElement->parentElement) {
$tr->appendChild($cellElement);
}
}
foreach($rowData as $index => $value) {
$headerRowIndex = $index;
if(!is_int($index)) {
$headerRowIndex = null;
foreach($tableData as $tableDataIndex => $tableDatum) {
if($index === key($tableDatum)) {
$headerRowIndex = $tableDataIndex;
break;
}
}
}
$key = $headerRow[$headerRowIndex];
$this->elementBinder->bind(
$key,
$value,
$tr
);
}
}
}
}
/** @param array<int, array<int,string>> | array<int, array<string, string>> | array<string, array<int, string>> | array<int, array<int, string>> | array<string, string> $array */
public function detectTableDataStructureType(array $array):TableDataStructureType {
if(empty($array)) {
return TableDataStructureType::NORMALISED;
}
reset($array);
if(array_is_list($array)) {
$allRowsAreLists = true;
$allRowDataAreLists = true;
$allRowDataAreAssoc = true;
foreach($array as $rowIndex => $rowData) {
if(!is_array($rowData)) {
throw new IncorrectTableDataFormat("Row $rowIndex data is not iterable");
}
if(array_is_list($rowData)) {
$allRowDataAreAssoc = false;
}
else {
$allRowsAreLists = false;
}
/**
* @var int|string $cellIndex
* @var string|array<string> $cellData
*/
foreach($rowData as $cellIndex => $cellData) {
if($rowIndex > 0) {
if(!empty($array[0]) && array_is_list($array[0]) && !array_is_list($rowData)) {
if(!is_iterable($cellData)) {
throw new IncorrectTableDataFormat("Row $rowIndex has a string key ($cellIndex) but the value is not iterable.");
}
}
if(!is_array($cellData) || !array_is_list($cellData)) {
$allRowDataAreLists = false;
}
}
}
}
if($allRowsAreLists) {
return TableDataStructureType::NORMALISED;
}
else {
if($allRowDataAreLists) {
return TableDataStructureType::DOUBLE_HEADER;
}
elseif($allRowDataAreAssoc) {
return TableDataStructureType::ASSOC_ROW;
}
}
}
else {
$allRowDataAreLists = true;
foreach($array as $rowIndex => $rowData) {
if(!is_array($rowData)) {
throw new IncorrectTableDataFormat("Column data \"$rowIndex\" is not iterable.");
}
if(!array_is_list($rowData)) {
$allRowDataAreLists = false;
}
}
if($allRowDataAreLists) {
return TableDataStructureType::HEADER_VALUE_LIST;
}
}
throw new IncorrectTableDataFormat();
}
/**
* @param iterable<int,iterable<int,string>>|iterable<int,iterable<string,string>>|iterable<string,iterable<int,string>>|iterable<int, iterable<int,string>|iterable<string,string>> $bindValue
* The structures allowed by this method are:
*
* 1) iterable<int, iterable<int,string>> If $bindValue has keys of type
* int, and the value of index 0 is an iterable of strings, then the
* value of index 0 must represent the columnHeaders; subsequent values
* must represent the columnValues.
* 2) iterable<int, iterable<int,string>|iterable<string,string>>
* Similar to structure 1, but with a key difference. If the value of
* index 0 is an iterable of strings, BUT the next value is an iterable
* with keys of type string, this represents "double header" data - the
* returned normalised value retains this double header data so the
* binder can insert <th> elements in the <tbody>.
* 3) iterable<int, iterable<string,string>> If $bindValue has keys of
* type int, and the value of index 0 is associative, then the value of
* each index must represent the individual rows, where the
* columnHeaders are the string key of the inner iterable, and the
* columnValues are the string value of the inner iterable.
* 4) iterable<string,iterable<int,string>> If $bindValue has keys of
* type string, the keys must represent the columnHeaders and the values
* must represent the columnValues.
*
* @return array<int, array<int, string>|array<string,array<int,string>>>
* A two-dimensional array where the outer array represents the rows,
* the inner array represents the columns. The first index's value is
* always the columnHeaders. The other index's values are always the
* columnValues. Typically, columnValues will be array<int,string> apart
* from when the data represents double-header tables, in which case the
* columnValues will be within array<string,array<int,string>>.
*/
private function normaliseTableData(iterable $bindValue):array {
$normalised = [];
if($bindValue instanceof Traversable) {
$bindValue = iterator_to_array($bindValue);
}
$structureType = $this->detectTableDataStructureType($bindValue);
if($structureType === TableDataStructureType::NORMALISED) {
$normalised = $bindValue;
}
elseif($structureType === TableDataStructureType::ASSOC_ROW) {
$headers = [];
foreach($bindValue as $row) {
if(empty($headers)) {
$headers = array_keys($row);
array_push($normalised, $headers);
}
$normalisedRow = [];
foreach($headers as $h) {
array_push($normalisedRow, $row[$h]);
}
array_push($normalised, $normalisedRow);
}
}
elseif($structureType === TableDataStructureType::HEADER_VALUE_LIST) {
$headers = array_keys($bindValue);
array_push($normalised, $headers);
foreach($bindValue[$headers[0]] as $rowIndex => $ignored) {
$row = [];
foreach($headers as $h) {
array_push($row, $bindValue[$h][$rowIndex]);
}
array_push($normalised, $row);
}
}
elseif($structureType === TableDataStructureType::DOUBLE_HEADER) {
$headers = $bindValue[0];
$rows = [];
foreach($bindValue[1] ?? [] as $thValue => $bindValueRow) {
array_push($rows, [
$thValue => $bindValueRow,
]);
}
$normalised = [
$headers,
...$rows,
];
}
return $normalised;
}
private function initBinders():void {
if(!isset($this->htmlAttributeBinder)) {
$this->htmlAttributeBinder = new HTMLAttributeBinder();
}
if(!isset($this->htmlAttributeCollection)) {
$this->htmlAttributeCollection = new HTMLAttributeCollection();
}
if(!isset($this->placeholderBinder)) {
$this->placeholderBinder = new PlaceholderBinder();
}
if(!isset($this->elementBinder)) {
$this->elementBinder = new ElementBinder();
}
}
}