PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.2.4
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.2.4
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 3.6.65 All 196 releases
fluentform / app / Services / csv / src / Writer.php

Writer.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 1.2.4, at app/Services/csv/src/Writer.php

171 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the League.csv library
4 *
5 * @license http://opensource.org/licenses/MIT
6 * @link https://github.com/thephpleague/csv/
7 * @version 8.2.2
8 * @package League.csv
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13 namespace League\Csv;
14
15 use InvalidArgumentException;
16 use League\Csv\Modifier\RowFilter;
17 use League\Csv\Modifier\StreamIterator;
18 use ReflectionMethod;
19 use RuntimeException;
20 use SplFileObject;
21 use Traversable;
22
23 /**
24 * A class to manage data insertion into a CSV
25 *
26 * @package League.csv
27 * @since 4.0.0
28 *
29 */
30 class Writer extends AbstractCsv
31 {
32 use RowFilter;
33
34 /**
35 * @inheritdoc
36 */
37 protected $stream_filter_mode = STREAM_FILTER_WRITE;
38
39 /**
40 * The CSV object holder
41 *
42 * @var SplFileObject|StreamIterator
43 */
44 protected $csv;
45
46 /**
47 * fputcsv method from SplFileObject or StreamIterator
48 *
49 * @var ReflectionMethod
50 */
51 protected $fputcsv;
52
53 /**
54 * Nb parameters for SplFileObject::fputcsv method
55 *
56 * @var integer
57 */
58 protected $fputcsv_param_count;
59
60 /**
61 * Adds multiple lines to the CSV document
62 *
63 * a simple wrapper method around insertOne
64 *
65 * @param Traversable|array $rows a multidimensional array or a Traversable object
66 *
67 * @throws InvalidArgumentException If the given rows format is invalid
68 *
69 * @return static
70 */
71 public function insertAll($rows)
72 {
73 if (!is_array($rows) && !$rows instanceof Traversable) {
74 throw new InvalidArgumentException(
75 'the provided data must be an array OR a `Traversable` object'
76 );
77 }
78
79 foreach ($rows as $row) {
80 $this->insertOne($row);
81 }
82
83 return $this;
84 }
85
86 /**
87 * Adds a single line to a CSV document
88 *
89 * @param string[]|string $row a string, an array or an object implementing to '__toString' method
90 *
91 * @return static
92 */
93 public function insertOne($row)
94 {
95 if (!is_array($row)) {
96 $row = str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
97 }
98 $row = $this->formatRow($row);
99 $this->validateRow($row);
100 $this->addRow($row);
101
102 return $this;
103 }
104
105 /**
106 * Add new record to the CSV document
107 *
108 * @param array $row record to add
109 */
110 protected function addRow(array $row)
111 {
112 $this->initCsv();
113 if (!$this->fputcsv->invokeArgs($this->csv, $this->getFputcsvParameters($row))) {
114 throw new RuntimeException('Unable to write record to the CSV document.');
115 }
116
117 if ("\n" !== $this->newline) {
118 $this->csv->fseek(-1, SEEK_CUR);
119 $this->csv->fwrite($this->newline, strlen($this->newline));
120 }
121 }
122
123 /**
124 * Initialize the CSV object and settings
125 */
126 protected function initCsv()
127 {
128 if (null !== $this->csv) {
129 return;
130 }
131
132 $this->csv = $this->getIterator();
133 $this->fputcsv = new ReflectionMethod(get_class($this->csv), 'fputcsv');
134 $this->fputcsv_param_count = $this->fputcsv->getNumberOfParameters();
135 }
136
137 /**
138 * returns the parameters for SplFileObject::fputcsv
139 *
140 * @param array $fields The fields to be add
141 *
142 * @return array
143 */
144 protected function getFputcsvParameters(array $fields)
145 {
146 $parameters = [$fields, $this->delimiter, $this->enclosure];
147 if (4 == $this->fputcsv_param_count) {
148 $parameters[] = $this->escape;
149 }
150
151 return $parameters;
152 }
153
154 /**
155 * {@inheritdoc}
156 */
157 public function isActiveStreamFilter()
158 {
159 return parent::isActiveStreamFilter() && null === $this->csv;
160 }
161
162 /**
163 * {@inheritdoc}
164 */
165 public function __destruct()
166 {
167 $this->csv = null;
168 parent::__destruct();
169 }
170 }
171