1 | <?php |
||
8 | class Csv extends BaseWriter implements IWriter |
||
9 | { |
||
10 | /** |
||
11 | * PhpSpreadsheet object. |
||
12 | * |
||
13 | * @var PhpSpreadsheet |
||
14 | */ |
||
15 | private $spreadsheet; |
||
16 | |||
17 | /** |
||
18 | * Delimiter. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | private $delimiter = ','; |
||
23 | |||
24 | /** |
||
25 | * Enclosure. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $enclosure = '"'; |
||
30 | |||
31 | /** |
||
32 | * Line ending. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | private $lineEnding = PHP_EOL; |
||
37 | |||
38 | /** |
||
39 | * Sheet index to write. |
||
40 | * |
||
41 | * @var int |
||
42 | */ |
||
43 | private $sheetIndex = 0; |
||
44 | |||
45 | /** |
||
46 | * Whether to write a BOM (for UTF8). |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | private $useBOM = false; |
||
51 | |||
52 | /** |
||
53 | * Whether to write a Separator line as the first line of the file |
||
54 | * sep=x. |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $includeSeparatorLine = false; |
||
59 | |||
60 | /** |
||
61 | * Whether to write a fully Excel compatible CSV file. |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $excelCompatibility = false; |
||
66 | |||
67 | /** |
||
68 | * Create a new CSV. |
||
69 | * |
||
70 | * @param Spreadsheet $spreadsheet Spreadsheet object |
||
71 | */ |
||
72 | 1 | public function __construct(Spreadsheet $spreadsheet) |
|
76 | |||
77 | /** |
||
78 | * Save PhpSpreadsheet to file. |
||
79 | * |
||
80 | * @param string $pFilename |
||
81 | * |
||
82 | * @throws Exception |
||
83 | */ |
||
84 | 1 | public function save($pFilename) |
|
85 | { |
||
86 | // Fetch sheet |
||
87 | 1 | $sheet = $this->spreadsheet->getSheet($this->sheetIndex); |
|
88 | |||
89 | 1 | $saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog(); |
|
90 | 1 | Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false); |
|
91 | 1 | $saveArrayReturnType = Calculation::getArrayReturnType(); |
|
92 | 1 | Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE); |
|
93 | |||
94 | // Open file |
||
95 | 1 | $fileHandle = fopen($pFilename, 'wb+'); |
|
96 | 1 | if ($fileHandle === false) { |
|
97 | throw new Exception("Could not open file $pFilename for writing."); |
||
98 | } |
||
99 | |||
100 | 1 | if ($this->excelCompatibility) { |
|
101 | $this->setUseBOM(true); // Enforce UTF-8 BOM Header |
||
102 | $this->setIncludeSeparatorLine(true); // Set separator line |
||
103 | $this->setEnclosure('"'); // Set enclosure to " |
||
104 | $this->setDelimiter(';'); // Set delimiter to a semi-colon |
||
105 | $this->setLineEnding("\r\n"); |
||
106 | } |
||
107 | 1 | if ($this->useBOM) { |
|
108 | // Write the UTF-8 BOM code if required |
||
109 | fwrite($fileHandle, "\xEF\xBB\xBF"); |
||
110 | } |
||
111 | 1 | if ($this->includeSeparatorLine) { |
|
112 | // Write the separator line if required |
||
113 | fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding); |
||
114 | } |
||
115 | |||
116 | // Identify the range that we need to extract from the worksheet |
||
117 | 1 | $maxCol = $sheet->getHighestDataColumn(); |
|
118 | 1 | $maxRow = $sheet->getHighestDataRow(); |
|
119 | |||
120 | // Write rows to file |
||
121 | 1 | for ($row = 1; $row <= $maxRow; ++$row) { |
|
122 | // Convert the row to an array... |
||
123 | 1 | $cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas); |
|
124 | // ... and write to the file |
||
125 | 1 | $this->writeLine($fileHandle, $cellsArray[0]); |
|
126 | } |
||
127 | |||
128 | // Close file |
||
129 | 1 | fclose($fileHandle); |
|
130 | |||
131 | 1 | Calculation::setArrayReturnType($saveArrayReturnType); |
|
132 | 1 | Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); |
|
133 | 1 | } |
|
134 | |||
135 | /** |
||
136 | * Get delimiter. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | public function getDelimiter() |
||
141 | { |
||
142 | return $this->delimiter; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Set delimiter. |
||
147 | * |
||
148 | * @param string $pValue Delimiter, defaults to ',' |
||
149 | * |
||
150 | * @return CSV |
||
151 | */ |
||
152 | public function setDelimiter($pValue) |
||
153 | { |
||
154 | $this->delimiter = $pValue; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get enclosure. |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getEnclosure() |
||
168 | |||
169 | /** |
||
170 | * Set enclosure. |
||
171 | * |
||
172 | * @param string $pValue Enclosure, defaults to " |
||
173 | * |
||
174 | * @return CSV |
||
175 | */ |
||
176 | public function setEnclosure($pValue) |
||
177 | { |
||
178 | if ($pValue == '') { |
||
179 | $pValue = null; |
||
180 | } |
||
181 | $this->enclosure = $pValue; |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Get line ending. |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getLineEnding() |
||
195 | |||
196 | /** |
||
197 | * Set line ending. |
||
198 | * |
||
199 | * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL) |
||
200 | * |
||
201 | * @return CSV |
||
202 | */ |
||
203 | public function setLineEnding($pValue) |
||
204 | { |
||
205 | $this->lineEnding = $pValue; |
||
206 | |||
207 | return $this; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get whether BOM should be used. |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function getUseBOM() |
||
219 | |||
220 | /** |
||
221 | * Set whether BOM should be used. |
||
222 | * |
||
223 | * @param bool $pValue Use UTF-8 byte-order mark? Defaults to false |
||
224 | * |
||
225 | * @return CSV |
||
226 | */ |
||
227 | public function setUseBOM($pValue) |
||
228 | { |
||
229 | $this->useBOM = $pValue; |
||
230 | |||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Get whether a separator line should be included. |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function getIncludeSeparatorLine() |
||
243 | |||
244 | /** |
||
245 | * Set whether a separator line should be included as the first line of the file. |
||
246 | * |
||
247 | * @param bool $pValue Use separator line? Defaults to false |
||
248 | * |
||
249 | * @return CSV |
||
250 | */ |
||
251 | public function setIncludeSeparatorLine($pValue) |
||
252 | { |
||
253 | $this->includeSeparatorLine = $pValue; |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Get whether the file should be saved with full Excel Compatibility. |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | public function getExcelCompatibility() |
||
267 | |||
268 | /** |
||
269 | * Set whether the file should be saved with full Excel Compatibility. |
||
270 | * |
||
271 | * @param bool $pValue Set the file to be written as a fully Excel compatible csv file |
||
272 | * Note that this overrides other settings such as useBOM, enclosure and delimiter |
||
273 | * |
||
274 | * @return CSV |
||
275 | */ |
||
276 | public function setExcelCompatibility($pValue) |
||
277 | { |
||
278 | $this->excelCompatibility = $pValue; |
||
279 | |||
280 | return $this; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Get sheet index. |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | public function getSheetIndex() |
||
292 | |||
293 | /** |
||
294 | * Set sheet index. |
||
295 | * |
||
296 | * @param int $pValue Sheet index |
||
297 | * |
||
298 | * @return CSV |
||
299 | */ |
||
300 | public function setSheetIndex($pValue) |
||
306 | |||
307 | /** |
||
308 | * Write line to CSV file. |
||
309 | * |
||
310 | * @param resource $pFileHandle PHP filehandle |
||
311 | * @param array $pValues Array containing values in a row |
||
312 | * |
||
313 | * @throws Exception |
||
314 | */ |
||
315 | 1 | private function writeLine($pFileHandle, array $pValues) |
|
344 | } |
||
345 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..