Skip to content

Commit

Permalink
Improve codebase and simplify CSV record formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 19, 2025
1 parent e3e2a42 commit 41d08de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
15 changes: 15 additions & 0 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\Csv;

use Closure;
use Deprecated;
use Generator;
use InvalidArgumentException;
Expand Down Expand Up @@ -51,6 +52,8 @@ abstract class AbstractCsv implements ByteSequence
protected string $enclosure = '"';
protected string $escape = '\\';
protected bool $is_input_bom_included = false;
/** @var array<Closure(array): array> collection of Closure to format the record before reading. */
protected array $formatters = [];

/**
* @final This method should not be overwritten in child classes
Expand Down Expand Up @@ -343,6 +346,18 @@ public function setEscape(string $escape): static
return $this;
}

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

return $this;
}

/**
* Enables BOM Stripping.
*/
Expand Down
14 changes: 1 addition & 13 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,12 @@ class Reader extends AbstractCsv implements TabularDataReader, JsonSerializable
protected bool $is_empty_records_included = false;
/** @var array<string> header record. */
protected array $header = [];
/** @var array<callable> callable collection to format the record before reading. */
protected array $formatters = [];

public static function createFromPath(string $path, string $open_mode = 'r', $context = null): static
{
return parent::createFromPath($path, $open_mode, $context);
}

/**
* Adds a record formatter.
*/
public function addFormatter(callable $formatter): self
{
$this->formatters[] = $formatter;

return $this;
}

/**
* Selects the record to be used as the CSV header.
*
Expand Down Expand Up @@ -610,7 +598,7 @@ protected function combineHeader(Iterator $iterator, array $header): Iterator
{
$formatter = fn (array $record): array => array_reduce(
$this->formatters,
fn (array $record, callable $formatter): array => $formatter($record),
fn (array $record, Closure $formatter): array => $formatter($record),
$record
);

Expand Down
14 changes: 0 additions & 14 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ class Writer extends AbstractCsv implements TabularDataWriter
protected const ENCLOSE_NONE = -1;

protected const STREAM_FILTER_MODE = STREAM_FILTER_WRITE;
/** @var array<Closure(array): array> callable collection to format the record before insertion. */
protected array $formatters = [];
/** @var array<Closure(array): bool> callable collection to validate the record before insertion. */
protected array $validators = [];
protected string $newline = "\n";
Expand Down Expand Up @@ -176,18 +174,6 @@ protected function validateRecord(array $record): void
}
}

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

return $this;
}

/**
* Adds a record validator.
*
Expand Down

0 comments on commit 41d08de

Please sign in to comment.