| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* League.Csv (https://csv.thephpleague.com) |
| 5 |
* |
| 6 |
* (c) Ignace Nyamagana Butera <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace League\Csv; |
| 15 |
|
| 16 |
use function array_reduce; |
| 17 |
use function strlen; |
| 18 |
use const PHP_VERSION_ID; |
| 19 |
use const SEEK_CUR; |
| 20 |
use const STREAM_FILTER_WRITE; |
| 21 |
|
| 22 |
/** |
| 23 |
* A class to insert records into a CSV Document. |
| 24 |
*/ |
| 25 |
class Writer extends AbstractCsv |
| 26 |
{ |
| 27 |
protected const STREAM_FILTER_MODE = STREAM_FILTER_WRITE; |
| 28 |
|
| 29 |
/** @var array<callable> callable collection to format the record before insertion. */ |
| 30 |
protected array $formatters = []; |
| 31 |
/** @var array<callable> callable collection to validate the record before insertion. */ |
| 32 |
protected array $validators = []; |
| 33 |
protected string $newline = "\n"; |
| 34 |
protected int $flush_counter = 0; |
| 35 |
protected ?int $flush_threshold = null; |
| 36 |
|
| 37 |
protected function resetProperties(): void |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns the current newline sequence characters. |
| 43 |
*/ |
| 44 |
public function getNewline(): string |
| 45 |
{ |
| 46 |
return $this->newline; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get the flush threshold. |
| 51 |
*/ |
| 52 |
public function getFlushThreshold(): ?int |
| 53 |
{ |
| 54 |
return $this->flush_threshold; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Adds multiple records to the CSV document. |
| 59 |
* |
| 60 |
* @see Writer::insertOne |
| 61 |
*/ |
| 62 |
public function insertAll(iterable $records): int |
| 63 |
{ |
| 64 |
$bytes = 0; |
| 65 |
foreach ($records as $record) { |
| 66 |
$bytes += $this->insertOne($record); |
| 67 |
} |
| 68 |
|
| 69 |
$this->flush_counter = 0; |
| 70 |
$this->document->fflush(); |
| 71 |
|
| 72 |
return $bytes; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Adds a single record to a CSV document. |
| 77 |
* |
| 78 |
* A record is an array that can contains scalar types values, NULL values |
| 79 |
* or objects implementing the __toString method. |
| 80 |
* |
| 81 |
* @throws CannotInsertRecord If the record can not be inserted |
| 82 |
*/ |
| 83 |
public function insertOne(array $record): int |
| 84 |
{ |
| 85 |
$record = array_reduce($this->formatters, fn (array $record, callable $formatter): array => $formatter($record), $record); |
| 86 |
$this->validateRecord($record); |
| 87 |
$bytes = $this->addRecord($record); |
| 88 |
if (false === $bytes || 0 >= $bytes) { |
| 89 |
throw CannotInsertRecord::triggerOnInsertion($record); |
| 90 |
} |
| 91 |
|
| 92 |
return $bytes + $this->consolidate(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Adds a single record to a CSV Document using PHP algorithm. |
| 97 |
* |
| 98 |
* @see https://php.net/manual/en/function.fputcsv.php |
| 99 |
* |
| 100 |
* @return int|false |
| 101 |
*/ |
| 102 |
protected function addRecord(array $record) |
| 103 |
{ |
| 104 |
if (PHP_VERSION_ID < 80100) { |
| 105 |
return $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape); |
| 106 |
} |
| 107 |
|
| 108 |
return $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape, $this->newline); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* DEPRECATION WARNING! This method will be removed in the next major point release. |
| 113 |
* |
| 114 |
* @deprecated since version 9.8.0 |
| 115 |
* @codeCoverageIgnore |
| 116 |
* |
| 117 |
* Format a record. |
| 118 |
* |
| 119 |
* The returned array must contain |
| 120 |
* - scalar types values, |
| 121 |
* - NULL values, |
| 122 |
* - or objects implementing the __toString() method. |
| 123 |
*/ |
| 124 |
protected function formatRecord(array $record, callable $formatter): array |
| 125 |
{ |
| 126 |
return $formatter($record); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Validate a record. |
| 131 |
* |
| 132 |
* @throws CannotInsertRecord If the validation failed |
| 133 |
*/ |
| 134 |
protected function validateRecord(array $record): void |
| 135 |
{ |
| 136 |
foreach ($this->validators as $name => $validator) { |
| 137 |
if (true !== $validator($record)) { |
| 138 |
throw CannotInsertRecord::triggerOnValidation($name, $record); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Apply post insertion actions. |
| 145 |
*/ |
| 146 |
protected function consolidate(): int |
| 147 |
{ |
| 148 |
$bytes = 0; |
| 149 |
if (80100 > PHP_VERSION_ID && "\n" !== $this->newline) { |
| 150 |
$this->document->fseek(-1, SEEK_CUR); |
| 151 |
/** @var int $newlineBytes */ |
| 152 |
$newlineBytes = $this->document->fwrite($this->newline, strlen($this->newline)); |
| 153 |
$bytes = $newlineBytes - 1; |
| 154 |
} |
| 155 |
|
| 156 |
if (null === $this->flush_threshold) { |
| 157 |
return $bytes; |
| 158 |
} |
| 159 |
|
| 160 |
++$this->flush_counter; |
| 161 |
if (0 === $this->flush_counter % $this->flush_threshold) { |
| 162 |
$this->flush_counter = 0; |
| 163 |
$this->document->fflush(); |
| 164 |
} |
| 165 |
|
| 166 |
return $bytes; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Adds a record formatter. |
| 171 |
*/ |
| 172 |
public function addFormatter(callable $formatter): self |
| 173 |
{ |
| 174 |
$this->formatters[] = $formatter; |
| 175 |
|
| 176 |
return $this; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Adds a record validator. |
| 181 |
*/ |
| 182 |
public function addValidator(callable $validator, string $validator_name): self |
| 183 |
{ |
| 184 |
$this->validators[$validator_name] = $validator; |
| 185 |
|
| 186 |
return $this; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Sets the newline sequence. |
| 191 |
*/ |
| 192 |
public function setNewline(string $newline): self |
| 193 |
{ |
| 194 |
$this->newline = $newline; |
| 195 |
|
| 196 |
return $this; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Set the flush threshold. |
| 201 |
* |
| 202 |
* @param ?int $threshold |
| 203 |
* |
| 204 |
* @throws InvalidArgument if the threshold is a integer lesser than 1 |
| 205 |
*/ |
| 206 |
public function setFlushThreshold(?int $threshold): self |
| 207 |
{ |
| 208 |
if ($threshold === $this->flush_threshold) { |
| 209 |
return $this; |
| 210 |
} |
| 211 |
|
| 212 |
if (null !== $threshold && 1 > $threshold) { |
| 213 |
throw InvalidArgument::dueToInvalidThreshold($threshold, __METHOD__); |
| 214 |
} |
| 215 |
|
| 216 |
$this->flush_threshold = $threshold; |
| 217 |
$this->flush_counter = 0; |
| 218 |
$this->document->fflush(); |
| 219 |
|
| 220 |
return $this; |
| 221 |
} |
| 222 |
} |
| 223 |
|