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
|
|
|
protected $fileHandle; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var XmlScanner |
58
|
|
|
*/ |
59
|
|
|
protected $securityScanner; |
60
|
|
|
|
61
|
1255 |
|
public function __construct() |
62
|
|
|
{ |
63
|
1255 |
|
$this->readFilter = new DefaultReadFilter(); |
64
|
|
|
} |
65
|
|
|
|
66
|
472 |
|
public function getReadDataOnly() |
67
|
|
|
{ |
68
|
472 |
|
return $this->readDataOnly; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
public function setReadDataOnly($readCellValuesOnly) |
72
|
|
|
{ |
73
|
1 |
|
$this->readDataOnly = (bool) $readCellValuesOnly; |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getReadEmptyCells() |
79
|
|
|
{ |
80
|
1 |
|
return $this->readEmptyCells; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
public function setReadEmptyCells($readEmptyCells) |
84
|
|
|
{ |
85
|
2 |
|
$this->readEmptyCells = (bool) $readEmptyCells; |
86
|
|
|
|
87
|
2 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
public function getIncludeCharts() |
91
|
|
|
{ |
92
|
1 |
|
return $this->includeCharts; |
93
|
|
|
} |
94
|
|
|
|
95
|
48 |
|
public function setIncludeCharts($includeCharts) |
96
|
|
|
{ |
97
|
48 |
|
$this->includeCharts = (bool) $includeCharts; |
98
|
|
|
|
99
|
48 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
public function getLoadSheetsOnly() |
103
|
|
|
{ |
104
|
2 |
|
return $this->loadSheetsOnly; |
105
|
|
|
} |
106
|
|
|
|
107
|
11 |
|
public function setLoadSheetsOnly($sheetList) |
108
|
|
|
{ |
109
|
11 |
|
if ($sheetList === null) { |
110
|
1 |
|
return $this->setLoadAllSheets(); |
111
|
|
|
} |
112
|
|
|
|
113
|
10 |
|
$this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList]; |
114
|
|
|
|
115
|
10 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
public function setLoadAllSheets() |
119
|
|
|
{ |
120
|
3 |
|
$this->loadSheetsOnly = null; |
121
|
|
|
|
122
|
3 |
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
599 |
|
public function getReadFilter() |
126
|
|
|
{ |
127
|
599 |
|
return $this->readFilter; |
128
|
|
|
} |
129
|
|
|
|
130
|
14 |
|
public function setReadFilter(IReadFilter $readFilter) |
131
|
|
|
{ |
132
|
14 |
|
$this->readFilter = $readFilter; |
133
|
|
|
|
134
|
14 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
3 |
|
public function getSecurityScanner() |
138
|
|
|
{ |
139
|
3 |
|
return $this->securityScanner; |
140
|
|
|
} |
141
|
|
|
|
142
|
1109 |
|
protected function processFlags(int $flags): void |
143
|
|
|
{ |
144
|
1109 |
|
if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) { |
145
|
2 |
|
$this->setIncludeCharts(true); |
146
|
|
|
} |
147
|
1109 |
|
if (((bool) ($flags & self::READ_DATA_ONLY)) === true) { |
148
|
|
|
$this->setReadDataOnly(true); |
149
|
|
|
} |
150
|
1109 |
|
if (((bool) ($flags & self::SKIP_EMPTY_CELLS)) === true) { |
151
|
|
|
$this->setReadEmptyCells(false); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
protected function loadSpreadsheetFromFile(string $filename): Spreadsheet |
156
|
|
|
{ |
157
|
1 |
|
throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Loads Spreadsheet from file. |
162
|
|
|
* |
163
|
|
|
* @param int $flags the optional second parameter flags may be used to identify specific elements |
164
|
|
|
* that should be loaded, but which won't be loaded by default, using these values: |
165
|
|
|
* IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file |
166
|
|
|
*/ |
167
|
1109 |
|
public function load(string $filename, int $flags = 0): Spreadsheet |
168
|
|
|
{ |
169
|
1109 |
|
$this->processFlags($flags); |
170
|
|
|
|
171
|
|
|
try { |
172
|
1109 |
|
return $this->loadSpreadsheetFromFile($filename); |
173
|
14 |
|
} catch (ReaderException $e) { |
174
|
14 |
|
throw $e; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Open file for reading. |
180
|
|
|
*/ |
181
|
537 |
|
protected function openFile(string $filename): void |
182
|
|
|
{ |
183
|
537 |
|
$fileHandle = false; |
184
|
537 |
|
if ($filename) { |
185
|
534 |
|
File::assertFile($filename); |
186
|
|
|
|
187
|
|
|
// Open file |
188
|
531 |
|
$fileHandle = fopen($filename, 'rb'); |
189
|
|
|
} |
190
|
534 |
|
if ($fileHandle === false) { |
191
|
3 |
|
throw new ReaderException('Could not open file ' . $filename . ' for reading.'); |
192
|
|
|
} |
193
|
|
|
|
194
|
531 |
|
$this->fileHandle = $fileHandle; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|