SpreadsheetReader   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 4
dl 0
loc 212
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 26 5
A count() 0 9 2
A getColumnHeaders() 0 4 1
A getRow() 0 6 1
A key() 0 4 1
A next() 0 4 1
A rewind() 0 8 2
A seek() 0 4 1
A setColumnHeaders() 0 4 1
A setHeaderRowNumber() 0 5 1
A valid() 0 4 1
A current() 0 13 3
1
<?php
2
/*
3
The MIT License (MIT)
4
5
Copyright (c) 2015 PortPHP
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
SOFTWARE.
24
 */
25
namespace Port\Spreadsheet;
26
27
use PhpOffice\PhpSpreadsheet\IOFactory;
28
use PhpOffice\PhpSpreadsheet\Spreadsheet;
29
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
30
use Port\Reader\CountableReader;
31
32
/**
33
 * Reads Spreadsheet files with the help of PHPSpreadsheet
34
 *
35
 * @author David de Boer <[email protected]>
36
 *
37
 * @see https://github.com/PHPOffice/PhpSpreadsheet
38
 */
39
class SpreadsheetReader implements CountableReader, \SeekableIterator
40
{
41
    /**
42
     * @var array
43
     */
44
    protected $columnHeaders;
45
46
    /**
47
     * Total number of rows
48
     *
49
     * @var int
50
     */
51
    protected $count;
52
53
    /**
54
     * @var int
55
     */
56
    protected $headerRowNumber;
57
58
    /**
59
     * @var int
60
     */
61
    protected $pointer = 0;
62
63
    /**
64
     * @var array
65
     */
66
    protected $worksheet;
67
68
    // phpcs:disable Generic.Files.LineLength.MaxExceeded
69
    /**
70
     * @param \SplFileObject $file            Spreadsheet file
71
     * @param int            $headerRowNumber Optional number of header row
72
     * @param int            $activeSheet     Index of active sheet to read from
73
     * @param bool           $readOnly        If set to false, the reader take care of the spreadsheet formatting (slow)
74
     * @param int            $maxRows         Maximum number of rows to read
75
     */
76 11
    public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null)
77
    {
78
        // phpcs:enable Generic.Files.LineLength.MaxExceeded
79 11
        $reader = IOFactory::createReaderForFile($file->getPathName());
80 11
        $reader->setReadDataOnly($readOnly);
81
        /** @var Spreadsheet $spreadsheet */
82 11
        $spreadsheet = $reader->load($file->getPathname());
83
84 11
        if (null !== $activeSheet) {
85 1
            $spreadsheet->setActiveSheetIndex($activeSheet);
86 1
        }
87
88
        /** @var Worksheet $sheet */
89 11
        $sheet = $spreadsheet->getActiveSheet();
90
91 11
        if ($maxRows && $maxRows < $sheet->getHighestDataRow()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $maxRows of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
92 1
            $maxColumn       = $sheet->getHighestDataColumn();
93 1
            $this->worksheet = $sheet->rangeToArray('A1:'.$maxColumn.$maxRows);
94 1
        } else {
95 11
            $this->worksheet = $spreadsheet->getActiveSheet()->toArray();
96
        }
97
98 11
        if (null !== $headerRowNumber) {
99 5
            $this->setHeaderRowNumber($headerRowNumber);
100 5
        }
101 11
    }
102
103
    /**
104
     * @return int
105
     */
106 5
    public function count()
107
    {
108 5
        $count = count($this->worksheet);
109 5
        if (null !== $this->headerRowNumber) {
110 2
            $count--;
111 2
        }
112
113 5
        return $count;
114
    }
115
116
    /**
117
     * Return the current row as an array
118
     *
119
     * If a header row has been set, an associative array will be returned
120
     *
121
     * @return array|null
122
     *
123
     * @author  Derek Chafin <[email protected]>
124
     */
125 6
    public function current()
126
    {
127 6
        $row = $this->worksheet[$this->pointer];
128
129
        // If the spreadsheet file has column headers, use them to construct an associative
130
        // array for the columns in this line
131 6
        if (!empty($this->columnHeaders) && count($this->columnHeaders) === count($row)) {
132 4
            return array_combine(array_values($this->columnHeaders), $row);
133
        }
134
135
        // Else just return the column values
136 2
        return $row;
137
    }
138
139
    /**
140
     * Get column headers
141
     *
142
     * @return array
143
     */
144 1
    public function getColumnHeaders()
145
    {
146 1
        return $this->columnHeaders;
147
    }
148
149
    /**
150
     * Get a row
151
     *
152
     * @param int $number
153
     *
154
     * @return array
155
     */
156 4
    public function getRow($number)
157
    {
158 4
        $this->seek($number);
159
160 4
        return $this->current();
161
    }
162
163
    /**
164
     * Return the key of the current element
165
     *
166
     * @return int
167
     */
168 1
    public function key()
169
    {
170 1
        return $this->pointer;
171
    }
172
173
    /**
174
     * Move forward to next element
175
     *
176
     * @return void Any returned value is ignored.
177
     */
178 2
    public function next()
179
    {
180 2
        $this->pointer++;
181 2
    }
182
183
    /**
184
     * Rewind the file pointer
185
     *
186
     * If a header row has been set, the pointer is set just below the header
187
     * row. That way, when you iterate over the rows, that header row is
188
     * skipped.
189
     *
190
     * @return void Any returned value is ignored.
191
     */
192 2
    public function rewind()
193
    {
194 2
        if (null === $this->headerRowNumber) {
195 1
            $this->pointer = 0;
196 1
        } else {
197 1
            $this->pointer = $this->headerRowNumber + 1;
198
        }
199 2
    }
200
201
    /**
202
     * Seeks to a position
203
     *
204
     * @link http://php.net/manual/en/seekableiterator.seek.php
205
     *
206
     * @param int $pointer The position to seek to.
207
     *
208
     * @return void Any returned value is ignored.
209
     */
210 4
    public function seek($pointer)
211
    {
212 4
        $this->pointer = $pointer;
213 4
    }
214
215
    /**
216
     * Set column headers
217
     *
218
     * @param array $columnHeaders
219
     *
220
     * @return void Any returned value is ignored.
221
     */
222 2
    public function setColumnHeaders(array $columnHeaders)
223
    {
224 2
        $this->columnHeaders = $columnHeaders;
225 2
    }
226
227
    /**
228
     * Set header row number
229
     *
230
     * @param int $rowNumber Number of the row that contains column header names
231
     *
232
     * @return void Any returned value is ignored.
233
     */
234 5
    public function setHeaderRowNumber($rowNumber)
235
    {
236 5
        $this->headerRowNumber = $rowNumber;
237 5
        $this->columnHeaders   = $this->worksheet[$rowNumber];
238 5
    }
239
240
    /**
241
     * Checks if current position is valid
242
     *
243
     * @return bool The return value will be casted to boolean and then evaluated.
244
     * Returns true on success or false on failure.
245
     */
246 2
    public function valid()
247
    {
248 2
        return isset($this->worksheet[$this->pointer]);
249
    }
250
}
251