Test Failed
Push — develop ( 90366f...812a46 )
by Adrien
28:16
created

Csv::getUseBOM()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
8
class Csv extends BaseWriter
9
{
10
    /**
11
     * PhpSpreadsheet object.
12
     *
13
     * @var Spreadsheet
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 4
    public function __construct(Spreadsheet $spreadsheet)
73
    {
74 4
        $this->spreadsheet = $spreadsheet;
75 4
    }
76
77
    /**
78
     * Save PhpSpreadsheet to file.
79
     *
80
     * @param string $pFilename
81
     *
82
     * @throws Exception
83
     */
84 3
    public function save($pFilename)
85
    {
86
        // Fetch sheet
87 3
        $sheet = $this->spreadsheet->getSheet($this->sheetIndex);
88
89 3
        $saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog();
90 3
        Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false);
91 3
        $saveArrayReturnType = Calculation::getArrayReturnType();
92 3
        Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
93
94
        // Open file
95 3
        $fileHandle = fopen($pFilename, 'wb+');
96 3
        if ($fileHandle === false) {
97
            throw new Exception("Could not open file $pFilename for writing.");
98
        }
99
100 3
        if ($this->excelCompatibility) {
101 1
            $this->setUseBOM(true); //  Enforce UTF-8 BOM Header
102 1
            $this->setIncludeSeparatorLine(true); //  Set separator line
103 1
            $this->setEnclosure('"'); //  Set enclosure to "
104 1
            $this->setDelimiter(';'); //  Set delimiter to a semi-colon
105 1
            $this->setLineEnding("\r\n");
106
        }
107 3
        if ($this->useBOM) {
108
            // Write the UTF-8 BOM code if required
109 2
            fwrite($fileHandle, "\xEF\xBB\xBF");
110
        }
111 3
        if ($this->includeSeparatorLine) {
112
            // Write the separator line if required
113 1
            fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
114
        }
115
116
        //    Identify the range that we need to extract from the worksheet
117 3
        $maxCol = $sheet->getHighestDataColumn();
118 3
        $maxRow = $sheet->getHighestDataRow();
119
120
        // Write rows to file
121 3
        for ($row = 1; $row <= $maxRow; ++$row) {
122
            // Convert the row to an array...
123 3
            $cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas);
124
            // ... and write to the file
125 3
            $this->writeLine($fileHandle, $cellsArray[0]);
126
        }
127
128
        // Close file
129 3
        fclose($fileHandle);
130
131 3
        Calculation::setArrayReturnType($saveArrayReturnType);
132 3
        Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
133 3
    }
134
135
    /**
136
     * Get delimiter.
137
     *
138
     * @return string
139
     */
140 1
    public function getDelimiter()
141
    {
142 1
        return $this->delimiter;
143
    }
144
145
    /**
146
     * Set delimiter.
147
     *
148
     * @param string $pValue Delimiter, defaults to ','
149
     *
150
     * @return CSV
151
     */
152 1
    public function setDelimiter($pValue)
153
    {
154 1
        $this->delimiter = $pValue;
155
156 1
        return $this;
157
    }
158
159
    /**
160
     * Get enclosure.
161
     *
162
     * @return string
163
     */
164
    public function getEnclosure()
165
    {
166
        return $this->enclosure;
167
    }
168
169
    /**
170
     * Set enclosure.
171
     *
172
     * @param string $pValue Enclosure, defaults to "
173
     *
174
     * @return CSV
175
     */
176 1
    public function setEnclosure($pValue)
177
    {
178 1
        if ($pValue == '') {
179
            $pValue = null;
180
        }
181 1
        $this->enclosure = $pValue;
182
183 1
        return $this;
184
    }
185
186
    /**
187
     * Get line ending.
188
     *
189
     * @return string
190
     */
191
    public function getLineEnding()
192
    {
193
        return $this->lineEnding;
194
    }
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 1
    public function setLineEnding($pValue)
204
    {
205 1
        $this->lineEnding = $pValue;
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * Get whether BOM should be used.
212
     *
213
     * @return bool
214
     */
215
    public function getUseBOM()
216
    {
217
        return $this->useBOM;
218
    }
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 2
    public function setUseBOM($pValue)
228
    {
229 2
        $this->useBOM = $pValue;
230
231 2
        return $this;
232
    }
233
234
    /**
235
     * Get whether a separator line should be included.
236
     *
237
     * @return bool
238
     */
239
    public function getIncludeSeparatorLine()
240
    {
241
        return $this->includeSeparatorLine;
242
    }
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 1
    public function setIncludeSeparatorLine($pValue)
252
    {
253 1
        $this->includeSeparatorLine = $pValue;
254
255 1
        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()
264
    {
265
        return $this->excelCompatibility;
266
    }
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 1
    public function setExcelCompatibility($pValue)
277
    {
278 1
        $this->excelCompatibility = $pValue;
279
280 1
        return $this;
281
    }
282
283
    /**
284
     * Get sheet index.
285
     *
286
     * @return int
287
     */
288
    public function getSheetIndex()
289
    {
290
        return $this->sheetIndex;
291
    }
292
293
    /**
294
     * Set sheet index.
295
     *
296
     * @param int $pValue Sheet index
297
     *
298
     * @return CSV
299
     */
300 1
    public function setSheetIndex($pValue)
301
    {
302 1
        $this->sheetIndex = $pValue;
303
304 1
        return $this;
305
    }
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 3
    private function writeLine($pFileHandle, array $pValues)
316
    {
317
        // No leading delimiter
318 3
        $writeDelimiter = false;
319
320
        // Build the line
321 3
        $line = '';
322
323 3
        foreach ($pValues as $element) {
324
            // Escape enclosures
325 3
            $element = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $element);
326
327
            // Add delimiter
328 3
            if ($writeDelimiter) {
329 2
                $line .= $this->delimiter;
330
            } else {
331 3
                $writeDelimiter = true;
332
            }
333
334
            // Add enclosed string
335 3
            $line .= $this->enclosure . $element . $this->enclosure;
336
        }
337
338
        // Add line ending
339 3
        $line .= $this->lineEnding;
340
341
        // Write to file
342 3
        fwrite($pFileHandle, $line);
343 3
    }
344
}
345