1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\IValueBinder; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\File; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
11
|
|
|
|
12
|
|
|
abstract class BaseReader implements IReader |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Read data only? |
16
|
|
|
* Identifies whether the Reader should only read data values for cells, and ignore any formatting information; |
17
|
|
|
* or whether it should read both data and formatting. |
18
|
|
|
*/ |
19
|
|
|
protected bool $readDataOnly = false; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Read empty cells? |
23
|
|
|
* Identifies whether the Reader should read data values for all cells, or should ignore cells containing |
24
|
|
|
* null value or empty string. |
25
|
|
|
*/ |
26
|
|
|
protected bool $readEmptyCells = true; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Read charts that are defined in the workbook? |
30
|
|
|
* Identifies whether the Reader should read the definitions for any charts that exist in the workbook;. |
31
|
|
|
*/ |
32
|
|
|
protected bool $includeCharts = false; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Restrict which sheets should be loaded? |
36
|
|
|
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded. |
37
|
|
|
* This property is ignored for Csv, Html, and Slk. |
38
|
|
|
* |
39
|
|
|
* @var null|string[] |
40
|
|
|
*/ |
41
|
|
|
protected ?array $loadSheetsOnly = null; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Ignore rows with no cells? |
45
|
|
|
* Identifies whether the Reader should ignore rows with no cells. |
46
|
|
|
* Currently implemented only for Xlsx. |
47
|
|
|
*/ |
48
|
|
|
protected bool $ignoreRowsWithNoCells = false; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* IReadFilter instance. |
52
|
|
|
*/ |
53
|
|
|
protected IReadFilter $readFilter; |
54
|
|
|
|
55
|
|
|
/** @var resource */ |
56
|
|
|
protected $fileHandle; |
57
|
|
|
|
58
|
|
|
protected ?XmlScanner $securityScanner = null; |
59
|
|
|
|
60
|
|
|
protected ?IValueBinder $valueBinder = null; |
61
|
|
|
|
62
|
1693 |
|
public function __construct() |
63
|
|
|
{ |
64
|
1693 |
|
$this->readFilter = new DefaultReadFilter(); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function getReadDataOnly(): bool |
68
|
|
|
{ |
69
|
1 |
|
return $this->readDataOnly; |
70
|
|
|
} |
71
|
|
|
|
72
|
4 |
|
public function setReadDataOnly(bool $readCellValuesOnly): self |
73
|
|
|
{ |
74
|
4 |
|
$this->readDataOnly = $readCellValuesOnly; |
75
|
|
|
|
76
|
4 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
public function getReadEmptyCells(): bool |
80
|
|
|
{ |
81
|
2 |
|
return $this->readEmptyCells; |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
public function setReadEmptyCells(bool $readEmptyCells): self |
85
|
|
|
{ |
86
|
7 |
|
$this->readEmptyCells = $readEmptyCells; |
87
|
|
|
|
88
|
7 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function getIgnoreRowsWithNoCells(): bool |
92
|
|
|
{ |
93
|
1 |
|
return $this->ignoreRowsWithNoCells; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function setIgnoreRowsWithNoCells(bool $ignoreRowsWithNoCells): self |
97
|
|
|
{ |
98
|
1 |
|
$this->ignoreRowsWithNoCells = $ignoreRowsWithNoCells; |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function getIncludeCharts(): bool |
104
|
|
|
{ |
105
|
2 |
|
return $this->includeCharts; |
106
|
|
|
} |
107
|
|
|
|
108
|
70 |
|
public function setIncludeCharts(bool $includeCharts): self |
109
|
|
|
{ |
110
|
70 |
|
$this->includeCharts = $includeCharts; |
111
|
|
|
|
112
|
70 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function getLoadSheetsOnly(): ?array |
116
|
|
|
{ |
117
|
2 |
|
return $this->loadSheetsOnly; |
118
|
|
|
} |
119
|
|
|
|
120
|
25 |
|
public function setLoadSheetsOnly(string|array|null $sheetList): self |
121
|
|
|
{ |
122
|
25 |
|
if ($sheetList === null) { |
123
|
1 |
|
return $this->setLoadAllSheets(); |
124
|
|
|
} |
125
|
|
|
|
126
|
24 |
|
$this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList]; |
127
|
|
|
|
128
|
24 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
4 |
|
public function setLoadAllSheets(): self |
132
|
|
|
{ |
133
|
4 |
|
$this->loadSheetsOnly = null; |
134
|
|
|
|
135
|
4 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
870 |
|
public function getReadFilter(): IReadFilter |
139
|
|
|
{ |
140
|
870 |
|
return $this->readFilter; |
141
|
|
|
} |
142
|
|
|
|
143
|
14 |
|
public function setReadFilter(IReadFilter $readFilter): self |
144
|
|
|
{ |
145
|
14 |
|
$this->readFilter = $readFilter; |
146
|
|
|
|
147
|
14 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
2 |
|
public function getSecurityScanner(): ?XmlScanner |
151
|
|
|
{ |
152
|
2 |
|
return $this->securityScanner; |
153
|
|
|
} |
154
|
|
|
|
155
|
1357 |
|
public function getSecurityScannerOrThrow(): XmlScanner |
156
|
|
|
{ |
157
|
1357 |
|
if ($this->securityScanner === null) { |
158
|
1 |
|
throw new ReaderException('Security scanner is unexpectedly null'); |
159
|
|
|
} |
160
|
|
|
|
161
|
1356 |
|
return $this->securityScanner; |
162
|
|
|
} |
163
|
|
|
|
164
|
1501 |
|
protected function processFlags(int $flags): void |
165
|
|
|
{ |
166
|
1501 |
|
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) { |
167
|
2 |
|
$this->setIncludeCharts(true); |
168
|
|
|
} |
169
|
1501 |
|
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) { |
170
|
2 |
|
$this->setReadDataOnly(true); |
171
|
|
|
} |
172
|
1501 |
|
if (((bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) { |
173
|
|
|
$this->setReadEmptyCells(false); |
174
|
|
|
} |
175
|
1501 |
|
if (((bool) ($flags & self::IGNORE_ROWS_WITH_NO_CELLS)) === true) { |
176
|
1 |
|
$this->setIgnoreRowsWithNoCells(true); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet |
181
|
|
|
{ |
182
|
1 |
|
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method'); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Loads Spreadsheet from file. |
187
|
|
|
* |
188
|
|
|
* @param int $flags the optional second parameter flags may be used to identify specific elements |
189
|
|
|
* that should be loaded, but which won't be loaded by default, using these values: |
190
|
|
|
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file |
191
|
|
|
*/ |
192
|
1501 |
|
public function load(string $filename, int $flags = 0): Spreadsheet |
193
|
|
|
{ |
194
|
1501 |
|
$this->processFlags($flags); |
195
|
|
|
|
196
|
|
|
try { |
197
|
1501 |
|
return $this->loadSpreadsheetFromFile($filename); |
198
|
42 |
|
} catch (ReaderException $e) { |
199
|
28 |
|
throw $e; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Open file for reading. |
205
|
|
|
*/ |
206
|
654 |
|
protected function openFile(string $filename): void |
207
|
|
|
{ |
208
|
654 |
|
$fileHandle = false; |
209
|
654 |
|
if ($filename) { |
210
|
651 |
|
File::assertFile($filename); |
211
|
|
|
|
212
|
|
|
// Open file |
213
|
648 |
|
$fileHandle = fopen($filename, 'rb'); |
214
|
|
|
} |
215
|
651 |
|
if ($fileHandle === false) { |
216
|
3 |
|
throw new ReaderException('Could not open file ' . $filename . ' for reading.'); |
217
|
|
|
} |
218
|
|
|
|
219
|
648 |
|
$this->fileHandle = $fileHandle; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns). |
224
|
|
|
*/ |
225
|
1 |
|
public function listWorksheetInfo(string $filename): array |
226
|
|
|
{ |
227
|
1 |
|
throw new PhpSpreadsheetException('Reader classes must implement their own listWorksheetInfo() method'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns names of the worksheets from a file, |
232
|
|
|
* possibly without parsing the whole file to a Spreadsheet object. |
233
|
|
|
* Readers will often have a more efficient method with which |
234
|
|
|
* they can override this method. |
235
|
|
|
*/ |
236
|
11 |
|
public function listWorksheetNames(string $filename): array |
237
|
|
|
{ |
238
|
11 |
|
$returnArray = []; |
239
|
11 |
|
$info = $this->listWorksheetInfo($filename); |
240
|
11 |
|
foreach ($info as $infoArray) { |
241
|
11 |
|
if (isset($infoArray['worksheetName'])) { |
242
|
11 |
|
$returnArray[] = $infoArray['worksheetName']; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
11 |
|
return $returnArray; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function getValueBinder(): ?IValueBinder |
250
|
|
|
{ |
251
|
|
|
return $this->valueBinder; |
252
|
|
|
} |
253
|
|
|
|
254
|
3 |
|
public function setValueBinder(?IValueBinder $valueBinder): self |
255
|
|
|
{ |
256
|
3 |
|
$this->valueBinder = $valueBinder; |
257
|
|
|
|
258
|
3 |
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
1533 |
|
protected function newSpreadsheet(): Spreadsheet |
262
|
|
|
{ |
263
|
1533 |
|
return new Spreadsheet(); |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|