Passed
Push — master ( 3c7afe...11f0b5 )
by
unknown
14:12 queued 14s
created

BaseReader   A

Complexity

Total Complexity 36

Size/Duplication

Total Lines 254
Duplicated Lines 0 %

Test Coverage

Coverage 96.34%

Importance

Changes 0
Metric Value
wmc 36
eloc 68
dl 0
loc 254
ccs 79
cts 82
cp 0.9634
rs 9.52
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A getReadDataOnly() 0 3 1
A getReadEmptyCells() 0 3 1
A __construct() 0 3 1
A setReadDataOnly() 0 5 1
A setReadEmptyCells() 0 5 1
A setIgnoreRowsWithNoCells() 0 5 1
A setIncludeCharts() 0 5 1
A getReadFilter() 0 3 1
A loadSpreadsheetFromFile() 0 3 1
A setLoadAllSheets() 0 5 1
A getLoadSheetsOnly() 0 3 1
A processFlags() 0 13 5
A setReadFilter() 0 5 1
A getIgnoreRowsWithNoCells() 0 3 1
A getSecurityScannerOrThrow() 0 7 2
A openFile() 0 14 3
A load() 0 8 2
A getIncludeCharts() 0 3 1
A getSecurityScanner() 0 3 1
A setLoadSheetsOnly() 0 9 3
A getValueBinder() 0 3 1
A setValueBinder() 0 5 1
A listWorksheetInfo() 0 3 1
A newSpreadsheet() 0 3 1
A listWorksheetNames() 0 9 2
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 1707
    public function __construct()
63
    {
64 1707
        $this->readFilter = new DefaultReadFilter();
65
    }
66
67 1
    public function getReadDataOnly(): bool
68
    {
69 1
        return $this->readDataOnly;
70
    }
71
72 9
    public function setReadDataOnly(bool $readCellValuesOnly): self
73
    {
74 9
        $this->readDataOnly = $readCellValuesOnly;
75
76 9
        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 884
    public function getReadFilter(): IReadFilter
139
    {
140 884
        return $this->readFilter;
141
    }
142
143 15
    public function setReadFilter(IReadFilter $readFilter): self
144
    {
145 15
        $this->readFilter = $readFilter;
146
147 15
        return $this;
148
    }
149
150 2
    public function getSecurityScanner(): ?XmlScanner
151
    {
152 2
        return $this->securityScanner;
153
    }
154
155 1371
    public function getSecurityScannerOrThrow(): XmlScanner
156
    {
157 1371
        if ($this->securityScanner === null) {
158 1
            throw new ReaderException('Security scanner is unexpectedly null');
159
        }
160
161 1370
        return $this->securityScanner;
162
    }
163
164 1515
    protected function processFlags(int $flags): void
165
    {
166 1515
        if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) {
167 2
            $this->setIncludeCharts(true);
168
        }
169 1515
        if (((bool) ($flags & self::READ_DATA_ONLY)) === true) {
170 2
            $this->setReadDataOnly(true);
171
        }
172 1515
        if (((bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) {
173
            $this->setReadEmptyCells(false);
174
        }
175 1515
        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 1515
    public function load(string $filename, int $flags = 0): Spreadsheet
193
    {
194 1515
        $this->processFlags($flags);
195
196
        try {
197 1515
            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
     * @return array<int, array{worksheetName: string, lastColumnLetter: string, lastColumnIndex: int, totalRows: int, totalColumns: int, sheetState: string}>
226
     */
227 1
    public function listWorksheetInfo(string $filename): array
228
    {
229 1
        throw new PhpSpreadsheetException('Reader classes must implement their own listWorksheetInfo() method');
230
    }
231
232
    /**
233
     * Returns names of the worksheets from a file,
234
     * possibly without parsing the whole file to a Spreadsheet object.
235
     * Readers will often have a more efficient method with which
236
     * they can override this method.
237
     *
238
     * @return string[]
239
     */
240 11
    public function listWorksheetNames(string $filename): array
241
    {
242 11
        $returnArray = [];
243 11
        $info = $this->listWorksheetInfo($filename);
244 11
        foreach ($info as $infoArray) {
245 11
            $returnArray[] = $infoArray['worksheetName'];
246
        }
247
248 11
        return $returnArray;
249
    }
250
251
    public function getValueBinder(): ?IValueBinder
252
    {
253
        return $this->valueBinder;
254
    }
255
256 3
    public function setValueBinder(?IValueBinder $valueBinder): self
257
    {
258 3
        $this->valueBinder = $valueBinder;
259
260 3
        return $this;
261
    }
262
263 1547
    protected function newSpreadsheet(): Spreadsheet
264
    {
265 1547
        return new Spreadsheet();
266
    }
267
}
268