PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.15.0
Independent Analytics – WordPress Analytics Plugin v2.15.0
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / vendor / league / csv / src / Writer.php
independent-analytics / vendor / league / csv / src Last commit date
AbstractCsv.php 4 days ago ByteSequence.php 4 days ago CannotInsertRecord.php 4 days ago CharsetConverter.php 4 days ago ColumnConsistency.php 4 days ago EncloseField.php 4 days ago EscapeFormula.php 4 days ago Exception.php 9 months ago HTMLConverter.php 4 days ago Info.php 4 days ago InvalidArgument.php 9 months ago MapIterator.php 4 days ago RFC4180Field.php 4 days ago Reader.php 4 days ago ResultSet.php 4 days ago Statement.php 4 days ago Stream.php 4 days ago SyntaxError.php 4 days ago TabularDataReader.php 4 days ago UnableToProcessCsv.php 9 months ago UnavailableFeature.php 9 months ago UnavailableStream.php 9 months ago Writer.php 4 days ago XMLConverter.php 4 days ago functions.php 9 months ago functions_include.php 9 months ago
Writer.php
194 lines
1 <?php
2
3 /**
4 * League.Csv (https://csv.thephpleague.com)
5 *
6 * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 declare (strict_types=1);
12 namespace IAWPSCOPED\League\Csv;
13
14 use function array_reduce;
15 use function strlen;
16 use const PHP_VERSION_ID;
17 use const SEEK_CUR;
18 use const STREAM_FILTER_WRITE;
19 /**
20 * A class to insert records into a CSV Document.
21 * @internal
22 */
23 class Writer extends AbstractCsv
24 {
25 protected const STREAM_FILTER_MODE = STREAM_FILTER_WRITE;
26 /** @var array<callable> callable collection to format the record before insertion. */
27 protected array $formatters = [];
28 /** @var array<callable> callable collection to validate the record before insertion. */
29 protected array $validators = [];
30 protected string $newline = "\n";
31 protected int $flush_counter = 0;
32 protected ?int $flush_threshold = null;
33 protected function resetProperties() : void
34 {
35 }
36 /**
37 * Returns the current newline sequence characters.
38 */
39 public function getNewline() : string
40 {
41 return $this->newline;
42 }
43 /**
44 * Get the flush threshold.
45 */
46 public function getFlushThreshold() : ?int
47 {
48 return $this->flush_threshold;
49 }
50 /**
51 * Adds multiple records to the CSV document.
52 *
53 * @see Writer::insertOne
54 */
55 public function insertAll(iterable $records) : int
56 {
57 $bytes = 0;
58 foreach ($records as $record) {
59 $bytes += $this->insertOne($record);
60 }
61 $this->flush_counter = 0;
62 $this->document->fflush();
63 return $bytes;
64 }
65 /**
66 * Adds a single record to a CSV document.
67 *
68 * A record is an array that can contains scalar types values, NULL values
69 * or objects implementing the __toString method.
70 *
71 * @throws CannotInsertRecord If the record can not be inserted
72 */
73 public function insertOne(array $record) : int
74 {
75 $record = array_reduce($this->formatters, fn(array $record, callable $formatter): array => $formatter($record), $record);
76 $this->validateRecord($record);
77 $bytes = $this->addRecord($record);
78 if (\false === $bytes || 0 >= $bytes) {
79 throw CannotInsertRecord::triggerOnInsertion($record);
80 }
81 return $bytes + $this->consolidate();
82 }
83 /**
84 * Adds a single record to a CSV Document using PHP algorithm.
85 *
86 * @see https://php.net/manual/en/function.fputcsv.php
87 *
88 * @return int|false
89 */
90 protected function addRecord(array $record)
91 {
92 if (PHP_VERSION_ID < 80100) {
93 return $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape);
94 }
95 return $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape, $this->newline);
96 }
97 /**
98 * DEPRECATION WARNING! This method will be removed in the next major point release.
99 *
100 * @deprecated since version 9.8.0
101 * @codeCoverageIgnore
102 *
103 * Format a record.
104 *
105 * The returned array must contain
106 * - scalar types values,
107 * - NULL values,
108 * - or objects implementing the __toString() method.
109 */
110 protected function formatRecord(array $record, callable $formatter) : array
111 {
112 return $formatter($record);
113 }
114 /**
115 * Validate a record.
116 *
117 * @throws CannotInsertRecord If the validation failed
118 */
119 protected function validateRecord(array $record) : void
120 {
121 foreach ($this->validators as $name => $validator) {
122 if (\true !== $validator($record)) {
123 throw CannotInsertRecord::triggerOnValidation($name, $record);
124 }
125 }
126 }
127 /**
128 * Apply post insertion actions.
129 */
130 protected function consolidate() : int
131 {
132 $bytes = 0;
133 if (80100 > PHP_VERSION_ID && "\n" !== $this->newline) {
134 $this->document->fseek(-1, SEEK_CUR);
135 /** @var int $newlineBytes */
136 $newlineBytes = $this->document->fwrite($this->newline, strlen($this->newline));
137 $bytes = $newlineBytes - 1;
138 }
139 if (null === $this->flush_threshold) {
140 return $bytes;
141 }
142 ++$this->flush_counter;
143 if (0 === $this->flush_counter % $this->flush_threshold) {
144 $this->flush_counter = 0;
145 $this->document->fflush();
146 }
147 return $bytes;
148 }
149 /**
150 * Adds a record formatter.
151 */
152 public function addFormatter(callable $formatter) : self
153 {
154 $this->formatters[] = $formatter;
155 return $this;
156 }
157 /**
158 * Adds a record validator.
159 */
160 public function addValidator(callable $validator, string $validator_name) : self
161 {
162 $this->validators[$validator_name] = $validator;
163 return $this;
164 }
165 /**
166 * Sets the newline sequence.
167 */
168 public function setNewline(string $newline) : self
169 {
170 $this->newline = $newline;
171 return $this;
172 }
173 /**
174 * Set the flush threshold.
175 *
176 * @param ?int $threshold
177 *
178 * @throws InvalidArgument if the threshold is a integer lesser than 1
179 */
180 public function setFlushThreshold(?int $threshold) : self
181 {
182 if ($threshold === $this->flush_threshold) {
183 return $this;
184 }
185 if (null !== $threshold && 1 > $threshold) {
186 throw InvalidArgument::dueToInvalidThreshold($threshold, __METHOD__);
187 }
188 $this->flush_threshold = $threshold;
189 $this->flush_counter = 0;
190 $this->document->fflush();
191 return $this;
192 }
193 }
194