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 |