Passed
Pull Request — master (#4185)
by Owen
15:32
created

BaseReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 1
cts 1
cp 1
crap 1
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 1598
60
    protected ?IValueBinder $valueBinder = null;
61 1598
62
    public function __construct()
63
    {
64 1
        $this->readFilter = new DefaultReadFilter();
65
    }
66 1
67
    public function getReadDataOnly(): bool
68
    {
69 3
        return $this->readDataOnly;
70
    }
71 3
72
    public function setReadDataOnly(bool $readCellValuesOnly): self
73 3
    {
74
        $this->readDataOnly = $readCellValuesOnly;
75
76 2
        return $this;
77
    }
78 2
79
    public function getReadEmptyCells(): bool
80
    {
81 7
        return $this->readEmptyCells;
82
    }
83 7
84
    public function setReadEmptyCells(bool $readEmptyCells): self
85 7
    {
86
        $this->readEmptyCells = $readEmptyCells;
87
88 1
        return $this;
89
    }
90 1
91
    public function getIgnoreRowsWithNoCells(): bool
92
    {
93 1
        return $this->ignoreRowsWithNoCells;
94
    }
95 1
96
    public function setIgnoreRowsWithNoCells(bool $ignoreRowsWithNoCells): self
97 1
    {
98
        $this->ignoreRowsWithNoCells = $ignoreRowsWithNoCells;
99
100 2
        return $this;
101
    }
102 2
103
    public function getIncludeCharts(): bool
104
    {
105 67
        return $this->includeCharts;
106
    }
107 67
108
    public function setIncludeCharts(bool $includeCharts): self
109 67
    {
110
        $this->includeCharts = $includeCharts;
111
112 2
        return $this;
113
    }
114 2
115
    public function getLoadSheetsOnly(): ?array
116
    {
117 25
        return $this->loadSheetsOnly;
118
    }
119 25
120 1
    public function setLoadSheetsOnly(string|array|null $sheetList): self
121
    {
122
        if ($sheetList === null) {
123 24
            return $this->setLoadAllSheets();
124
        }
125 24
126
        $this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList];
127
128 4
        return $this;
129
    }
130 4
131
    public function setLoadAllSheets(): self
132 4
    {
133
        $this->loadSheetsOnly = null;
134
135 828
        return $this;
136
    }
137 828
138
    public function getReadFilter(): IReadFilter
139
    {
140 14
        return $this->readFilter;
141
    }
142 14
143
    public function setReadFilter(IReadFilter $readFilter): self
144 14
    {
145
        $this->readFilter = $readFilter;
146
147 2
        return $this;
148
    }
149 2
150
    public function getSecurityScanner(): ?XmlScanner
151
    {
152 1276
        return $this->securityScanner;
153
    }
154 1276
155 1
    public function getSecurityScannerOrThrow(): XmlScanner
156
    {
157
        if ($this->securityScanner === null) {
158 1275
            throw new ReaderException('Security scanner is unexpectedly null');
159
        }
160
161 1416
        return $this->securityScanner;
162
    }
163 1416
164 2
    protected function processFlags(int $flags): void
165
    {
166 1416
        if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) {
167 2
            $this->setIncludeCharts(true);
168
        }
169 1416
        if (((bool) ($flags & self::READ_DATA_ONLY)) === true) {
170
            $this->setReadDataOnly(true);
171
        }
172 1416
        if (((bool) ($flags & self::IGNORE_EMPTY_CELLS)) === true) {
173 1
            $this->setReadEmptyCells(false);
174
        }
175
        if (((bool) ($flags & self::IGNORE_ROWS_WITH_NO_CELLS)) === true) {
176
            $this->setIgnoreRowsWithNoCells(true);
177 1
        }
178
    }
179 1
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 1416
     *                       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 1416
     */
192
    public function load(string $filename, int $flags = 0): Spreadsheet
193
    {
194 1416
        $this->processFlags($flags);
195 29
196 22
        try {
197
            return $this->loadSpreadsheetFromFile($filename);
198
        } catch (ReaderException $e) {
199
            throw $e;
200
        }
201
    }
202
203 619
    /**
204
     * Open file for reading.
205 619
     */
206 619
    protected function openFile(string $filename): void
207 616
    {
208
        $fileHandle = false;
209
        if ($filename) {
210 613
            File::assertFile($filename);
211
212 616
            // Open file
213 3
            $fileHandle = fopen($filename, 'rb');
214
        }
215
        if ($fileHandle === false) {
216 613
            throw new ReaderException('Could not open file ' . $filename . ' for reading.');
217
        }
218
219
        $this->fileHandle = $fileHandle;
220
    }
221
222 1
    /**
223
     * Return worksheet info (Name, Last Column Letter, Last Column Index, Total Rows, Total Columns).
224 1
     */
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 11
     * Readers will often have a more efficient method with which
234
     * they can override this method.
235 11
     */
236 11
    public function listWorksheetNames(string $filename): array
237 11
    {
238 11
        $returnArray = [];
239 11
        $info = $this->listWorksheetInfo($filename);
240
        foreach ($info as $infoArray) {
241
            if (isset($infoArray['worksheetName'])) {
242
                $returnArray[] = $infoArray['worksheetName'];
243 11
            }
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
}
261