-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlTableFromNode.php
More file actions
285 lines (215 loc) · 9.79 KB
/
HtmlTableFromNode.php
File metadata and controls
285 lines (215 loc) · 9.79 KB
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
<?php
declare( strict_types = 1 );
namespace TheWebSolver\Codegarage\Scraper\Traits\Table;
use DOMNode;
use Iterator;
use DOMElement;
use ArrayObject;
use DOMNodeList;
use TheWebSolver\Codegarage\Scraper\Enums\Table;
use TheWebSolver\Codegarage\Scraper\Enums\EventAt;
use TheWebSolver\Codegarage\Scraper\Data\EmptyItem;
use TheWebSolver\Codegarage\Scraper\Data\TableCell;
use TheWebSolver\Codegarage\Scraper\Data\TableHead;
use TheWebSolver\Codegarage\Scraper\AssertDOMElement;
use TheWebSolver\Codegarage\Scraper\Event\TableTraced;
use TheWebSolver\Codegarage\Scraper\Data\CollectionSet;
use TheWebSolver\Codegarage\Scraper\DOMDocumentFactory;
use TheWebSolver\Codegarage\Scraper\Error\InvalidSource;
use TheWebSolver\Codegarage\Scraper\Interfaces\Transformer;
use TheWebSolver\Codegarage\Scraper\Traits\Table\TableExtractor;
/** @template TableColumnValue */
trait HtmlTableFromNode {
/** @use TableExtractor<TableColumnValue> */
use TableExtractor;
/** @throws InvalidSource When "table" cannot be resolved in given source. */
public function inferFrom( string|DOMElement $source, bool $normalize = true ): void {
$source = $this->getValidatedTableSource( $source, $normalize );
if ( $source instanceof DOMNodeList ) {
$this->inferTableFromDOMNodeList( $source );
return;
}
$this->inferChildNodesFromTable( $source );
}
protected function useCurrentIterationValidatedHead( mixed $node ): TableHead {
if ( $isValid = AssertDOMElement::isValid( $node, Table::Head ) ) {
return new TableHead( $isValid, isAllowed: true, value: trim( $node->textContent ) );
}
$isAllowed = $node instanceof DOMNode && XML_COMMENT_NODE === $node->nodeType;
return new TableHead( $isValid, $isAllowed, value: null );
}
/** @param Transformer<static,string> $transformer */
protected function transformCurrentIterationTableHead( mixed $node, Transformer $transformer ): string {
return $transformer->transform( $this->assertThingIsValidNode( $node ), $this );
}
protected function getTagnameFrom( mixed $thing ): mixed {
return $thing instanceof DOMElement ? $thing->tagName : null;
}
/**
* @param Transformer<static,TableColumnValue> $transformer
* @return TableCell<TableColumnValue>
*/
protected function transformCurrentIterationTableColumn( mixed $node, Transformer $transformer ): TableCell {
return new TableCell(
position: 0,
value: $transformer->transform( $column = $this->assertThingIsValidNode( $node ), $this ),
rowspan: (int) ( $column->getAttribute( 'rowspan' ) ?: 1 )
);
}
/** @param ?TableColumnValue $value */
protected function afterCurrentTableColumnRegistered( mixed $column, mixed $value ): void {
$column = $this->assertThingIsValidNode( $column );
$value && $this->findTableStructureIn( node: $column, minChildNodesCount: 1 );
}
private function inferChildNodesFromTable( DOMElement $element ): bool {
$table = $this->ensureTableWithChildNodesStructure( $element );
if ( ! $table || ! $tableStructure = $this->traceTableStructureIn( $table ) ) {
return false;
}
[$bodyNode, $captionNode, $headNode] = $tableStructure;
$id = spl_object_id( $element ) * spl_object_id( $bodyNode );
$this->dispatchEventForTable( $id, $bodyNode );
$captionNode && $this->captionStructureContentFrom( $captionNode );
$headNode && $this->headStructureContentFrom( $headNode );
$iterator = $this->bodyStructureIteratorFrom( $bodyNode );
$iterator->valid() && ( $this->discoveredTable__rows[ $id ] = $iterator );
$this->dispatchEvent( new TableTraced( Table::TBody, EventAt::End, $element, $this ) );
return true;
}
/** @param DOMNodeList<DOMNode> $elementList */
private function inferTableFromDOMNodeList( DOMNodeList $elementList ): void {
foreach ( $elementList as $node ) {
if ( ! AssertDOMElement::isValid( $node ) || ! $this->inferChildNodesFromTable( $node ) ) {
continue;
}
if ( $this->targetIsCurrentTable( $node ) ) {
break;
}
}
}
private function findTableStructureIn( DOMElement $node, int $minChildNodesCount = 0 ): void {
( ! $this->getTableId() || $this->shouldPerform__allTableDiscovery )
&& ( ( $nodes = $node->childNodes )->length > $minChildNodesCount )
&& $this->inferTableFromDOMNodeList( $nodes );
}
/** @phpstan-assert-if-true =DOMElement $node */
private function isTableRowStructure( DOMNode $node ): bool {
return $node->childNodes->length && AssertDOMElement::isValid( $node, Table::Row );
}
private function ensureTableWithChildNodesStructure( DOMElement $element ): ?DOMElement {
if ( 'table' !== $element->tagName ) {
$this->findTableStructureIn( $element );
return null;
}
if ( $isTarget = $this->isTargetedTable( $element ) ) {
$this->registerTargetedTable( $element );
}
return $isTarget && $element->childNodes->length ? $element : null;
}
private function captionStructureContentFrom( DOMElement $node ): void {
$this->dispatchEvent( new TableTraced( Table::Caption, EventAt::Start, $node, $this ) );
$transformer = $this->discoveredTable__transformers[ Table::Caption->value ] ?? null;
$content = $transformer?->transform( $node, $this ) ?? trim( $node->textContent );
$this->discoveredTable__captions[ $this->currentTable__id ] = $content;
$this->dispatchEvent( new TableTraced( Table::Caption, EventAt::End, $node, $this ) );
}
private function headStructureContentFrom( DOMElement $node ): void {
$this->dispatchEvent( $event = new TableTraced( Table::THead, EventAt::Start, $node, $this ) );
if ( $event->shouldStopTrace() ) {
$this->dispatchEvent( new TableTraced( Table::THead, EventAt::End, $node, $this ) );
return;
}
$headRow = $node->getElementsByTagName( Table::Head->value );
$headRow->length && $this->inferTableHeadFrom( $headRow );
$this->dispatchEvent( new TableTraced( Table::THead, EventAt::End, $node, $this ) );
}
/** @return ?array{0:DOMElement,1:?DOMElement,2:?DOMElement} */
private function traceTableStructureIn( DOMElement $element ): ?array {
if ( ! $bodyNode = $element->getElementsByTagName( Table::TBody->value )->item( 0 ) ) {
return null;
}
if ( ! $this->tableColumnsExistInBody( $bodyNode ) ) {
return null;
}
$captionNode = $element->getElementsByTagName( Table::Caption->value )->item( 0 );
$headNode = $element->getElementsByTagName( Table::THead->value )->item( 0 );
return [
$bodyNode,
$this->shouldTraceTableStructure( Table::Caption ) ? $captionNode : null,
$this->shouldTraceTableStructure( Table::THead ) ? $headNode : null,
];
}
/**
* @param DOMElement $body
* @return Iterator<array-key,ArrayObject<array-key,TableColumnValue>>
*/
private function bodyStructureIteratorFrom( DOMElement $body ): Iterator {
if ( ! ( $rowList = $body->getElementsByTagName( Table::Row->value ) )->length ) {
return;
}
[$headInspected, $bodyStarted, $position] = $this->useCurrentTableBodyDetails();
foreach ( $rowList as $row ) {
$isHead = ! $headInspected && $this->inspectFirstRowForHeadStructure( $row );
$headInspected = true;
if ( $isHead ) {
// We can only determine whether first row contains table heads after it is inferred.
// We'll simply dispatch the ending event here to notify subscribers, if any.
$this->dispatchEvent( new TableTraced( Table::THead, EventAt::End, $row, $this ) );
// Contents of <tr> as head MUST NOT BE COLLECTED as table column also.
// Advance table body to next <tr> if first row is collected as head.
continue;
}
if ( ! $bodyStarted ) {
$this->hydrateIndicesSourceFromAttribute();
$this->dispatchEvent( $event = new TableTraced( Table::Row, EventAt::Start, $body, $this ) );
// Although not recommended, it is entirely possible to stop inferring further table rows.
// This just means that tracer was used to trace "<th>" that were present in "<tbody>".
if ( $event->shouldStopTrace() ) {
break;
}
$bodyStarted = true;
}
// TODO: add support whether to skip yielding empty <tr> or not.
if ( ! trim( $row->textContent ) ) {
continue;
}
$transformer = $this->discoveredTable__transformers[ Table::Row->value ] ?? null;
$content = $transformer?->transform( $row, $this ) ?? $row->childNodes;
if ( $content instanceof CollectionSet ) {
$this->registerCurrentTableDatasetCount( $content->value->count() );
yield $content->key => $content->value;
} else {
$dataset = $content instanceof ArrayObject ? $content : new ArrayObject( $this->inferTableDataFrom( $content ) );
$this->registerCurrentTableDatasetCount( $dataset->count() );
yield $dataset;
}
$this->registerCurrentIterationTableRow( ++$position );
}//end foreach
$this->dispatchEvent( new TableTraced( Table::Row, EventAt::End, $body, $this ) );
}
private function inspectFirstRowForHeadStructure( DOMElement $row ): bool {
$this->inferTableHeadFrom( $row->childNodes );
return $this->currentIteration__allTableHeads;
}
private function tableColumnsExistInBody( DOMElement $body ): bool {
return ! ! $body->getElementsByTagName( 'td' )->length;
}
/**
* @return DOMElement|DOMNodeList<DOMNode>
* @throws InvalidSource When source invalid.
*/
private function getValidatedTableSource( string|DOMElement $source, bool $normalize ): DOMElement|DOMNodeList {
if ( ! $source instanceof DOMElement ) {
return DOMDocumentFactory::bodyFromHtml( $source, normalize: $normalize )->childNodes;
}
'table' !== $source->tagName && throw new InvalidSource(
sprintf( '%s trait only supports table "DOMElement"', HtmlTableFromNode::class )
);
return $source;
}
/** @phpstan-assert DOMElement $node */
private function assertThingIsValidNode( mixed $node ): DOMElement {
$node instanceof EmptyItem && $node = new DOMElement( 'td', EmptyItem::class );
return $node instanceof DOMElement ? $node : $this->throwUnsupportedNodeType( $node, HtmlTableFromNode::class );
}
}