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