Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
    public function __construct(Worksheet $subject, $startRow = 1, $endRow = null)
49
    {
50
        // Set subject
51
        $this->subject = $subject;
52
        $this->resetEnd($endRow);
53
        $this->resetStart($startRow);
54
    }
55
 
56
    public function __destruct()
57
    {
58
        $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
    public function resetStart(int $startRow = 1)
69
    {
70
        if ($startRow > $this->subject->getHighestRow()) {
71
            throw new PhpSpreadsheetException(
72
                "Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})"
73
            );
74
        }
75
 
76
        $this->startRow = $startRow;
77
        if ($this->endRow < $this->startRow) {
78
            $this->endRow = $this->startRow;
79
        }
80
        $this->seek($startRow);
81
 
82
        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
    public function resetEnd($endRow = null)
93
    {
94
        $this->endRow = $endRow ?: $this->subject->getHighestRow();
95
 
96
        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
    public function seek(int $row = 1)
107
    {
108
        if (($row < $this->startRow) || ($row > $this->endRow)) {
109
            throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
110
        }
111
        $this->position = $row;
112
 
113
        return $this;
114
    }
115
 
116
    /**
117
     * Rewind the iterator to the starting row.
118
     */
119
    public function rewind(): void
120
    {
121
        $this->position = $this->startRow;
122
    }
123
 
124
    /**
125
     * Return the current row in this worksheet.
126
     */
127
    public function current(): Row
128
    {
129
        return new Row($this->subject, $this->position);
130
    }
131
 
132
    /**
133
     * Return the current iterator key.
134
     */
135
    public function key(): int
136
    {
137
        return $this->position;
138
    }
139
 
140
    /**
141
     * Set the iterator to its next value.
142
     */
143
    public function next(): void
144
    {
145
        ++$this->position;
146
    }
147
 
148
    /**
149
     * Set the iterator to its previous value.
150
     */
151
    public function prev(): void
152
    {
153
        --$this->position;
154
    }
155
 
156
    /**
157
     * Indicate if more rows exist in the worksheet range of rows that we're iterating.
158
     */
159
    public function valid(): bool
160
    {
161
        return $this->position <= $this->endRow && $this->position >= $this->startRow;
162
    }
163
}