Completed
Push — develop ( d383bc...d9bd45 )
by Adrien
23:59
created

RowCellIterator::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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\Cell\Coordinate;
6
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7
8
class RowCellIterator extends CellIterator
9
{
10
    /**
11
     * Current iterator position.
12
     *
13
     * @var int
14
     */
15
    private $currentColumnIndex;
16
17
    /**
18
     * Row index.
19
     *
20
     * @var int
21
     */
22
    private $rowIndex = 1;
23
24
    /**
25
     * Start position.
26
     *
27
     * @var int
28
     */
29
    private $startColumnIndex = 1;
30
31
    /**
32
     * End position.
33
     *
34
     * @var int
35
     */
36
    private $endColumnIndex = 1;
37
38
    /**
39
     * Create a new column iterator.
40
     *
41
     * @param Worksheet $worksheet The worksheet to iterate over
42
     * @param int $rowIndex The row that we want to iterate
43
     * @param string $startColumn The column address at which to start iterating
44
     * @param string $endColumn Optionally, the column address at which to stop iterating
45
     */
46 9 View Code Duplication
    public function __construct(Worksheet $worksheet = null, $rowIndex = 1, $startColumn = 'A', $endColumn = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        // Set subject and row index
49 9
        $this->worksheet = $worksheet;
50 9
        $this->rowIndex = $rowIndex;
51 9
        $this->resetEnd($endColumn);
52 9
        $this->resetStart($startColumn);
53 9
    }
54
55
    /**
56
     * (Re)Set the start column and the current column pointer.
57
     *
58
     * @param string $startColumn The column address at which to start iterating
59
     *
60
     * @throws PhpSpreadsheetException
61
     *
62
     * @return RowCellIterator
63
     */
64 9
    public function resetStart($startColumn = 'A')
65
    {
66 9
        $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn);
67 9
        $this->adjustForExistingOnlyRange();
68 9
        $this->seek(Coordinate::stringFromColumnIndex($this->startColumnIndex));
69
70 9
        return $this;
71
    }
72
73
    /**
74
     * (Re)Set the end column.
75
     *
76
     * @param string $endColumn The column address at which to stop iterating
77
     *
78
     * @throws PhpSpreadsheetException
79
     *
80
     * @return RowCellIterator
81
     */
82 9 View Code Duplication
    public function resetEnd($endColumn = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 9
        $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn();
85 9
        $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);
86 9
        $this->adjustForExistingOnlyRange();
87
88 9
        return $this;
89
    }
90
91
    /**
92
     * Set the column pointer to the selected column.
93
     *
94
     * @param string $column The column address to set the current pointer at
95
     *
96
     * @throws PhpSpreadsheetException
97
     *
98
     * @return RowCellIterator
99
     */
100 9 View Code Duplication
    public function seek($column = 'A')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 9
        $column = Coordinate::columnIndexFromString($column);
103 9
        if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) {
104
            throw new PhpSpreadsheetException("Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})");
105 9
        } elseif ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($column, $this->rowIndex))) {
106
            throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
107
        }
108 9
        $this->currentColumnIndex = $column;
109
110 9
        return $this;
111
    }
112
113
    /**
114
     * Rewind the iterator to the starting column.
115
     */
116 3
    public function rewind()
117
    {
118 3
        $this->currentColumnIndex = $this->startColumnIndex;
119 3
    }
120
121
    /**
122
     * Return the current cell in this worksheet row.
123
     *
124
     * @return \PhpOffice\PhpSpreadsheet\Cell\Cell
125
     */
126 5
    public function current()
127
    {
128 5
        return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex);
129
    }
130
131
    /**
132
     * Return the current iterator key.
133
     *
134
     * @return string
135
     */
136 3
    public function key()
137
    {
138 3
        return Coordinate::stringFromColumnIndex($this->currentColumnIndex);
139
    }
140
141
    /**
142
     * Set the iterator to its next value.
143
     */
144 5
    public function next()
145
    {
146 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147 5
            ++$this->currentColumnIndex;
148 5
        } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex <= $this->endColumnIndex));
149 5
    }
150
151
    /**
152
     * Set the iterator to its previous value.
153
     *
154
     * @throws PhpSpreadsheetException
155
     */
156 2
    public function prev()
157
    {
158 2 View Code Duplication
        if ($this->currentColumnIndex <= $this->startColumnIndex) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159 1
            throw new PhpSpreadsheetException('Column is already at the beginning of range (' . Coordinate::stringFromColumnIndex($this->endColumnIndex) . ' - ' . Coordinate::stringFromColumnIndex($this->endColumnIndex) . ')');
160
        }
161 View Code Duplication
        do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162 1
            --$this->currentColumnIndex;
163 1
        } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex >= $this->startColumnIndex));
164 1
    }
165
166
    /**
167
     * Indicate if more columns exist in the worksheet range of columns that we're iterating.
168
     *
169
     * @return bool
170
     */
171 5
    public function valid()
172
    {
173 5
        return $this->currentColumnIndex <= $this->endColumnIndex;
174
    }
175
176
    /**
177
     * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
178
     *
179
     * @throws PhpSpreadsheetException
180
     */
181 9 View Code Duplication
    protected function adjustForExistingOnlyRange()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
    {
183 9
        if ($this->onlyExistingCells) {
184
            while ((!$this->worksheet->cellExistsByColumnAndRow($this->startColumnIndex, $this->rowIndex)) && ($this->startColumnIndex <= $this->endColumnIndex)) {
185
                ++$this->startColumnIndex;
186
            }
187
            if ($this->startColumnIndex > $this->endColumnIndex) {
188
                throw new PhpSpreadsheetException('No cells exist within the specified range');
189
            }
190
            while ((!$this->worksheet->cellExistsByColumnAndRow($this->endColumnIndex, $this->rowIndex)) && ($this->endColumnIndex >= $this->startColumnIndex)) {
191
                --$this->endColumnIndex;
192
            }
193
            if ($this->endColumnIndex < $this->startColumnIndex) {
194
                throw new PhpSpreadsheetException('No cells exist within the specified range');
195
            }
196
        }
197 9
    }
198
}
199