Failed Conditions
Pull Request — develop_3.0 (#434)
by Hura
03:16
created

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