1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Spaghetti\XLSXParser; |
4
|
|
|
|
5
|
|
|
use Iterator; |
6
|
|
|
use XMLReader; |
7
|
|
|
|
8
|
|
|
use function count; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal |
12
|
|
|
*/ |
13
|
|
|
final class RowIterator implements Iterator |
14
|
|
|
{ |
15
|
|
|
private XMLReader $xml; |
16
|
|
|
private array $currentValue; |
17
|
|
|
private bool $valid; |
18
|
|
|
private ?int $currentKey; |
19
|
|
|
private readonly Transformer\Column $columnTransformer; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
private readonly Transformer\Value $valueTransformer, |
23
|
|
|
private readonly string $path, |
24
|
|
|
?Transformer\Column $columnTransformer = null, |
25
|
|
|
) { |
26
|
|
|
$this->columnTransformer = $columnTransformer ?? new Transformer\Column(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function current(): array |
30
|
|
|
{ |
31
|
|
|
return $this->currentValue; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function key(): int |
35
|
|
|
{ |
36
|
|
|
return $this->currentKey; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function next(): void |
40
|
|
|
{ |
41
|
|
|
$this->valid = false; |
42
|
|
|
|
43
|
|
|
$style = $type = $columnIndex = $row = $currentKey = null; |
44
|
|
|
|
45
|
|
|
while ($this->xml->read()) { |
46
|
|
|
if (XMLReader::ELEMENT === $this->xml->nodeType) { |
47
|
|
|
$this->process(columnIndex: $columnIndex, currentKey: $currentKey, row: $row, type: $type, style: $style); |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (XMLReader::END_ELEMENT === $this->xml->nodeType) { |
52
|
|
|
switch ($this->xml->name) { |
53
|
|
|
case 'row': |
54
|
|
|
if ($this->processEnd(row: $row, currentKey: $currentKey)) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
break; |
58
|
|
|
case 'sheetData': |
59
|
|
|
break 2; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function rewind(): void |
66
|
|
|
{ |
67
|
|
|
$xml = new XMLReader(); |
68
|
|
|
|
69
|
|
|
$this->xml = false === $xml->open(uri: $this->path) ? null : $xml; |
70
|
|
|
|
71
|
|
|
$this->next(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function valid(): bool |
75
|
|
|
{ |
76
|
|
|
return $this->valid; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function processEnd(?Row &$row, ?int &$currentKey): bool |
80
|
|
|
{ |
81
|
|
|
$currentValue = $row->getData(); |
|
|
|
|
82
|
|
|
if (count(value: $currentValue)) { |
83
|
|
|
$this->currentKey = $currentKey; |
84
|
|
|
$this->currentValue = $currentValue; |
85
|
|
|
$this->valid = true; |
86
|
|
|
|
87
|
|
|
return true; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function process(?int &$columnIndex, ?int &$currentKey, ?Row &$row, ?string &$type, ?string &$style): void |
94
|
|
|
{ |
95
|
|
|
switch ($this->xml->name) { |
96
|
|
|
case 'row': |
97
|
|
|
$currentKey = (int) $this->xml->getAttribute(name: 'r'); |
98
|
|
|
$row = new Row(); |
99
|
|
|
break; |
100
|
|
|
case 'c': |
101
|
|
|
$columnIndex = $this->columnTransformer->transform(name: $this->xml->getAttribute(name: 'r')); |
102
|
|
|
$style = $this->xml->getAttribute(name: 's') ?? ''; |
103
|
|
|
$type = $this->xml->getAttribute(name: 't') ?? ''; |
104
|
|
|
break; |
105
|
|
|
case 'v': |
106
|
|
|
$row->addValue( |
107
|
|
|
columnIndex: $columnIndex, |
|
|
|
|
108
|
|
|
value: $this->valueTransformer->transform(value: $this->xml->readString(), type: $type, style: $style), |
|
|
|
|
109
|
|
|
); |
110
|
|
|
break; |
111
|
|
|
default: |
112
|
|
|
$row?->addValue(columnIndex: $columnIndex, value: $this->xml->readString()); |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|