1 | <?php |
||
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) |
|
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) |
|
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) |
|
66 | |||
67 | /** |
||
68 | * Opens the CSV streamer and makes it ready to accept data. |
||
69 | * |
||
70 | * @return void |
||
71 | */ |
||
72 | 8 | protected function openWriter() |
|
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() |
|
115 | } |
||
116 |