| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Reader;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Shared\File;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
10 |
|
|
|
11 |
abstract class BaseReader implements IReader
|
|
|
12 |
{
|
|
|
13 |
/**
|
|
|
14 |
* Read data only?
|
|
|
15 |
* Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
|
|
|
16 |
* or whether it should read both data and formatting.
|
|
|
17 |
*
|
|
|
18 |
* @var bool
|
|
|
19 |
*/
|
|
|
20 |
protected $readDataOnly = false;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Read empty cells?
|
|
|
24 |
* Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
|
|
|
25 |
* null value or empty string.
|
|
|
26 |
*
|
|
|
27 |
* @var bool
|
|
|
28 |
*/
|
|
|
29 |
protected $readEmptyCells = true;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Read charts that are defined in the workbook?
|
|
|
33 |
* Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
|
|
|
34 |
*
|
|
|
35 |
* @var bool
|
|
|
36 |
*/
|
|
|
37 |
protected $includeCharts = false;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Restrict which sheets should be loaded?
|
|
|
41 |
* This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
|
|
|
42 |
*
|
|
|
43 |
* @var null|string[]
|
|
|
44 |
*/
|
|
|
45 |
protected $loadSheetsOnly;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* IReadFilter instance.
|
|
|
49 |
*
|
|
|
50 |
* @var IReadFilter
|
|
|
51 |
*/
|
|
|
52 |
protected $readFilter;
|
|
|
53 |
|
|
|
54 |
/** @var resource */
|
|
|
55 |
protected $fileHandle;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* @var ?XmlScanner
|
|
|
59 |
*/
|
|
|
60 |
protected $securityScanner;
|
|
|
61 |
|
|
|
62 |
public function __construct()
|
|
|
63 |
{
|
|
|
64 |
$this->readFilter = new DefaultReadFilter();
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function getReadDataOnly()
|
|
|
68 |
{
|
|
|
69 |
return $this->readDataOnly;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public function setReadDataOnly($readCellValuesOnly)
|
|
|
73 |
{
|
|
|
74 |
$this->readDataOnly = (bool) $readCellValuesOnly;
|
|
|
75 |
|
|
|
76 |
return $this;
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public function getReadEmptyCells()
|
|
|
80 |
{
|
|
|
81 |
return $this->readEmptyCells;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public function setReadEmptyCells($readEmptyCells)
|
|
|
85 |
{
|
|
|
86 |
$this->readEmptyCells = (bool) $readEmptyCells;
|
|
|
87 |
|
|
|
88 |
return $this;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
public function getIncludeCharts()
|
|
|
92 |
{
|
|
|
93 |
return $this->includeCharts;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public function setIncludeCharts($includeCharts)
|
|
|
97 |
{
|
|
|
98 |
$this->includeCharts = (bool) $includeCharts;
|
|
|
99 |
|
|
|
100 |
return $this;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public function getLoadSheetsOnly()
|
|
|
104 |
{
|
|
|
105 |
return $this->loadSheetsOnly;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public function setLoadSheetsOnly($sheetList)
|
|
|
109 |
{
|
|
|
110 |
if ($sheetList === null) {
|
|
|
111 |
return $this->setLoadAllSheets();
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
$this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList];
|
|
|
115 |
|
|
|
116 |
return $this;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
public function setLoadAllSheets()
|
|
|
120 |
{
|
|
|
121 |
$this->loadSheetsOnly = null;
|
|
|
122 |
|
|
|
123 |
return $this;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public function getReadFilter()
|
|
|
127 |
{
|
|
|
128 |
return $this->readFilter;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public function setReadFilter(IReadFilter $readFilter)
|
|
|
132 |
{
|
|
|
133 |
$this->readFilter = $readFilter;
|
|
|
134 |
|
|
|
135 |
return $this;
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function getSecurityScanner(): ?XmlScanner
|
|
|
139 |
{
|
|
|
140 |
return $this->securityScanner;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public function getSecurityScannerOrThrow(): XmlScanner
|
|
|
144 |
{
|
|
|
145 |
if ($this->securityScanner === null) {
|
|
|
146 |
throw new ReaderException('Security scanner is unexpectedly null');
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
return $this->securityScanner;
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
protected function processFlags(int $flags): void
|
|
|
153 |
{
|
|
|
154 |
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) {
|
|
|
155 |
$this->setIncludeCharts(true);
|
|
|
156 |
}
|
|
|
157 |
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) {
|
|
|
158 |
$this->setReadDataOnly(true);
|
|
|
159 |
}
|
|
|
160 |
if (((bool) ($flags & self::SKIP_EMPTY_CELLS) || (bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) {
|
|
|
161 |
$this->setReadEmptyCells(false);
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
|
|
|
166 |
{
|
|
|
167 |
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method');
|
|
|
168 |
}
|
|
|
169 |
|
|
|
170 |
/**
|
|
|
171 |
* Loads Spreadsheet from file.
|
|
|
172 |
*
|
|
|
173 |
* @param int $flags the optional second parameter flags may be used to identify specific elements
|
|
|
174 |
* that should be loaded, but which won't be loaded by default, using these values:
|
|
|
175 |
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file
|
|
|
176 |
*/
|
|
|
177 |
public function load(string $filename, int $flags = 0): Spreadsheet
|
|
|
178 |
{
|
|
|
179 |
$this->processFlags($flags);
|
|
|
180 |
|
|
|
181 |
try {
|
|
|
182 |
return $this->loadSpreadsheetFromFile($filename);
|
|
|
183 |
} catch (ReaderException $e) {
|
|
|
184 |
throw $e;
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
/**
|
|
|
189 |
* Open file for reading.
|
|
|
190 |
*/
|
|
|
191 |
protected function openFile(string $filename): void
|
|
|
192 |
{
|
|
|
193 |
$fileHandle = false;
|
|
|
194 |
if ($filename) {
|
|
|
195 |
File::assertFile($filename);
|
|
|
196 |
|
|
|
197 |
// Open file
|
|
|
198 |
$fileHandle = fopen($filename, 'rb');
|
|
|
199 |
}
|
|
|
200 |
if ($fileHandle === false) {
|
|
|
201 |
throw new ReaderException('Could not open file ' . $filename . ' for reading.');
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
$this->fileHandle = $fileHandle;
|
|
|
205 |
}
|
|
|
206 |
}
|