1 | <?php |
||
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) |
|
102 | |||
103 | /** |
||
104 | * @return int |
||
105 | */ |
||
106 | 5 | public function count() |
|
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() |
|
148 | |||
149 | /** |
||
150 | * Get a row |
||
151 | * |
||
152 | * @param int $number |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | 4 | public function getRow($number) |
|
162 | |||
163 | /** |
||
164 | * Return the key of the current element |
||
165 | * |
||
166 | * @return int |
||
167 | */ |
||
168 | 1 | public function key() |
|
172 | |||
173 | /** |
||
174 | * Move forward to next element |
||
175 | * |
||
176 | * @return void Any returned value is ignored. |
||
177 | */ |
||
178 | 2 | public function next() |
|
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() |
|
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) |
|
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) |
|
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) |
|
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() |
|
250 | } |
||
251 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: