1 | <?php |
||
17 | class ExcelReader implements CountableReader, \SeekableIterator |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $worksheet; |
||
23 | |||
24 | /** |
||
25 | * @var integer |
||
26 | */ |
||
27 | protected $headerRowNumber; |
||
28 | |||
29 | /** |
||
30 | * @var integer |
||
31 | */ |
||
32 | protected $pointer = 0; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $columnHeaders; |
||
38 | |||
39 | /** |
||
40 | * Total number of rows |
||
41 | * |
||
42 | * @var integer |
||
43 | */ |
||
44 | protected $count; |
||
45 | |||
46 | /** |
||
47 | * @param \SplFileObject $file Excel file |
||
48 | * @param integer $headerRowNumber Optional number of header row |
||
49 | * @param integer $activeSheet Index of active sheet to read from |
||
50 | * @param boolean $readOnly If set to false, the reader take care of the excel formatting (slow) |
||
51 | * @param integer $maxRows Maximum number of rows to read |
||
52 | */ |
||
53 | 6 | public function __construct(\SplFileObject $file, $headerRowNumber = null, $activeSheet = null, $readOnly = true, $maxRows = null) |
|
54 | { |
||
55 | 6 | $reader = \PHPExcel_IOFactory::createReaderForFile($file->getPathName()); |
|
56 | 6 | $reader->setReadDataOnly($readOnly); |
|
57 | /** @var \PHPExcel $excel */ |
||
58 | 6 | $excel = $reader->load($file->getPathname()); |
|
59 | |||
60 | 6 | if (null !== $activeSheet) { |
|
61 | 1 | $excel->setActiveSheetIndex($activeSheet); |
|
62 | 1 | } |
|
63 | 6 | $sheet = $excel->getActiveSheet(); |
|
64 | |||
65 | 6 | if ($maxRows && $maxRows < $sheet->getHighestDataRow()) { |
|
|
|||
66 | 1 | $maxColumn = $sheet->getHighestDataColumn(); |
|
67 | 1 | $this->worksheet = $sheet->rangeToArray('A1:' . $maxColumn . $maxRows); |
|
68 | 1 | } else { |
|
69 | 6 | $this->worksheet = $excel->getActiveSheet()->toArray(); |
|
70 | } |
||
71 | |||
72 | 6 | if (null !== $headerRowNumber) { |
|
73 | 3 | $this->setHeaderRowNumber($headerRowNumber); |
|
74 | 3 | } |
|
75 | 6 | } |
|
76 | |||
77 | /** |
||
78 | * Return the current row as an array |
||
79 | * |
||
80 | * If a header row has been set, an associative array will be returned |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 1 | public function current() |
|
101 | |||
102 | /** |
||
103 | * Get column headers |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getColumnHeaders() |
||
111 | |||
112 | /** |
||
113 | * Set column headers |
||
114 | * |
||
115 | * @param array $columnHeaders |
||
116 | */ |
||
117 | public function setColumnHeaders(array $columnHeaders) |
||
121 | |||
122 | /** |
||
123 | * Rewind the file pointer |
||
124 | * |
||
125 | * If a header row has been set, the pointer is set just below the header |
||
126 | * row. That way, when you iterate over the rows, that header row is |
||
127 | * skipped. |
||
128 | */ |
||
129 | 1 | public function rewind() |
|
137 | |||
138 | /** |
||
139 | * Set header row number |
||
140 | * |
||
141 | * @param integer $rowNumber Number of the row that contains column header names |
||
142 | */ |
||
143 | 3 | public function setHeaderRowNumber($rowNumber) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 1 | public function next() |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | 1 | public function valid() |
|
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function key() |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function seek($pointer) |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | 5 | public function count() |
|
193 | |||
194 | /** |
||
195 | * Get a row |
||
196 | * |
||
197 | * @param integer $number |
||
198 | * |
||
199 | * @return array |
||
200 | */ |
||
201 | public function getRow($number) |
||
207 | } |
||
208 |
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: