-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpreadsheetIterator.php
250 lines (209 loc) · 6.7 KB
/
SpreadsheetIterator.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
<?php
declare(strict_types=1);
/*
* This file is part of the xezilaires project.
*
* (c) sigwin.hr
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Xezilaires;
use Xezilaires\Exception\DenormalizerException;
use Xezilaires\Exception\MappingException;
use Xezilaires\Metadata\ArrayReference;
use Xezilaires\Metadata\ColumnReference;
use Xezilaires\Metadata\HeaderReference;
use Xezilaires\Metadata\Mapping;
use Xezilaires\Metadata\Reference;
final class SpreadsheetIterator implements Iterator
{
private Spreadsheet $spreadsheet;
private Mapping $mapping;
private Denormalizer $denormalizer;
private array $context;
/**
* @psalm-suppress PropertyNotSetInConstructor
*/
private Iterator $iterator;
/**
* @var array<string, array<int, string>|string>
*
* @psalm-suppress PropertyNotSetInConstructor
*/
private array $headers;
private int $index = 0;
public function __construct(Spreadsheet $spreadsheet, Mapping $mapping, Denormalizer $denormalizer, array $context = [])
{
$this->spreadsheet = $spreadsheet;
$this->mapping = $mapping;
$this->denormalizer = $denormalizer;
$this->context = $context;
}
/**
* {@inheritdoc}
*/
public function current(): object
{
$row = $this->spreadsheet->getCurrentRow();
/** @var array<string, null|array<null|float|int|string>|float|int|string> $data */
$data = [];
foreach ($this->mapping->getReferences() as $name => $reference) {
if ($reference instanceof ArrayReference) {
$data[$name] = $this->readArrayReference($row, $reference);
} else {
$data[$name] = $this->readReference($row, $reference);
}
}
try {
return $this->denormalizer->denormalize($data, $this->mapping->getClassName(), null, $this->context);
} catch (Exception $exception) {
throw DenormalizerException::denormalizationFailed($exception);
}
}
/**
* {@inheritdoc}
*/
public function prev(): void
{
--$this->index;
$this->getIterator()->prev();
}
/**
* {@inheritdoc}
*/
public function next(): void
{
++$this->index;
$this->getIterator()->next();
}
/**
* {@inheritdoc}
*/
public function key(): int
{
/** @var bool $sequential */
$sequential = $this->mapping->getOption('sequential');
if ($sequential) {
return $this->index;
}
return $this->getIterator()->key();
}
/**
* {@inheritdoc}
*/
public function valid(): bool
{
return $this->getIterator()->valid();
}
/**
* {@inheritdoc}
*/
public function rewind(): void
{
$this->index = 0;
$this->getIterator()->rewind();
}
/**
* {@inheritdoc}
*/
public function seek(int $rowIndex): void
{
/** @var int $start */
$start = $this->mapping->getOption('start');
$this->index = $rowIndex;
$this->getIterator()->seek($start + $rowIndex);
}
private function getIterator(): Iterator
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->iterator) === false) {
/** @var int $start */
$start = $this->mapping->getOption('start');
$this->spreadsheet->createIterator($start);
$iterator = $this->spreadsheet->getIterator();
$reverse = $this->mapping->getOption('reverse');
if ($reverse === true) {
$iterator = new ReverseIterator($iterator, $start, $this->spreadsheet->getHighestRow());
}
$this->iterator = $iterator;
}
return $this->iterator;
}
/**
* @return array<string, array<int, string>|string>
*/
private function getHeaderColumnReferences(): array
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->headers) === false) {
/** @var null|int $headerRowIndex */
$headerRowIndex = $this->mapping->getOption('header');
if ($headerRowIndex === null) {
throw MappingException::missingHeaderOption();
}
/** @var array<string, null|string> $headerRow */
$headerRow = $this->spreadsheet->getRow($headerRowIndex);
$this->headers = [];
foreach ($headerRow as $column => $header) {
if ($header === null) {
continue;
}
if (isset($this->headers[$header])) {
if (\is_string($this->headers[$header])) {
$this->headers[$header] = (array) $this->headers[$header];
}
$this->headers[$header][] = $column;
} else {
$this->headers[$header] = $column;
}
}
}
return $this->headers;
}
private function getColumnByHeader(string $header): string
{
$headerColumnReferences = $this->getHeaderColumnReferences();
if (false === \array_key_exists($header, $headerColumnReferences)) {
throw MappingException::headerNotFound($header, array_keys($headerColumnReferences));
}
if (\is_array($headerColumnReferences[$header])) {
throw MappingException::ambiguousHeader($header, $headerColumnReferences[$header]);
}
return $headerColumnReferences[$header];
}
/**
* @param array<string, null|float|int|string> $row
*
* @return array<null|float|int|string>
*/
private function readArrayReference(array $row, ArrayReference $reference): array
{
$data = [];
foreach ($reference->getReference() as $arrayReference) {
$data[] = $this->readReference($row, $arrayReference);
}
return $data;
}
/**
* @param array<string, null|float|int|string> $row
*
* @return null|float|int|string
*/
private function readReference(array $row, Reference $reference)
{
switch (true) {
case $reference instanceof ColumnReference:
$column = $reference->getReference();
$data = $row[$column];
break;
case $reference instanceof HeaderReference:
$column = $this->getColumnByHeader($reference->getReference());
$data = $row[$column];
break;
default:
throw MappingException::unexpectedReference();
}
return $data;
}
}