Completed
Push — master ( da3bcc...1194b2 )
by Mark
32s queued 27s
created

RowIterator::resetEnd()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
use Iterator as NativeIterator;
6
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7
8
/**
9
 * @implements NativeIterator<int, Row>
10
 */
11
class RowIterator implements NativeIterator
12
{
13
    /**
14
     * Worksheet to iterate.
15
     *
16
     * @var Worksheet
17
     */
18
    private $subject;
19
20
    /**
21
     * Current iterator position.
22
     *
23
     * @var int
24
     */
25
    private $position = 1;
26
27
    /**
28
     * Start position.
29
     *
30
     * @var int
31
     */
32
    private $startRow = 1;
33
34
    /**
35
     * End position.
36
     *
37
     * @var int
38
     */
39
    private $endRow = 1;
40
41
    /**
42
     * Create a new row iterator.
43
     *
44
     * @param Worksheet $subject The worksheet to iterate over
45
     * @param int $startRow The row number at which to start iterating
46
     * @param int $endRow Optionally, the row number at which to stop iterating
47
     */
48 115
    public function __construct(Worksheet $subject, $startRow = 1, $endRow = null)
49
    {
50
        // Set subject
51 115
        $this->subject = $subject;
52 115
        $this->resetEnd($endRow);
53 115
        $this->resetStart($startRow);
54
    }
55
56 114
    public function __destruct()
57
    {
58 114
        $this->subject = null; // @phpstan-ignore-line
59
    }
60
61
    /**
62
     * (Re)Set the start row and the current row pointer.
63
     *
64
     * @param int $startRow The row number at which to start iterating
65
     *
66
     * @return $this
67
     */
68 115
    public function resetStart(int $startRow = 1)
69
    {
70 115
        if ($startRow > $this->subject->getHighestRow()) {
71 3
            throw new PhpSpreadsheetException(
72 3
                "Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})"
73 3
            );
74
        }
75
76 114
        $this->startRow = $startRow;
77 114
        if ($this->endRow < $this->startRow) {
78 1
            $this->endRow = $this->startRow;
79
        }
80 114
        $this->seek($startRow);
81
82 114
        return $this;
83
    }
84
85
    /**
86
     * (Re)Set the end row.
87
     *
88
     * @param int $endRow The row number at which to stop iterating
89
     *
90
     * @return $this
91
     */
92 115
    public function resetEnd($endRow = null)
93
    {
94 115
        $this->endRow = $endRow ?: $this->subject->getHighestRow();
95
96 115
        return $this;
97
    }
98
99
    /**
100
     * Set the row pointer to the selected row.
101
     *
102
     * @param int $row The row number to set the current pointer at
103
     *
104
     * @return $this
105
     */
106 114
    public function seek(int $row = 1)
107
    {
108 114
        if (($row < $this->startRow) || ($row > $this->endRow)) {
109 1
            throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
110
        }
111 114
        $this->position = $row;
112
113 114
        return $this;
114
    }
115
116
    /**
117
     * Rewind the iterator to the starting row.
118
     */
119 65
    public function rewind(): void
120
    {
121 65
        $this->position = $this->startRow;
122
    }
123
124
    /**
125
     * Return the current row in this worksheet.
126
     */
127 109
    public function current(): Row
128
    {
129 109
        return new Row($this->subject, $this->position);
130
    }
131
132
    /**
133
     * Return the current iterator key.
134
     */
135 4
    public function key(): int
136
    {
137 4
        return $this->position;
138
    }
139
140
    /**
141
     * Set the iterator to its next value.
142
     */
143 66
    public function next(): void
144
    {
145 66
        ++$this->position;
146
    }
147
148
    /**
149
     * Set the iterator to its previous value.
150
     */
151 2
    public function prev(): void
152
    {
153 2
        --$this->position;
154
    }
155
156
    /**
157
     * Indicate if more rows exist in the worksheet range of rows that we're iterating.
158
     */
159 68
    public function valid(): bool
160
    {
161 68
        return $this->position <= $this->endRow && $this->position >= $this->startRow;
162
    }
163
}
164