PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / vendor / openspout / openspout / src / Writer / CSV / Writer.php

Writer.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.19, at vendor/openspout/openspout/src/Writer/CSV/Writer.php

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