|
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
|
|
|
/** @var bool */ |
|
33
|
|
|
protected $shouldAddBOM = true; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Sets the field delimiter for the CSV |
|
37
|
|
|
* |
|
38
|
|
|
* @api |
|
39
|
|
|
* @param string $fieldDelimiter Character that delimits fields |
|
40
|
|
|
* @return Writer |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setFieldDelimiter($fieldDelimiter) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->fieldDelimiter = $fieldDelimiter; |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets the field enclosure for the CSV |
|
50
|
|
|
* |
|
51
|
|
|
* @api |
|
52
|
|
|
* @param string $fieldEnclosure Character that enclose fields |
|
53
|
|
|
* @return Writer |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setFieldEnclosure($fieldEnclosure) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->fieldEnclosure = $fieldEnclosure; |
|
58
|
|
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Set if a BOM has to be added to the file |
|
63
|
|
|
* |
|
64
|
|
|
* @param bool $shouldAddBOM |
|
65
|
|
|
* @return Writer |
|
66
|
|
|
*/ |
|
67
|
|
|
public function setShouldAddBOM($shouldAddBOM) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->shouldAddBOM = (bool) $shouldAddBOM; |
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Opens the CSV streamer and makes it ready to accept data. |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function openWriter() |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->shouldAddBOM) { |
|
81
|
|
|
// Adds UTF-8 BOM for Unicode compatibility |
|
82
|
|
|
$this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Adds data to the currently opened writer. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $dataRow Array containing data to be written. |
|
90
|
|
|
* Example $dataRow = ['data1', 1234, null, '', 'data5']; |
|
91
|
|
|
* @param \Box\Spout\Writer\Style\Style $style Ignored here since CSV does not support styling. |
|
92
|
|
|
* @return void |
|
93
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If unable to write data |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function addRowToWriter(array $dataRow, $style) |
|
96
|
|
|
{ |
|
97
|
|
|
$wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $this->fieldDelimiter, $this->fieldEnclosure); |
|
98
|
|
|
if ($wasWriteSuccessful === false) { |
|
99
|
|
|
throw new IOException('Unable to write data'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->lastWrittenRowIndex++; |
|
103
|
|
|
if ($this->lastWrittenRowIndex % self::FLUSH_THRESHOLD === 0) { |
|
104
|
|
|
$this->globalFunctionsHelper->fflush($this->filePointer); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Closes the CSV streamer, preventing any additional writing. |
|
110
|
|
|
* If set, sets the headers and redirects output to the browser. |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function closeWriter() |
|
115
|
|
|
{ |
|
116
|
|
|
$this->lastWrittenRowIndex = 0; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|