Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
    private Worksheet $subject;
17
 
18
    /**
19
     * Current iterator position.
20
     */
21
    private int $position = 1;
22
 
23
    /**
24
     * Start position.
25
     */
26
    private int $startRow = 1;
27
 
28
    /**
29
     * End position.
30
     */
31
    private int $endRow = 1;
32
 
33
    /**
34
     * Create a new row iterator.
35
     *
36
     * @param Worksheet $subject The worksheet to iterate over
37
     * @param int $startRow The row number at which to start iterating
38
     * @param ?int $endRow Optionally, the row number at which to stop iterating
39
     */
40
    public function __construct(Worksheet $subject, int $startRow = 1, ?int $endRow = null)
41
    {
42
        // Set subject
43
        $this->subject = $subject;
44
        $this->resetEnd($endRow);
45
        $this->resetStart($startRow);
46
    }
47
 
48
    public function __destruct()
49
    {
50
        unset($this->subject);
51
    }
52
 
53
    /**
54
     * (Re)Set the start row and the current row pointer.
55
     *
56
     * @param int $startRow The row number at which to start iterating
57
     *
58
     * @return $this
59
     */
60
    public function resetStart(int $startRow = 1): static
61
    {
62
        if ($startRow > $this->subject->getHighestRow()) {
63
            throw new PhpSpreadsheetException(
64
                "Start row ({$startRow}) is beyond highest row ({$this->subject->getHighestRow()})"
65
            );
66
        }
67
 
68
        $this->startRow = $startRow;
69
        if ($this->endRow < $this->startRow) {
70
            $this->endRow = $this->startRow;
71
        }
72
        $this->seek($startRow);
73
 
74
        return $this;
75
    }
76
 
77
    /**
78
     * (Re)Set the end row.
79
     *
80
     * @param ?int $endRow The row number at which to stop iterating
81
     *
82
     * @return $this
83
     */
84
    public function resetEnd(?int $endRow = null): static
85
    {
86
        $this->endRow = $endRow ?: $this->subject->getHighestRow();
87
 
88
        return $this;
89
    }
90
 
91
    /**
92
     * Set the row pointer to the selected row.
93
     *
94
     * @param int $row The row number to set the current pointer at
95
     *
96
     * @return $this
97
     */
98
    public function seek(int $row = 1): static
99
    {
100
        if (($row < $this->startRow) || ($row > $this->endRow)) {
101
            throw new PhpSpreadsheetException("Row $row is out of range ({$this->startRow} - {$this->endRow})");
102
        }
103
        $this->position = $row;
104
 
105
        return $this;
106
    }
107
 
108
    /**
109
     * Rewind the iterator to the starting row.
110
     */
111
    public function rewind(): void
112
    {
113
        $this->position = $this->startRow;
114
    }
115
 
116
    /**
117
     * Return the current row in this worksheet.
118
     */
119
    public function current(): Row
120
    {
121
        return new Row($this->subject, $this->position);
122
    }
123
 
124
    /**
125
     * Return the current iterator key.
126
     */
127
    public function key(): int
128
    {
129
        return $this->position;
130
    }
131
 
132
    /**
133
     * Set the iterator to its next value.
134
     */
135
    public function next(): void
136
    {
137
        ++$this->position;
138
    }
139
 
140
    /**
141
     * Set the iterator to its previous value.
142
     */
143
    public function prev(): void
144
    {
145
        --$this->position;
146
    }
147
 
148
    /**
149
     * Indicate if more rows exist in the worksheet range of rows that we're iterating.
150
     */
151
    public function valid(): bool
152
    {
153
        return $this->position <= $this->endRow && $this->position >= $this->startRow;
154
    }
155
}