This commit is contained in:
Jephté Clain 2024-10-04 14:18:31 +04:00
commit c88277a6d6
5 changed files with 33 additions and 26 deletions

View File

@ -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 ($this->useHeaders) {
if ($headers !== null) $this->headers = $headers;
else $this->ensureHeaders();
if ($this->headers !== null) $this->_write($this->headers);
}
$this->wroteHeaders = true;
}

View File

@ -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;

View File

@ -1,7 +1,7 @@
<?php
namespace nur\sery\file\csv;
interface IBuilder {
interface IBuilder extends \nur\sery\file\IReader {
function writeHeaders(?array $headers=null): void;
function write(?array $row): void;

View File

@ -37,17 +37,15 @@ trait TAbstractBuilder {
$output = $params["output"] ?? null;
$ssType = null;
if (is_string($output)) {
switch (path::ext($output)) {
case ".csv":
$ext = path::ext($output);
if ($output === "-" || $ext === ".csv") {
$class = CsvBuilder::class;
break;
case ".ods":
} elseif ($ext === ".ods") {
$ssType = "ods";
break;
case ".xlsx":
default:
} elseif ($ext === ".xlsx") {
$ssType = "xlsx";
} else {
$ssType = "xlsx";
break;
}
}
$params["ss_type"] = $ssType;

View File

@ -36,17 +36,15 @@ trait TAbstractReader {
$input = $params["input"] ?? null;
$ssType = null;
if (is_string($input)) {
switch (path::ext($input)) {
case ".csv":
$ext = path::ext($input);
if ($input === "-" || $ext === ".csv") {
$class = CsvReader::class;
break;
case ".ods":
} elseif ($ext === ".ods") {
$ssType = "ods";
break;
case ".xlsx":
default:
} elseif ($ext === ".xlsx") {
$ssType = "xlsx";
} else {
$ssType = "xlsx";
break;
}
}
$params["ss_type"] = $ssType;