Failed Conditions
Pull Request — master (#209)
by
unknown
02:31
created

Writer::openWriter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 0
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
9
/**
10
 * Class Writer
11
 * This class provides support to write data to CSV files
12
 *
13
 * @package Box\Spout\Writer\CSV
14
 */
15
class Writer extends AbstractWriter
16
{
17
    /** Number of rows to write before flushing */
18
    const FLUSH_THRESHOLD = 500;
19
20
    /** @var string Content-Type value for the header */
21
    protected static $headerContentType = 'text/csv; charset=UTF-8';
22
23
    /** @var string Defines the character used to delimit fields (one character only) */
24
    protected $fieldDelimiter = ',';
25
26
    /** @var string Defines the character used to enclose fields (one character only) */
27
    protected $fieldEnclosure = '"';
28
29
    /** @var int */
30
    protected $lastWrittenRowIndex = 0;
31
32
    /**
33
     * Sets the field delimiter for the CSV
34
     *
35
     * @api
36
     * @param string $fieldDelimiter Character that delimits fields
37
     * @return Writer
38
     */
39 15
    public function setFieldDelimiter($fieldDelimiter)
40
    {
41 15
        $this->fieldDelimiter = $fieldDelimiter;
42 15
        return $this;
43
    }
44
45
    /**
46
     * Sets the field enclosure for the CSV
47
     *
48
     * @api
49
     * @param string $fieldEnclosure Character that enclose fields
50
     * @return Writer
51
     */
52 15
    public function setFieldEnclosure($fieldEnclosure)
53
    {
54 15
        $this->fieldEnclosure = $fieldEnclosure;
55 15
        return $this;
56
    }
57
58
    /**
59
     * Opens the CSV streamer and makes it ready to accept data.
60
     *
61
     * @return void
62
     */
63 15
    protected function openWriter()
64
    {
65
        // Adds UTF-8 BOM for Unicode compatibility
66 15
        $this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
67 15
    }
68
69
    public function registerStyle($style) {
0 ignored issues
show
Unused Code introduced by
The parameter $style is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
        return false;
71
    }
72
73
    /**
74
     * Adds data to the currently opened writer.
75
     *
76
     * @param  array $dataRow Array containing data to be written.
77
     *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
78
     * @param \Box\Spout\Writer\Style\Style $style Ignored here since CSV does not support styling.
79
     * @return void
80
     * @throws \Box\Spout\Common\Exception\IOException If unable to write data
81
     */
82 15
    protected function addRowToWriter(array $dataRow, $style)
83
    {
84 15
        $wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $this->fieldDelimiter, $this->fieldEnclosure);
85 15
        if ($wasWriteSuccessful === false) {
86
            throw new IOException('Unable to write data');
87
        }
88
89 15
        $this->lastWrittenRowIndex++;
90 15
        if ($this->lastWrittenRowIndex % self::FLUSH_THRESHOLD === 0) {
91
            $this->globalFunctionsHelper->fflush($this->filePointer);
92
        }
93 15
    }
94
95
    /**
96
     * Closes the CSV streamer, preventing any additional writing.
97
     * If set, sets the headers and redirects output to the browser.
98
     *
99
     * @return void
100
     */
101 15
    protected function closeWriter()
102
    {
103 15
        $this->lastWrittenRowIndex = 0;
104 15
    }
105
}
106