Completed
Push — master ( 3090c1...7517cd )
by Adrien
12:19
created

Csv   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 332
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 332
ccs 80
cts 80
cp 1
rs 10
c 0
b 0
f 0
wmc 27

17 Methods

Rating   Name   Duplication   Size   Complexity  
A setExcelCompatibility() 0 5 1
A setSheetIndex() 0 5 1
A writeLine() 0 28 3
A setLineEnding() 0 5 1
A setIncludeSeparatorLine() 0 5 1
A setUseBOM() 0 5 1
A __construct() 0 3 1
A getDelimiter() 0 3 1
A setDelimiter() 0 5 1
A getSheetIndex() 0 3 1
A getUseBOM() 0 3 1
A getIncludeSeparatorLine() 0 3 1
A getEnclosure() 0 3 1
A setEnclosure() 0 5 2
A getExcelCompatibility() 0 3 1
A getLineEnding() 0 3 1
B save() 0 53 8
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Writer;
4
5
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
6
use PhpOffice\PhpSpreadsheet\Spreadsheet;
7
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
8
9
class Csv extends BaseWriter
10
{
11
    /**
12
     * PhpSpreadsheet object.
13
     *
14
     * @var Spreadsheet
15
     */
16
    private $spreadsheet;
17
18
    /**
19
     * Delimiter.
20
     *
21
     * @var string
22
     */
23
    private $delimiter = ',';
24
25
    /**
26
     * Enclosure.
27
     *
28
     * @var string
29
     */
30
    private $enclosure = '"';
31
32
    /**
33
     * Line ending.
34
     *
35
     * @var string
36
     */
37
    private $lineEnding = PHP_EOL;
38
39
    /**
40
     * Sheet index to write.
41
     *
42
     * @var int
43
     */
44
    private $sheetIndex = 0;
45
46
    /**
47
     * Whether to write a BOM (for UTF8).
48
     *
49
     * @var bool
50
     */
51
    private $useBOM = false;
52
53
    /**
54
     * Whether to write a Separator line as the first line of the file
55
     *     sep=x.
56
     *
57
     * @var bool
58
     */
59
    private $includeSeparatorLine = false;
60
61
    /**
62
     * Whether to write a fully Excel compatible CSV file.
63
     *
64
     * @var bool
65
     */
66
    private $excelCompatibility = false;
67
68
    /**
69
     * Create a new CSV.
70
     *
71
     * @param Spreadsheet $spreadsheet Spreadsheet object
72
     */
73 8
    public function __construct(Spreadsheet $spreadsheet)
74
    {
75 8
        $this->spreadsheet = $spreadsheet;
76 8
    }
77
78
    /**
79
     * Save PhpSpreadsheet to file.
80
     *
81
     * @param resource|string $pFilename
82
     */
83 6
    public function save($pFilename)
84
    {
85
        // Fetch sheet
86 6
        $sheet = $this->spreadsheet->getSheet($this->sheetIndex);
87
88 6
        $saveDebugLog = Calculation::getInstance($this->spreadsheet)->getDebugLog()->getWriteDebugLog();
89 6
        Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog(false);
90 6
        $saveArrayReturnType = Calculation::getArrayReturnType();
91 6
        Calculation::setArrayReturnType(Calculation::RETURN_ARRAY_AS_VALUE);
92
93
        // Open file
94 6
        if (is_resource($pFilename)) {
95 1
            $fileHandle = $pFilename;
96 5
        } elseif (!$pFilename) {
97 1
            $fileHandle = false;
98
        } else {
99 4
            $fileHandle = fopen($pFilename, 'wb+');
100
        }
101
102 6
        if ($fileHandle === false) {
103 1
            throw new WriterException("Could not open file $pFilename for writing.");
104
        }
105
106 5
        if ($this->excelCompatibility) {
107 1
            $this->setUseBOM(true); //  Enforce UTF-8 BOM Header
108 1
            $this->setIncludeSeparatorLine(true); //  Set separator line
109 1
            $this->setEnclosure('"'); //  Set enclosure to "
110 1
            $this->setDelimiter(';'); //  Set delimiter to a semi-colon
111 1
            $this->setLineEnding("\r\n");
112
        }
113 5
        if ($this->useBOM) {
114
            // Write the UTF-8 BOM code if required
115 2
            fwrite($fileHandle, "\xEF\xBB\xBF");
116
        }
117 5
        if ($this->includeSeparatorLine) {
118
            // Write the separator line if required
119 1
            fwrite($fileHandle, 'sep=' . $this->getDelimiter() . $this->lineEnding);
120
        }
121
122
        //    Identify the range that we need to extract from the worksheet
123 5
        $maxCol = $sheet->getHighestDataColumn();
124 5
        $maxRow = $sheet->getHighestDataRow();
125
126
        // Write rows to file
127 5
        for ($row = 1; $row <= $maxRow; ++$row) {
128
            // Convert the row to an array...
129 5
            $cellsArray = $sheet->rangeToArray('A' . $row . ':' . $maxCol . $row, '', $this->preCalculateFormulas);
130
            // ... and write to the file
131 5
            $this->writeLine($fileHandle, $cellsArray[0]);
132
        }
133
134 5
        Calculation::setArrayReturnType($saveArrayReturnType);
135 5
        Calculation::getInstance($this->spreadsheet)->getDebugLog()->setWriteDebugLog($saveDebugLog);
136 5
    }
137
138
    /**
139
     * Get delimiter.
140
     *
141
     * @return string
142
     */
143 1
    public function getDelimiter()
144
    {
145 1
        return $this->delimiter;
146
    }
147
148
    /**
149
     * Set delimiter.
150
     *
151
     * @param string $pValue Delimiter, defaults to ','
152
     *
153
     * @return $this
154
     */
155 1
    public function setDelimiter($pValue)
156
    {
157 1
        $this->delimiter = $pValue;
158
159 1
        return $this;
160
    }
161
162
    /**
163
     * Get enclosure.
164
     *
165
     * @return string
166
     */
167 1
    public function getEnclosure()
168
    {
169 1
        return $this->enclosure;
170
    }
171
172
    /**
173
     * Set enclosure.
174
     *
175
     * @param string $pValue Enclosure, defaults to "
176
     *
177
     * @return $this
178
     */
179 2
    public function setEnclosure($pValue)
180
    {
181 2
        $this->enclosure = $pValue ? $pValue : '"';
182
183 2
        return $this;
184
    }
185
186
    /**
187
     * Get line ending.
188
     *
189
     * @return string
190
     */
191 1
    public function getLineEnding()
192
    {
193 1
        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 $this
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 1
    public function getUseBOM()
216
    {
217 1
        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 $this
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 1
    public function getIncludeSeparatorLine()
240
    {
241 1
        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 $this
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 1
    public function getExcelCompatibility()
264
    {
265 1
        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 $this
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 2
    public function getSheetIndex()
289
    {
290 2
        return $this->sheetIndex;
291
    }
292
293
    /**
294
     * Set sheet index.
295
     *
296
     * @param int $pValue Sheet index
297
     *
298
     * @return $this
299
     */
300 2
    public function setSheetIndex($pValue)
301
    {
302 2
        $this->sheetIndex = $pValue;
303
304 2
        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 5
    private function writeLine($pFileHandle, array $pValues)
314
    {
315
        // No leading delimiter
316 5
        $writeDelimiter = false;
317
318
        // Build the line
319 5
        $line = '';
320
321 5
        foreach ($pValues as $element) {
322
            // Escape enclosures
323 5
            $element = str_replace($this->enclosure, $this->enclosure . $this->enclosure, $element);
324
325
            // Add delimiter
326 5
            if ($writeDelimiter) {
327 2
                $line .= $this->delimiter;
328
            } else {
329 5
                $writeDelimiter = true;
330
            }
331
332
            // Add enclosed string
333 5
            $line .= $this->enclosure . $element . $this->enclosure;
334
        }
335
336
        // Add line ending
337 5
        $line .= $this->lineEnding;
338
339
        // Write to file
340 5
        fwrite($pFileHandle, $line);
341 5
    }
342
}
343