Skip to content

Commit

Permalink
Improve Writer speed by precalculating the insert method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 9, 2025
1 parent 83b2aa9 commit e9e4bc3
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class Writer extends AbstractCsv implements TabularDataWriter
protected const ENCLOSE_NONE = -1;

protected const STREAM_FILTER_MODE = STREAM_FILTER_WRITE;
/** @var array<callable> callable collection to format the record before insertion. */
/** @var array<Closure(array): array> callable collection to format the record before insertion. */
protected array $formatters = [];
/** @var array<callable> callable collection to validate the record before insertion. */
/** @var array<Closure(array): bool> callable collection to validate the record before insertion. */
protected array $validators = [];
protected string $newline = "\n";
protected int $flush_counter = 0;
Expand All @@ -57,16 +57,16 @@ protected function resetProperties(): void
[$this->enclosure.$this->enclosure, $this->escape.$this->enclosure],
];

$this->insertRecord = fn (array $record): int|false => match ($this->enclose_all) {
self::ENCLOSE_ALL => $this->document->fwrite(implode(
$this->insertRecord = match ($this->enclose_all) {
self::ENCLOSE_ALL => fn (array $record): int|false => $this->document->fwrite(implode(
$this->delimiter,
array_map(
fn ($content) => $this->enclosure.$content.$this->enclosure,
str_replace($this->enclosure_replace[0], $this->enclosure_replace[1], $record)
)
).$this->newline),
self::ENCLOSE_NONE => $this->document->fwrite(implode($this->delimiter, $record).$this->newline),
default => $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape, $this->newline),
self::ENCLOSE_NONE => fn (array $record): int|false => $this->document->fwrite(implode($this->delimiter, $record).$this->newline),
default => fn (array $record): int|false => $this->document->fputcsv($record, $this->delimiter, $this->enclosure, $this->escape, $this->newline),
};
}

Expand Down Expand Up @@ -178,20 +178,24 @@ protected function validateRecord(array $record): void

/**
* Adds a record formatter.
*
* @param callable(array): array $formatter
*/
public function addFormatter(callable $formatter): self
{
$this->formatters[] = $formatter;
$this->formatters[] = !$formatter instanceof Closure ? $formatter(...) : $formatter;

return $this;
}

/**
* Adds a record validator.
*
* @param callable(array): bool $validator
*/
public function addValidator(callable $validator, string $validator_name): self
{
$this->validators[$validator_name] = $validator;
$this->validators[$validator_name] = !$validator instanceof Closure ? $validator(...) : $validator;

return $this;
}
Expand Down Expand Up @@ -318,7 +322,7 @@ protected function consolidate(): int
*
* Returns the current newline sequence characters.
*/
#[Deprecated(message:'use League\Csv\Writer::getEndOfLine()', since:'league/csv:9.8.0')]
#[Deprecated(message:'use League\Csv\Writer::getEndOfLine()', since:'league/csv:9.10.0')]
public function getNewline(): string
{
return $this->getEndOfLine();
Expand All @@ -333,7 +337,7 @@ public function getNewline(): string
*
* Sets the newline sequence.
*/
#[Deprecated(message:'use League\Csv\Writer::setEndOfLine()', since:'league/csv:9.8.0')]
#[Deprecated(message:'use League\Csv\Writer::setEndOfLine()', since:'league/csv:9.10.0')]
public function setNewline(string $newline): self
{
return $this->setEndOfLine($newline);
Expand Down

0 comments on commit e9e4bc3

Please sign in to comment.