From 3bb18450fde42e2f6b631102dbcaacbc7d826d26 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 3 Oct 2024 10:35:25 +0400 Subject: [PATCH 1/2] modifs.mineures sans commentaires --- src/file/csv/AbstractReader.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/file/csv/AbstractReader.php b/src/file/csv/AbstractReader.php index 2c80209..1929c2d 100644 --- a/src/file/csv/AbstractReader.php +++ b/src/file/csv/AbstractReader.php @@ -11,7 +11,7 @@ abstract class AbstractReader implements IReader { const HEADERS = null; /** @var bool faut-il utiliser les en-têtes pour retourner des tableaux associatifs? */ - const COOk_ROWS = true; + const USE_HEADERS = true; /** @var ?string nom du fichier depuis lequel lire */ const INPUT = null; @@ -40,7 +40,7 @@ abstract class AbstractReader implements IReader { # $this->schema = $params["schema"] ?? static::SCHEMA; $this->headers = $params["headers"] ?? static::HEADERS; - $this->cookRows = $params["cook_rows"] ?? static::COOk_ROWS; + $this->useHeaders = $params["use_headers"] ?? static::USE_HEADERS; $this->input = $params["input"] ?? static::INPUT; $this->trim = boolval($params["trim"] ?? static::TRIM); $this->parseEmptyAsNull = boolval($params["empty_as_null"] ?? static::PARSE_EMPTY_AS_NULL); @@ -52,7 +52,7 @@ abstract class AbstractReader implements IReader { protected ?array $headers; - protected bool $cookRows; + protected bool $useHeaders; protected $input; @@ -67,7 +67,7 @@ abstract class AbstractReader implements IReader { protected int $isrc = 0, $idest = 0; protected function cookRow(array &$row): bool { - if (!$this->cookRows) return true; + if (!$this->useHeaders) return true; if ($this->isrc == 0) { # ligne d'en-tête $headers = $this->headers; From b469031de1b3872287495313e798171bf0d23e49 Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Thu, 3 Oct 2024 11:07:37 +0400 Subject: [PATCH 2/2] modifs.mineures sans commentaires --- src/file/csv/AbstractBuilder.php | 19 +++++++++++++++---- src/file/csv/AbstractReader.php | 2 +- src/file/csv/IBuilder.php | 2 +- src/file/csv/TAbstractBuilder.php | 14 ++++++-------- src/file/csv/TAbstractReader.php | 14 ++++++-------- 5 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/file/csv/AbstractBuilder.php b/src/file/csv/AbstractBuilder.php index 1ea4045..6336d36 100644 --- a/src/file/csv/AbstractBuilder.php +++ b/src/file/csv/AbstractBuilder.php @@ -16,6 +16,12 @@ abstract class AbstractBuilder extends TempStream implements IBuilder { /** @var ?array liste des colonnes à écrire */ const HEADERS = null; + /** + * @var bool faut-il écrire les en-têtes, soit celles qui sont spécifiées, + * soit celles qui sont calculées à partir des clés de la première ligne + */ + const USE_HEADERS = true; + /** @var ?string nom du fichier téléchargé */ const OUTPUT = null; @@ -23,6 +29,7 @@ abstract class AbstractBuilder extends TempStream implements IBuilder { if ($output !== null) $params["output"] = $output; $this->schema = $params["schema"] ?? static::SCHEMA; $this->headers = $params["headers"] ?? static::HEADERS; + $this->useHeaders = $params["use_headers"] ?? static::USE_HEADERS; $rows = $params["rows"] ?? null; if (is_callable($rows)) $rows = $rows(); $this->rows = $rows; @@ -44,6 +51,8 @@ abstract class AbstractBuilder extends TempStream implements IBuilder { protected ?array $headers; + protected bool $useHeaders; + protected ?iterable $rows; protected ?string $output; @@ -53,7 +62,7 @@ abstract class AbstractBuilder extends TempStream implements IBuilder { protected ?array $cookArgs; protected function ensureHeaders(?array $row=null): void { - if ($this->headers !== null) return; + if ($this->headers !== null || !$this->useHeaders) return; if ($this->schema === null) $headers = null; else $headers = array_keys($this->schema); if ($headers === null && $row !== null) $headers = array_keys($row); @@ -66,9 +75,11 @@ abstract class AbstractBuilder extends TempStream implements IBuilder { function writeHeaders(?array $headers=null): void { if ($this->wroteHeaders) return; - if ($headers !== null) $this->headers = $headers; - else $this->ensureHeaders(); - if ($this->headers !== null) $this->_write($this->headers); + if ($this->useHeaders) { + if ($headers !== null) $this->headers = $headers; + else $this->ensureHeaders(); + if ($this->headers !== null) $this->_write($this->headers); + } $this->wroteHeaders = true; } diff --git a/src/file/csv/AbstractReader.php b/src/file/csv/AbstractReader.php index 1929c2d..4351169 100644 --- a/src/file/csv/AbstractReader.php +++ b/src/file/csv/AbstractReader.php @@ -5,7 +5,7 @@ use nur\sery\A; use nur\sery\php\time\Date; use nur\sery\php\time\DateTime; -abstract class AbstractReader implements IReader { +abstract class AbstractReader implements IReader { const SCHEMA = null; const HEADERS = null; diff --git a/src/file/csv/IBuilder.php b/src/file/csv/IBuilder.php index 393a724..1c98c3c 100644 --- a/src/file/csv/IBuilder.php +++ b/src/file/csv/IBuilder.php @@ -1,7 +1,7 @@