1 | <?php |
||
27 | class Csv extends BaseWriter implements IWriter |
||
28 | { |
||
29 | /** |
||
30 | * PhpSpreadsheet object. |
||
31 | * |
||
32 | * @var PhpSpreadsheet |
||
33 | */ |
||
34 | private $spreadsheet; |
||
35 | |||
36 | /** |
||
37 | * Delimiter. |
||
38 | * |
||
39 | * @var string |
||
40 | */ |
||
41 | private $delimiter = ','; |
||
42 | |||
43 | /** |
||
44 | * Enclosure. |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $enclosure = '"'; |
||
49 | |||
50 | /** |
||
51 | * Line ending. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | private $lineEnding = PHP_EOL; |
||
56 | |||
57 | /** |
||
58 | * Sheet index to write. |
||
59 | * |
||
60 | * @var int |
||
61 | */ |
||
62 | private $sheetIndex = 0; |
||
63 | |||
64 | /** |
||
65 | * Whether to write a BOM (for UTF8). |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $useBOM = false; |
||
70 | |||
71 | /** |
||
72 | * Whether to write a Separator line as the first line of the file |
||
73 | * sep=x. |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | private $includeSeparatorLine = false; |
||
78 | |||
79 | /** |
||
80 | * Whether to write a fully Excel compatible CSV file. |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | private $excelCompatibility = false; |
||
85 | |||
86 | /** |
||
87 | * Create a new CSV. |
||
88 | * |
||
89 | * @param \PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet Spreadsheet object |
||
90 | */ |
||
91 | 2 | public function __construct(\PhpOffice\PhpSpreadsheet\Spreadsheet $spreadsheet) |
|
95 | |||
96 | /** |
||
97 | * Save PhpSpreadsheet to file. |
||
98 | * |
||
99 | * @param string $pFilename |
||
100 | * |
||
101 | * @throws Exception |
||
102 | */ |
||
103 | 2 | public function save($pFilename) |
|
104 | { |
||
105 | // Fetch sheet |
||
106 | 2 | $sheet = $this->spreadsheet->getSheet($this->sheetIndex); |
|
107 | |||
108 | 2 | $saveDebugLog = \PhpOffice\PhpSpreadsheet\Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog(); |
|
109 | 2 | \PhpOffice\PhpSpreadsheet\Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false); |
|
110 | 2 | $saveArrayReturnType = \PhpOffice\PhpSpreadsheet\Calculation::getArrayReturnType(); |
|
111 | 2 | \PhpOffice\PhpSpreadsheet\Calculation::setArrayReturnType(\PhpOffice\PhpSpreadsheet\Calculation::RETURN_ARRAY_AS_VALUE); |
|
112 | |||
113 | // Open file |
||
114 | 2 | $fileHandle = fopen($pFilename, 'wb+'); |
|
115 | 2 | if ($fileHandle === false) { |
|
116 | throw new Exception("Could not open file $pFilename for writing."); |
||
117 | } |
||
118 | |||
119 | 2 | if ($this->excelCompatibility) { |
|
120 | $this->setUseBOM(true); // Enforce UTF-8 BOM Header |
||
121 | $this->setIncludeSeparatorLine(true); // Set separator line |
||
122 | $this->setEnclosure('"'); // Set enclosure to " |
||
123 | $this->setDelimiter(';'); // Set delimiter to a semi-colon |
||
124 | $this->setLineEnding("\r\n"); |
||
125 | } |
||
126 | 2 | if ($this->useBOM) { |
|
127 | // Write the UTF-8 BOM code if required |
||
128 | 1 | fwrite($fileHandle, "\xEF\xBB\xBF"); |
|
129 | } |
||
130 | 2 | if ($this->includeSeparatorLine) { |
|
131 | // Write the separator line if required |
||
132 | fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding); |
||
133 | } |
||
134 | |||
135 | // Identify the range that we need to extract from the worksheet |
||
136 | 2 | $maxCol = $sheet->getHighestDataColumn(); |
|
137 | 2 | $maxRow = $sheet->getHighestDataRow(); |
|
138 | |||
139 | // Write rows to file |
||
140 | 2 | for ($row = 1; $row <= $maxRow; ++$row) { |
|
141 | // Convert the row to an array... |
||
142 | 2 | $cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas); |
|
143 | // ... and write to the file |
||
144 | 2 | $this->writeLine($fileHandle, $cellsArray[0]); |
|
145 | } |
||
146 | |||
147 | // Close file |
||
148 | 2 | fclose($fileHandle); |
|
149 | |||
150 | 2 | \PhpOffice\PhpSpreadsheet\Calculation::setArrayReturnType($saveArrayReturnType); |
|
151 | 2 | \PhpOffice\PhpSpreadsheet\Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog); |
|
152 | 2 | } |
|
153 | |||
154 | /** |
||
155 | * Get delimiter. |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function getDelimiter() |
||
160 | { |
||
161 | return $this->delimiter; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set delimiter. |
||
166 | * |
||
167 | * @param string $pValue Delimiter, defaults to ',' |
||
168 | * |
||
169 | * @return CSV |
||
170 | */ |
||
171 | public function setDelimiter($pValue) |
||
172 | { |
||
173 | $this->delimiter = $pValue; |
||
174 | |||
175 | return $this; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Get enclosure. |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getEnclosure() |
||
187 | |||
188 | /** |
||
189 | * Set enclosure. |
||
190 | * |
||
191 | * @param string $pValue Enclosure, defaults to " |
||
192 | * |
||
193 | * @return CSV |
||
194 | */ |
||
195 | public function setEnclosure($pValue) |
||
196 | { |
||
197 | if ($pValue == '') { |
||
198 | $pValue = null; |
||
199 | } |
||
200 | $this->enclosure = $pValue; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get line ending. |
||
207 | * |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getLineEnding() |
||
214 | |||
215 | /** |
||
216 | * Set line ending. |
||
217 | * |
||
218 | * @param string $pValue Line ending, defaults to OS line ending (PHP_EOL) |
||
219 | * |
||
220 | * @return CSV |
||
221 | */ |
||
222 | public function setLineEnding($pValue) |
||
223 | { |
||
224 | $this->lineEnding = $pValue; |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get whether BOM should be used. |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function getUseBOM() |
||
238 | |||
239 | /** |
||
240 | * Set whether BOM should be used. |
||
241 | * |
||
242 | * @param bool $pValue Use UTF-8 byte-order mark? Defaults to false |
||
243 | * |
||
244 | * @return CSV |
||
245 | */ |
||
246 | 1 | public function setUseBOM($pValue) |
|
252 | |||
253 | /** |
||
254 | * Get whether a separator line should be included. |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function getIncludeSeparatorLine() |
||
262 | |||
263 | /** |
||
264 | * Set whether a separator line should be included as the first line of the file. |
||
265 | * |
||
266 | * @param bool $pValue Use separator line? Defaults to false |
||
267 | * |
||
268 | * @return CSV |
||
269 | */ |
||
270 | public function setIncludeSeparatorLine($pValue) |
||
271 | { |
||
272 | $this->includeSeparatorLine = $pValue; |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get whether the file should be saved with full Excel Compatibility. |
||
279 | * |
||
280 | * @return bool |
||
281 | */ |
||
282 | public function getExcelCompatibility() |
||
286 | |||
287 | /** |
||
288 | * Set whether the file should be saved with full Excel Compatibility. |
||
289 | * |
||
290 | * @param bool $pValue Set the file to be written as a fully Excel compatible csv file |
||
291 | * Note that this overrides other settings such as useBOM, enclosure and delimiter |
||
292 | * |
||
293 | * @return CSV |
||
294 | */ |
||
295 | public function setExcelCompatibility($pValue) |
||
296 | { |
||
297 | $this->excelCompatibility = $pValue; |
||
298 | |||
299 | return $this; |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Get sheet index. |
||
304 | * |
||
305 | * @return int |
||
306 | */ |
||
307 | public function getSheetIndex() |
||
311 | |||
312 | /** |
||
313 | * Set sheet index. |
||
314 | * |
||
315 | * @param int $pValue Sheet index |
||
316 | * |
||
317 | * @return CSV |
||
318 | */ |
||
319 | public function setSheetIndex($pValue) |
||
320 | { |
||
321 | $this->sheetIndex = $pValue; |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Write line to CSV file. |
||
328 | * |
||
329 | * @param resource $pFileHandle PHP filehandle |
||
330 | * @param array $pValues Array containing values in a row |
||
331 | * |
||
332 | * @throws Exception |
||
333 | */ |
||
334 | 2 | private function writeLine($pFileHandle, array $pValues) |
|
363 | } |
||
364 |
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..