PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.15
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.15
6.2.15 6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 All 197 releases
fluentform / vendor / openspout / openspout / src / Writer / CSV / Writer.php

Writer.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.15, at vendor/openspout/openspout/src/Writer/CSV/Writer.php

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