Passed
Push — develop ( 125f46...5e090d )
by Adrien
26:19
created

RowIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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