réorganiser le code
This commit is contained in:
parent
0c87ff1a7b
commit
2fb49a110e
@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace nulib\ext\spout;
|
||||
|
||||
use DateTimeInterface;
|
||||
use nulib\cl;
|
||||
use nulib\file\tab\AbstractBuilder;
|
||||
use nulib\file\tab\TAbstractBuilder;
|
||||
@ -24,36 +23,6 @@ use OpenSpout\Writer\XLSX\Entity\SheetView;
|
||||
class SpoutBuilder extends AbstractBuilder {
|
||||
use TAbstractBuilder;
|
||||
|
||||
const DATE_FORMAT = "dd/mm/yyyy";
|
||||
|
||||
const DATETIME_FORMAT = "dd/mm/yyyy hh:mm:ss";
|
||||
|
||||
/** @var bool faut-il choisir le type numérique pour une chaine numérique? */
|
||||
const TYPE_NUMERIC = true;
|
||||
|
||||
/** @var bool faut-il choisir le type date pour une chaine au bon format? */
|
||||
const TYPE_DATE = true;
|
||||
|
||||
/** @var array configuration du writer */
|
||||
const WRITER_PARAMS = null;
|
||||
|
||||
/** @var array options du writer */
|
||||
const WRITER_OPTIONS_PARAMS = null;
|
||||
|
||||
/** @var array configuration de la première feuille */
|
||||
const SHEET_PARAMS = null;
|
||||
|
||||
/** @var array configuration de la vue de la première feuille */
|
||||
const SHEET_VIEW_PARAMS = null;
|
||||
|
||||
/**
|
||||
* @var string|int|null nom de la feuille dans laquelle écrire.
|
||||
*
|
||||
* spécifier cette valeur est équivalent à spécifier la clé "->setName" de
|
||||
* SHEET_PARAMS
|
||||
*/
|
||||
const WSNAME = null;
|
||||
|
||||
protected static function apply_params($object, ?array $params, array $refParams) {
|
||||
foreach (array_keys($refParams) as $method) {
|
||||
if (!str::starts_with("->", $method)) continue;
|
||||
@ -151,54 +120,89 @@ class SpoutBuilder extends AbstractBuilder {
|
||||
return $style;
|
||||
}
|
||||
|
||||
const DATE_FORMAT = "dd/mm/yyyy";
|
||||
|
||||
const DATETIME_FORMAT = "dd/mm/yyyy hh:mm:ss";
|
||||
|
||||
/** @var bool faut-il choisir le type numérique pour une chaine numérique? */
|
||||
const TYPE_NUMERIC = true;
|
||||
|
||||
/** @var bool faut-il choisir le type date pour une chaine au bon format? */
|
||||
const TYPE_DATE = true;
|
||||
|
||||
/** @var array configuration du writer */
|
||||
const WRITER_PARAMS = null;
|
||||
|
||||
/** @var array configuration de la première feuille */
|
||||
const SHEET_PARAMS = null;
|
||||
|
||||
/** @var string nom de la première feuille */
|
||||
const SHEET_NAME = null;
|
||||
|
||||
/** @var array configuration de la vue de la première feuille */
|
||||
const SHEET_VIEW_PARAMS = null;
|
||||
|
||||
function __construct(?string $output, ?array $params=null) {
|
||||
parent::__construct($output, $params);
|
||||
$ssType = $params["ss_type"] ?? null;
|
||||
if ($ssType === null) {
|
||||
$writerType = $params["ss_type"] ?? null;
|
||||
if ($writerType === null) {
|
||||
switch (path::ext($this->output)) {
|
||||
case ".ods":
|
||||
$ssType = self::SS_TYPE_ODS;
|
||||
$writerType = self::WRITER_TYPE_ODS;
|
||||
break;
|
||||
case ".xlsx":
|
||||
default:
|
||||
$ssType = self::SS_TYPE_XLSX;
|
||||
$writerType = self::WRITER_TYPE_XLSX;
|
||||
break;
|
||||
}
|
||||
}
|
||||
switch ($ssType) {
|
||||
|
||||
$writerParams = $params["spout"] ?? static::WRITER_PARAMS;
|
||||
$writerParams["->setDefaultColumnWidth"] ??= 10.5;
|
||||
switch ($writerType) {
|
||||
case "ods":
|
||||
case self::SS_TYPE_ODS:
|
||||
$ss = WriterEntityFactory::createODSWriter();
|
||||
$ssType = self::SS_TYPE_ODS;
|
||||
case self::WRITER_TYPE_ODS:
|
||||
$writerType = self::WRITER_TYPE_ODS;
|
||||
$writer = WriterEntityFactory::createODSWriter();
|
||||
self::apply_params($writer, $writerParams, ref_params_ods::WRITER);
|
||||
break;
|
||||
case "xlsx":
|
||||
case self::SS_TYPE_XLSX:
|
||||
case self::WRITER_TYPE_XLSX:
|
||||
default:
|
||||
$ss = WriterEntityFactory::createXLSXWriter();
|
||||
$ssType = self::SS_TYPE_XLSX;
|
||||
$writerType = self::WRITER_TYPE_XLSX;
|
||||
$writer = WriterEntityFactory::createXLSXWriter();
|
||||
self::apply_params($writer, $writerParams, ref_params_xlsx::WRITER);
|
||||
break;
|
||||
}
|
||||
$ss->setDefaultColumnWidth(10.5);
|
||||
$ss->writeToStream($this->getResource());
|
||||
$this->ssType = $ssType;
|
||||
$this->ss = $ss;
|
||||
self::ensure_style($writerParams["default_row_style"]);
|
||||
$defaultRowStyle = $writerParams["default_row_style"];
|
||||
if ($defaultRowStyle !== null) $writer->setDefaultRowStyle($defaultRowStyle);
|
||||
$writer->writeToStream($this->getResource());
|
||||
|
||||
$this->writerType = $writerType;
|
||||
$this->writer = $writer;
|
||||
$this->writerParams = $writerParams;
|
||||
$this->typeNumeric = boolval($params["type_numeric"] ?? static::TYPE_NUMERIC);
|
||||
$this->typeDate = boolval($params["type_date"] ?? static::TYPE_DATE);
|
||||
$this->firstSheet = true;
|
||||
|
||||
$sheetParams = $params["sheet"] ?? static::SHEET_PARAMS;
|
||||
$sheetName = $params["sheet_name"] ?? static::SHEET_NAME;
|
||||
if ($sheetParams !== null) $sheetParams["name"] = $sheetName;
|
||||
$sheetViewParams = $params["sheet_view"] ?? static::SHEET_VIEW_PARAMS;
|
||||
if ($sheetViewParams !== null) $sheetParams["view"] = $sheetViewParams;
|
||||
$this->firstSheet = true;
|
||||
$this->sheetParams = null;
|
||||
$this->setSheet($params["wsname"] ?? static::WSNAME, $sheetParams);
|
||||
$this->setSheet(null, $sheetParams);
|
||||
}
|
||||
|
||||
const SS_TYPE_ODS = 1, SS_TYPE_XLSX = 2;
|
||||
const WRITER_TYPE_ODS = 1, WRITER_TYPE_XLSX = 2;
|
||||
|
||||
/** @var int type de fichier généré */
|
||||
protected int $ssType;
|
||||
protected int $writerType;
|
||||
|
||||
protected WriterMultiSheetsAbstract $ss;
|
||||
protected WriterMultiSheetsAbstract $writer;
|
||||
|
||||
protected ?array $writerParams;
|
||||
|
||||
protected bool $typeNumeric;
|
||||
|
||||
@ -240,42 +244,41 @@ class SpoutBuilder extends AbstractBuilder {
|
||||
function setSheet($sheetName, ?array $sheetParams=null): self {
|
||||
if ($sheetName !== null) $sheetParams["->setName"] = $sheetName;
|
||||
|
||||
$ss = $this->ss;
|
||||
$writer = $this->writer;
|
||||
if ($this->firstSheet) {
|
||||
$this->firstSheet = false;
|
||||
$ws = $ss->getCurrentSheet();
|
||||
$sheet = $writer->getCurrentSheet();
|
||||
} else {
|
||||
$ws = $ss->addNewSheetAndMakeItCurrent();
|
||||
$sheet = $writer->addNewSheetAndMakeItCurrent();
|
||||
$this->wroteHeaders = false;
|
||||
$this->built = false;
|
||||
}
|
||||
$this->rowStyle = self::STYLE_ROW;
|
||||
|
||||
switch ($this->ssType) {
|
||||
case self::SS_TYPE_ODS:
|
||||
switch ($this->writerType) {
|
||||
case self::WRITER_TYPE_ODS:
|
||||
# appliquer les paramètres de la feuille
|
||||
$this->apply_params($ss, $sheetParams, ref_params_ods::SHEET);
|
||||
$this->apply_params($sheet, $sheetParams, ref_params_ods::SHEET);
|
||||
break;
|
||||
case self::SS_TYPE_XLSX:
|
||||
case self::WRITER_TYPE_XLSX:
|
||||
# appliquer les paramètres de la feuille
|
||||
$this->apply_params($ss, $sheetParams, ref_params_xlsx::SHEET);
|
||||
$this->ensure_style($sheetParams["default_style"]);
|
||||
$this->ensure_style($sheetParams["header_style"]);
|
||||
$this->ensure_style($sheetParams["odd_style"]);
|
||||
$this->ensure_style($sheetParams["even_style"]);
|
||||
self::set_defaults($sheetParams, "header_style", [
|
||||
"font" => ["bold" => true],
|
||||
"bg_color" => "gray",
|
||||
]);
|
||||
self::set_defaults($sheetParams, "even_style", [
|
||||
"bg_color" => "light_gray",
|
||||
]);
|
||||
$this->apply_params($sheet, $sheetParams, ref_params_xlsx::SHEET);
|
||||
# appliquer les paramètres de la vue de la feuille
|
||||
$sheetViewParams =& $sheetParams["view"];
|
||||
$sheetViewParams["->setFreezeRow"] ??= 2;
|
||||
$ws->setSheetView($this->apply_params(new SheetView(), $sheetViewParams, ref_params_xlsx::SHEET_VIEW));
|
||||
$sheet->setSheetView(self::apply_params(new SheetView(), $sheetViewParams, ref_params_xlsx::SHEET_VIEW));
|
||||
break;
|
||||
}
|
||||
self::set_defaults($sheetParams, "header_style", [
|
||||
"font" => ["bold" => true],
|
||||
"bg_color" => "gray",
|
||||
]);
|
||||
self::set_defaults($sheetParams, "even_style", [
|
||||
"bg_color" => "light_gray",
|
||||
]);
|
||||
$this->ensure_style($sheetParams["header_style"]);
|
||||
$this->ensure_style($sheetParams["odd_style"]);
|
||||
$this->ensure_style($sheetParams["even_style"]);
|
||||
$this->sheetParams = $sheetParams;
|
||||
|
||||
if ($sheetParams !== null) {
|
||||
@ -372,7 +375,7 @@ class SpoutBuilder extends AbstractBuilder {
|
||||
}
|
||||
$rowStyle ??= $oddStyle;
|
||||
self::ensure_style($rowStyle);
|
||||
$this->ss->addRow(WriterEntityFactory::createRow($cells, $rowStyle));
|
||||
$this->writer->addRow(WriterEntityFactory::createRow($cells, $rowStyle));
|
||||
if ($differentOddEven) $this->oddEvenIndex++;
|
||||
}
|
||||
|
||||
@ -396,7 +399,7 @@ class SpoutBuilder extends AbstractBuilder {
|
||||
}
|
||||
|
||||
protected function _checkOk(): bool {
|
||||
$this->ss->close();
|
||||
$this->writer->close();
|
||||
$this->rewind();
|
||||
return true;
|
||||
}
|
||||
|
@ -3,20 +3,19 @@ namespace nulib\ext\spout;
|
||||
|
||||
class ref_params_ods extends ref_params {
|
||||
const READER = [
|
||||
"options" => self::READER_OPTIONS,
|
||||
];
|
||||
|
||||
const READER_OPTIONS = [];
|
||||
|
||||
const WRITER = [
|
||||
"options" => self::WRITER_OPTIONS,
|
||||
"default_style" => self::STYLE,
|
||||
"->setDefaultColumnWidth" => ["float"],
|
||||
"->setDefaultRowHeight" => ["float"],
|
||||
"->setColumnWidth" => ["float", ["int", null]],
|
||||
"->setColumnWidthForRange" => ["int", "int", "int"],
|
||||
"default_row_style" => self::STYLE,
|
||||
];
|
||||
|
||||
const WRITER_OPTIONS = [];
|
||||
|
||||
const SHEET = [
|
||||
"->setName" => ["string"],
|
||||
"-setIsVisible" => ["bool"],
|
||||
"header_style" => self::STYLE,
|
||||
"odd_style" => self::STYLE,
|
||||
"even_style" => self::STYLE,
|
||||
|
@ -3,52 +3,19 @@ namespace nulib\ext\spout;
|
||||
|
||||
class ref_params_xlsx extends ref_params {
|
||||
const READER = [
|
||||
"options" => self::READER_OPTIONS,
|
||||
];
|
||||
|
||||
const READER_OPTIONS = [
|
||||
"SHOULD_FORMAT_DATES" => "bool",
|
||||
"SHOULD_PRESERVE_EMPTY_ROWS" => "bool",
|
||||
];
|
||||
|
||||
const WRITER = [
|
||||
"->setCreator" => ["string"],
|
||||
"->setColumnWidth" => ["int", "int"],
|
||||
"->setDefaultColumnWidth" => ["float"],
|
||||
"->setDefaultRowHeight" => ["float"],
|
||||
"->setColumnWidth" => ["float", ["int", null]],
|
||||
"->setColumnWidthForRange" => ["int", "int", "int"],
|
||||
"options" => self::WRITER_OPTIONS,
|
||||
"default_style" => self::STYLE,
|
||||
];
|
||||
|
||||
const WRITER_OPTIONS = [
|
||||
"SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY " => "bool",
|
||||
"SHOULD_USE_INLINE_STRINGS" => "bool",
|
||||
"->setPageSetup" => [
|
||||
"page_orientation" => "string",
|
||||
"page_size" => "string",
|
||||
"fit_to_height" => "bool",
|
||||
"fit_to_width" => "bool",
|
||||
],
|
||||
"->setPageMargin" => [
|
||||
"top" => "float",
|
||||
"right" => "float",
|
||||
"bottom" => "float",
|
||||
"left" => "float",
|
||||
"header" => "float",
|
||||
"footer" => "float",
|
||||
],
|
||||
"->setHeaderFooter" => [
|
||||
"odd_header" => "string",
|
||||
"odd_footer" => "string",
|
||||
"even_header" => "string",
|
||||
"even_footer" => "string",
|
||||
"different_odd_even" => "bool",
|
||||
],
|
||||
"default_row_style" => self::STYLE,
|
||||
];
|
||||
|
||||
const SHEET = [
|
||||
"->setName" => ["string"],
|
||||
"->setColumnWidth" => ["int", "int"],
|
||||
"->setColumnWidthForRange" => ["int", "int", "int"],
|
||||
"-setIsVisible" => ["bool"],
|
||||
"view" => self::SHEET_VIEW,
|
||||
"header_style" => self::STYLE,
|
||||
"odd_style" => self::STYLE,
|
||||
|
Loading…
Reference in New Issue
Block a user