Completed
Pull Request — develop_3.0 (#423)
by Adrien
02:30
created

Writer::setShouldAddBOM()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Box\Spout\Writer\CSV;
4
5
use Box\Spout\Writer\AbstractWriter;
6
use Box\Spout\Common\Exception\IOException;
7
use Box\Spout\Common\Helper\EncodingHelper;
8
use Box\Spout\Writer\Common\Options;
9
10
/**
11
 * Class Writer
12
 * This class provides support to write data to CSV files
13
 *
14
 * @package Box\Spout\Writer\CSV
15
 */
16
class Writer extends AbstractWriter
17
{
18
    /** Number of rows to write before flushing */
19
    const FLUSH_THRESHOLD = 500;
20
21
    /** @var string Content-Type value for the header */
22
    protected static $headerContentType = 'text/csv; charset=UTF-8';
23
24
    /** @var int */
25
    protected $lastWrittenRowIndex = 0;
26
27
    /**
28
     * Sets the field delimiter for the CSV
29
     *
30
     * @api
31
     * @param string $fieldDelimiter Character that delimits fields
32
     * @return Writer
33
     */
34 8
    public function setFieldDelimiter($fieldDelimiter)
35
    {
36 8
        $this->optionsManager->setOption(Options::FIELD_DELIMITER, $fieldDelimiter);
37 8
        return $this;
38
    }
39
40
    /**
41
     * Sets the field enclosure for the CSV
42
     *
43
     * @api
44
     * @param string $fieldEnclosure Character that enclose fields
45
     * @return Writer
46
     */
47 8
    public function setFieldEnclosure($fieldEnclosure)
48
    {
49 8
        $this->optionsManager->setOption(Options::FIELD_ENCLOSURE, $fieldEnclosure);
50 8
        return $this;
51
    }
52
53
    /**
54
     * Set if a BOM has to be added to the file
55
     *
56
     * @api
57
     * @param bool $shouldAddBOM
58
     * @return Writer
59
     */
60 8
    public function setShouldAddBOM($shouldAddBOM)
61
    {
62 8
        $this->optionsManager->setOption(Options::SHOULD_ADD_BOM, (bool) $shouldAddBOM);
63 8
        return $this;
64
    }
65
66
    /**
67
     * Opens the CSV streamer and makes it ready to accept data.
68
     *
69
     * @return void
70
     */
71 9
    protected function openWriter()
72
    {
73 9
        if ($this->optionsManager->getOption(Options::SHOULD_ADD_BOM)) {
74
            // Adds UTF-8 BOM for Unicode compatibility
75 8
            $this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
76
        }
77 9
    }
78
79
    /**
80
     * Adds data to the currently opened writer.
81
     *
82
     * @param  array $dataRow Array containing data to be written.
83
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
84
     * @param \Box\Spout\Writer\Style\Style $style Ignored here since CSV does not support styling.
85
     * @return void
86
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
87
     */
88 8
    protected function addRowToWriter(array $dataRow, $style)
89
    {
90 8
        $fieldDelimiter = $this->optionsManager->getOption(Options::FIELD_DELIMITER);
91 8
        $fieldEnclosure = $this->optionsManager->getOption(Options::FIELD_ENCLOSURE);
92
93 8
        $wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $fieldDelimiter, $fieldEnclosure);
94 8
        if ($wasWriteSuccessful === false) {
95
            throw new IOException('Unable to write data');
96
        }
97
98 8
        $this->lastWrittenRowIndex++;
99 8
        if ($this->lastWrittenRowIndex % self::FLUSH_THRESHOLD === 0) {
100
            $this->globalFunctionsHelper->fflush($this->filePointer);
101
        }
102 8
    }
103
104
    /**
105
     * Closes the CSV streamer, preventing any additional writing.
106
     * If set, sets the headers and redirects output to the browser.
107
     *
108
     * @return void
109
     */
110 9
    protected function closeWriter()
111
    {
112 9
        $this->lastWrittenRowIndex = 0;
113 9
    }
114
}
115