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