Completed
Push — develop ( 1cec98...8d7602 )
by Adrien
24:39
created

RowCellIterator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 4
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
crap 1
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
     * Destructor.
57
     */
58 9
    public function __destruct()
59
    {
60 9
        unset($this->worksheet);
61 9
    }
62
63
    /**
64
     * (Re)Set the start column and the current column pointer.
65
     *
66
     * @param string $startColumn The column address at which to start iterating
67
     *
68
     * @throws PhpSpreadsheetException
69
     *
70
     * @return RowCellIterator
71
     */
72 9
    public function resetStart($startColumn = 'A')
73
    {
74 9
        $this->startColumnIndex = Coordinate::columnIndexFromString($startColumn);
75 9
        $this->adjustForExistingOnlyRange();
76 9
        $this->seek(Coordinate::stringFromColumnIndex($this->startColumnIndex));
77
78 9
        return $this;
79
    }
80
81
    /**
82
     * (Re)Set the end column.
83
     *
84
     * @param string $endColumn The column address at which to stop iterating
85
     *
86
     * @throws PhpSpreadsheetException
87
     *
88
     * @return RowCellIterator
89
     */
90 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...
91
    {
92 9
        $endColumn = $endColumn ? $endColumn : $this->worksheet->getHighestColumn();
93 9
        $this->endColumnIndex = Coordinate::columnIndexFromString($endColumn);
94 9
        $this->adjustForExistingOnlyRange();
95
96 9
        return $this;
97
    }
98
99
    /**
100
     * Set the column pointer to the selected column.
101
     *
102
     * @param string $column The column address to set the current pointer at
103
     *
104
     * @throws PhpSpreadsheetException
105
     *
106
     * @return RowCellIterator
107
     */
108 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...
109
    {
110 9
        $column = Coordinate::columnIndexFromString($column);
111 9
        if (($column < $this->startColumnIndex) || ($column > $this->endColumnIndex)) {
112
            throw new PhpSpreadsheetException("Column $column is out of range ({$this->startColumnIndex} - {$this->endColumnIndex})");
113 9
        } elseif ($this->onlyExistingCells && !($this->worksheet->cellExistsByColumnAndRow($column, $this->rowIndex))) {
114
            throw new PhpSpreadsheetException('In "IterateOnlyExistingCells" mode and Cell does not exist');
115
        }
116 9
        $this->currentColumnIndex = $column;
117
118 9
        return $this;
119
    }
120
121
    /**
122
     * Rewind the iterator to the starting column.
123
     */
124 3
    public function rewind()
125
    {
126 3
        $this->currentColumnIndex = $this->startColumnIndex;
127 3
    }
128
129
    /**
130
     * Return the current cell in this worksheet row.
131
     *
132
     * @return \PhpOffice\PhpSpreadsheet\Cell\Cell
133
     */
134 5
    public function current()
135
    {
136 5
        return $this->worksheet->getCellByColumnAndRow($this->currentColumnIndex, $this->rowIndex);
137
    }
138
139
    /**
140
     * Return the current iterator key.
141
     *
142
     * @return string
143
     */
144 3
    public function key()
145
    {
146 3
        return Coordinate::stringFromColumnIndex($this->currentColumnIndex);
147
    }
148
149
    /**
150
     * Set the iterator to its next value.
151
     */
152 5
    public function next()
153
    {
154 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...
155 5
            ++$this->currentColumnIndex;
156 5
        } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex <= $this->endColumnIndex));
157 5
    }
158
159
    /**
160
     * Set the iterator to its previous value.
161
     *
162
     * @throws PhpSpreadsheetException
163
     */
164 2
    public function prev()
165
    {
166 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...
167 1
            throw new PhpSpreadsheetException('Column is already at the beginning of range (' . Coordinate::stringFromColumnIndex($this->endColumnIndex) . ' - ' . Coordinate::stringFromColumnIndex($this->endColumnIndex) . ')');
168
        }
169 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...
170 1
            --$this->currentColumnIndex;
171 1
        } while (($this->onlyExistingCells) && (!$this->worksheet->cellExistsByColumnAndRow($this->currentColumnIndex, $this->rowIndex)) && ($this->currentColumnIndex >= $this->startColumnIndex));
172 1
    }
173
174
    /**
175
     * Indicate if more columns exist in the worksheet range of columns that we're iterating.
176
     *
177
     * @return bool
178
     */
179 5
    public function valid()
180
    {
181 5
        return $this->currentColumnIndex <= $this->endColumnIndex;
182
    }
183
184
    /**
185
     * Validate start/end values for "IterateOnlyExistingCells" mode, and adjust if necessary.
186
     *
187
     * @throws PhpSpreadsheetException
188
     */
189 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...
190
    {
191 9
        if ($this->onlyExistingCells) {
192
            while ((!$this->worksheet->cellExistsByColumnAndRow($this->startColumnIndex, $this->rowIndex)) && ($this->startColumnIndex <= $this->endColumnIndex)) {
193
                ++$this->startColumnIndex;
194
            }
195
            if ($this->startColumnIndex > $this->endColumnIndex) {
196
                throw new PhpSpreadsheetException('No cells exist within the specified range');
197
            }
198
            while ((!$this->worksheet->cellExistsByColumnAndRow($this->endColumnIndex, $this->rowIndex)) && ($this->endColumnIndex >= $this->startColumnIndex)) {
199
                --$this->endColumnIndex;
200
            }
201
            if ($this->endColumnIndex < $this->startColumnIndex) {
202
                throw new PhpSpreadsheetException('No cells exist within the specified range');
203
            }
204
        }
205 9
    }
206
}
207