-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRowIterator.php
143 lines (117 loc) · 2.77 KB
/
RowIterator.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
<?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\Bridge\Spout;
use OpenSpout\Common\Entity\Row;
use OpenSpout\Reader\RowIteratorInterface;
use Xezilaires\Iterator;
/**
* @internal
*/
final class RowIterator implements Iterator
{
private RowIteratorInterface $iterator;
private int $firstRow;
/**
* @psalm-suppress PropertyNotSetInConstructor
*/
private int $highestRow;
public function __construct(RowIteratorInterface $iterator, int $firstRow)
{
$iterator->rewind();
$this->iterator = $iterator;
$this->firstRow = $firstRow;
}
/**
* {@inheritdoc}
*/
public function current(): object
{
/**
* @var Row $row
*
* @psalm-suppress UnnecessaryVarAnnotation
*/
$row = $this->iterator->current();
/** @var array<int, null|float|int|string> $current */
$current = $row->toArray();
return new \ArrayObject($current);
}
/**
* {@inheritdoc}
*/
public function next(): void
{
$this->iterator->next();
}
/**
* {@inheritdoc}
*/
public function key(): int
{
/** @var int $key */
$key = $this->iterator->key();
return $key;
}
/**
* {@inheritdoc}
*/
public function valid(): bool
{
return $this->iterator->valid();
}
/**
* {@inheritdoc}
*/
public function rewind(): void
{
$this->seek($this->firstRow);
}
/**
* {@inheritdoc}
*/
public function seek(int $rowIndex): void
{
$currentIndex = $this->key();
if ($currentIndex > $rowIndex) {
$this->iterator->rewind();
--$rowIndex;
} else {
$rowIndex -= $currentIndex;
}
for ($x = 1; $x <= $rowIndex; ++$x) {
$this->next();
}
}
/**
* {@inheritdoc}
*/
public function prev(): void
{
$this->seek($this->key() - 1);
}
public function getHighestRow(): int
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
if (isset($this->highestRow) === false) {
$highestRow = 0;
$this->iterator->rewind();
while ($this->iterator->valid()) {
++$highestRow;
$this->iterator->next();
}
// NOTE: Spout goes out of bounds, but the index is not incremented
// bug workaround
$this->prev();
$this->highestRow = $highestRow;
}
return $this->highestRow;
}
}