modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-10-03 11:07:37 +04:00
parent 3bb18450fd
commit b469031de1
5 changed files with 29 additions and 22 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 ($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;
}

View File

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

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;