| 1441 |
ariadna |
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 |
public function __construct()
|
|
|
63 |
{
|
|
|
64 |
$this->readFilter = new DefaultReadFilter();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function getReadDataOnly(): bool
|
|
|
68 |
{
|
|
|
69 |
return $this->readDataOnly;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function setReadDataOnly(bool $readCellValuesOnly): self
|
|
|
73 |
{
|
|
|
74 |
$this->readDataOnly = $readCellValuesOnly;
|
|
|
75 |
|
|
|
76 |
return $this;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public function getReadEmptyCells(): bool
|
|
|
80 |
{
|
|
|
81 |
return $this->readEmptyCells;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function setReadEmptyCells(bool $readEmptyCells): self
|
|
|
85 |
{
|
|
|
86 |
$this->readEmptyCells = $readEmptyCells;
|
|
|
87 |
|
|
|
88 |
return $this;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function getIgnoreRowsWithNoCells(): bool
|
|
|
92 |
{
|
|
|
93 |
return $this->ignoreRowsWithNoCells;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function setIgnoreRowsWithNoCells(bool $ignoreRowsWithNoCells): self
|
|
|
97 |
{
|
|
|
98 |
$this->ignoreRowsWithNoCells = $ignoreRowsWithNoCells;
|
|
|
99 |
|
|
|
100 |
return $this;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public function getIncludeCharts(): bool
|
|
|
104 |
{
|
|
|
105 |
return $this->includeCharts;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public function setIncludeCharts(bool $includeCharts): self
|
|
|
109 |
{
|
|
|
110 |
$this->includeCharts = $includeCharts;
|
|
|
111 |
|
|
|
112 |
return $this;
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public function getLoadSheetsOnly(): ?array
|
|
|
116 |
{
|
|
|
117 |
return $this->loadSheetsOnly;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
public function setLoadSheetsOnly(string|array|null $sheetList): self
|
|
|
121 |
{
|
|
|
122 |
if ($sheetList === null) {
|
|
|
123 |
return $this->setLoadAllSheets();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
$this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList];
|
|
|
127 |
|
|
|
128 |
return $this;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public function setLoadAllSheets(): self
|
|
|
132 |
{
|
|
|
133 |
$this->loadSheetsOnly = null;
|
|
|
134 |
|
|
|
135 |
return $this;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function getReadFilter(): IReadFilter
|
|
|
139 |
{
|
|
|
140 |
return $this->readFilter;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public function setReadFilter(IReadFilter $readFilter): self
|
|
|
144 |
{
|
|
|
145 |
$this->readFilter = $readFilter;
|
|
|
146 |
|
|
|
147 |
return $this;
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
public function getSecurityScanner(): ?XmlScanner
|
|
|
151 |
{
|
|
|
152 |
return $this->securityScanner;
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
public function getSecurityScannerOrThrow(): XmlScanner
|
|
|
156 |
{
|
|
|
157 |
if ($this->securityScanner === null) {
|
|
|
158 |
throw new ReaderException('Security scanner is unexpectedly null');
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
return $this->securityScanner;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
protected function processFlags(int $flags): void
|
|
|
165 |
{
|
|
|
166 |
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) {
|
|
|
167 |
$this->setIncludeCharts(true);
|
|
|
168 |
}
|
|
|
169 |
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) {
|
|
|
170 |
$this->setReadDataOnly(true);
|
|
|
171 |
}
|
|
|
172 |
if (((bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) {
|
|
|
173 |
$this->setReadEmptyCells(false);
|
|
|
174 |
}
|
|
|
175 |
if (((bool) ($flags & self::IGNORE_ROWS_WITH_NO_CELLS)) === true) {
|
|
|
176 |
$this->setIgnoreRowsWithNoCells(true);
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
|
|
|
180 |
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
|
|
|
181 |
{
|
|
|
182 |
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 |
public function load(string $filename, int $flags = 0): Spreadsheet
|
|
|
193 |
{
|
|
|
194 |
$this->processFlags($flags);
|
|
|
195 |
|
|
|
196 |
try {
|
|
|
197 |
return $this->loadSpreadsheetFromFile($filename);
|
|
|
198 |
} catch (ReaderException $e) {
|
|
|
199 |
throw $e;
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
|
|
|
203 |
/**
|
|
|
204 |
* Open file for reading.
|
|
|
205 |
*/
|
|
|
206 |
protected function openFile(string $filename): void
|
|
|
207 |
{
|
|
|
208 |
$fileHandle = false;
|
|
|
209 |
if ($filename) {
|
|
|
210 |
File::assertFile($filename);
|
|
|
211 |
|
|
|
212 |
// Open file
|
|
|
213 |
$fileHandle = fopen($filename, 'rb');
|
|
|
214 |
}
|
|
|
215 |
if ($fileHandle === false) {
|
|
|
216 |
throw new ReaderException('Could not open file ' . $filename . ' for reading.');
|
|
|
217 |
}
|
|
|
218 |
|
|
|
219 |
$this->fileHandle = $fileHandle;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
/**
|
|
|
223 |
* Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
|
|
|
224 |
*/
|
|
|
225 |
public function listWorksheetInfo(string $filename): array
|
|
|
226 |
{
|
|
|
227 |
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 |
public function listWorksheetNames(string $filename): array
|
|
|
237 |
{
|
|
|
238 |
$returnArray = [];
|
|
|
239 |
$info = $this->listWorksheetInfo($filename);
|
|
|
240 |
foreach ($info as $infoArray) {
|
|
|
241 |
if (isset($infoArray['worksheetName'])) {
|
|
|
242 |
$returnArray[] = $infoArray['worksheetName'];
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
return $returnArray;
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
public function getValueBinder(): ?IValueBinder
|
|
|
250 |
{
|
|
|
251 |
return $this->valueBinder;
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
public function setValueBinder(?IValueBinder $valueBinder): self
|
|
|
255 |
{
|
|
|
256 |
$this->valueBinder = $valueBinder;
|
|
|
257 |
|
|
|
258 |
return $this;
|
|
|
259 |
}
|
|
|
260 |
}
|