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