réorganiser le code
This commit is contained in:
parent
0c87ff1a7b
commit
2fb49a110e
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace nulib\ext\spout;
|
namespace nulib\ext\spout;
|
||||||
|
|
||||||
use DateTimeInterface;
|
|
||||||
use nulib\cl;
|
use nulib\cl;
|
||||||
use nulib\file\tab\AbstractBuilder;
|
use nulib\file\tab\AbstractBuilder;
|
||||||
use nulib\file\tab\TAbstractBuilder;
|
use nulib\file\tab\TAbstractBuilder;
|
||||||
@ -24,36 +23,6 @@ use OpenSpout\Writer\XLSX\Entity\SheetView;
|
|||||||
class SpoutBuilder extends AbstractBuilder {
|
class SpoutBuilder extends AbstractBuilder {
|
||||||
use TAbstractBuilder;
|
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) {
|
protected static function apply_params($object, ?array $params, array $refParams) {
|
||||||
foreach (array_keys($refParams) as $method) {
|
foreach (array_keys($refParams) as $method) {
|
||||||
if (!str::starts_with("->", $method)) continue;
|
if (!str::starts_with("->", $method)) continue;
|
||||||
@ -151,54 +120,89 @@ class SpoutBuilder extends AbstractBuilder {
|
|||||||
return $style;
|
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) {
|
function __construct(?string $output, ?array $params=null) {
|
||||||
parent::__construct($output, $params);
|
parent::__construct($output, $params);
|
||||||
$ssType = $params["ss_type"] ?? null;
|
$writerType = $params["ss_type"] ?? null;
|
||||||
if ($ssType === null) {
|
if ($writerType === null) {
|
||||||
switch (path::ext($this->output)) {
|
switch (path::ext($this->output)) {
|
||||||
case ".ods":
|
case ".ods":
|
||||||
$ssType = self::SS_TYPE_ODS;
|
$writerType = self::WRITER_TYPE_ODS;
|
||||||
break;
|
break;
|
||||||
case ".xlsx":
|
case ".xlsx":
|
||||||
default:
|
default:
|
||||||
$ssType = self::SS_TYPE_XLSX;
|
$writerType = self::WRITER_TYPE_XLSX;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch ($ssType) {
|
|
||||||
|
$writerParams = $params["spout"] ?? static::WRITER_PARAMS;
|
||||||
|
$writerParams["->setDefaultColumnWidth"] ??= 10.5;
|
||||||
|
switch ($writerType) {
|
||||||
case "ods":
|
case "ods":
|
||||||
case self::SS_TYPE_ODS:
|
case self::WRITER_TYPE_ODS:
|
||||||
$ss = WriterEntityFactory::createODSWriter();
|
$writerType = self::WRITER_TYPE_ODS;
|
||||||
$ssType = self::SS_TYPE_ODS;
|
$writer = WriterEntityFactory::createODSWriter();
|
||||||
|
self::apply_params($writer, $writerParams, ref_params_ods::WRITER);
|
||||||
break;
|
break;
|
||||||
case "xlsx":
|
case "xlsx":
|
||||||
case self::SS_TYPE_XLSX:
|
case self::WRITER_TYPE_XLSX:
|
||||||
default:
|
default:
|
||||||
$ss = WriterEntityFactory::createXLSXWriter();
|
$writerType = self::WRITER_TYPE_XLSX;
|
||||||
$ssType = self::SS_TYPE_XLSX;
|
$writer = WriterEntityFactory::createXLSXWriter();
|
||||||
|
self::apply_params($writer, $writerParams, ref_params_xlsx::WRITER);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$ss->setDefaultColumnWidth(10.5);
|
self::ensure_style($writerParams["default_row_style"]);
|
||||||
$ss->writeToStream($this->getResource());
|
$defaultRowStyle = $writerParams["default_row_style"];
|
||||||
$this->ssType = $ssType;
|
if ($defaultRowStyle !== null) $writer->setDefaultRowStyle($defaultRowStyle);
|
||||||
$this->ss = $ss;
|
$writer->writeToStream($this->getResource());
|
||||||
|
|
||||||
|
$this->writerType = $writerType;
|
||||||
|
$this->writer = $writer;
|
||||||
|
$this->writerParams = $writerParams;
|
||||||
$this->typeNumeric = boolval($params["type_numeric"] ?? static::TYPE_NUMERIC);
|
$this->typeNumeric = boolval($params["type_numeric"] ?? static::TYPE_NUMERIC);
|
||||||
$this->typeDate = boolval($params["type_date"] ?? static::TYPE_DATE);
|
$this->typeDate = boolval($params["type_date"] ?? static::TYPE_DATE);
|
||||||
$this->firstSheet = true;
|
|
||||||
|
|
||||||
$sheetParams = $params["sheet"] ?? static::SHEET_PARAMS;
|
$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;
|
$sheetViewParams = $params["sheet_view"] ?? static::SHEET_VIEW_PARAMS;
|
||||||
if ($sheetViewParams !== null) $sheetParams["view"] = $sheetViewParams;
|
if ($sheetViewParams !== null) $sheetParams["view"] = $sheetViewParams;
|
||||||
|
$this->firstSheet = true;
|
||||||
$this->sheetParams = null;
|
$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é */
|
/** @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;
|
protected bool $typeNumeric;
|
||||||
|
|
||||||
@ -240,42 +244,41 @@ class SpoutBuilder extends AbstractBuilder {
|
|||||||
function setSheet($sheetName, ?array $sheetParams=null): self {
|
function setSheet($sheetName, ?array $sheetParams=null): self {
|
||||||
if ($sheetName !== null) $sheetParams["->setName"] = $sheetName;
|
if ($sheetName !== null) $sheetParams["->setName"] = $sheetName;
|
||||||
|
|
||||||
$ss = $this->ss;
|
$writer = $this->writer;
|
||||||
if ($this->firstSheet) {
|
if ($this->firstSheet) {
|
||||||
$this->firstSheet = false;
|
$this->firstSheet = false;
|
||||||
$ws = $ss->getCurrentSheet();
|
$sheet = $writer->getCurrentSheet();
|
||||||
} else {
|
} else {
|
||||||
$ws = $ss->addNewSheetAndMakeItCurrent();
|
$sheet = $writer->addNewSheetAndMakeItCurrent();
|
||||||
$this->wroteHeaders = false;
|
$this->wroteHeaders = false;
|
||||||
$this->built = false;
|
$this->built = false;
|
||||||
}
|
}
|
||||||
$this->rowStyle = self::STYLE_ROW;
|
$this->rowStyle = self::STYLE_ROW;
|
||||||
|
|
||||||
switch ($this->ssType) {
|
switch ($this->writerType) {
|
||||||
case self::SS_TYPE_ODS:
|
case self::WRITER_TYPE_ODS:
|
||||||
# appliquer les paramètres de la feuille
|
# 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;
|
break;
|
||||||
case self::SS_TYPE_XLSX:
|
case self::WRITER_TYPE_XLSX:
|
||||||
# appliquer les paramètres de la feuille
|
# appliquer les paramètres de la feuille
|
||||||
$this->apply_params($ss, $sheetParams, ref_params_xlsx::SHEET);
|
$this->apply_params($sheet, $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",
|
|
||||||
]);
|
|
||||||
# appliquer les paramètres de la vue de la feuille
|
# appliquer les paramètres de la vue de la feuille
|
||||||
$sheetViewParams =& $sheetParams["view"];
|
$sheetViewParams =& $sheetParams["view"];
|
||||||
$sheetViewParams["->setFreezeRow"] ??= 2;
|
$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;
|
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;
|
$this->sheetParams = $sheetParams;
|
||||||
|
|
||||||
if ($sheetParams !== null) {
|
if ($sheetParams !== null) {
|
||||||
@ -372,7 +375,7 @@ class SpoutBuilder extends AbstractBuilder {
|
|||||||
}
|
}
|
||||||
$rowStyle ??= $oddStyle;
|
$rowStyle ??= $oddStyle;
|
||||||
self::ensure_style($rowStyle);
|
self::ensure_style($rowStyle);
|
||||||
$this->ss->addRow(WriterEntityFactory::createRow($cells, $rowStyle));
|
$this->writer->addRow(WriterEntityFactory::createRow($cells, $rowStyle));
|
||||||
if ($differentOddEven) $this->oddEvenIndex++;
|
if ($differentOddEven) $this->oddEvenIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -396,7 +399,7 @@ class SpoutBuilder extends AbstractBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected function _checkOk(): bool {
|
protected function _checkOk(): bool {
|
||||||
$this->ss->close();
|
$this->writer->close();
|
||||||
$this->rewind();
|
$this->rewind();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -3,20 +3,19 @@ namespace nulib\ext\spout;
|
|||||||
|
|
||||||
class ref_params_ods extends ref_params {
|
class ref_params_ods extends ref_params {
|
||||||
const READER = [
|
const READER = [
|
||||||
"options" => self::READER_OPTIONS,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const READER_OPTIONS = [];
|
|
||||||
|
|
||||||
const WRITER = [
|
const WRITER = [
|
||||||
"options" => self::WRITER_OPTIONS,
|
"->setDefaultColumnWidth" => ["float"],
|
||||||
"default_style" => self::STYLE,
|
"->setDefaultRowHeight" => ["float"],
|
||||||
|
"->setColumnWidth" => ["float", ["int", null]],
|
||||||
|
"->setColumnWidthForRange" => ["int", "int", "int"],
|
||||||
|
"default_row_style" => self::STYLE,
|
||||||
];
|
];
|
||||||
|
|
||||||
const WRITER_OPTIONS = [];
|
|
||||||
|
|
||||||
const SHEET = [
|
const SHEET = [
|
||||||
"->setName" => ["string"],
|
"->setName" => ["string"],
|
||||||
|
"-setIsVisible" => ["bool"],
|
||||||
"header_style" => self::STYLE,
|
"header_style" => self::STYLE,
|
||||||
"odd_style" => self::STYLE,
|
"odd_style" => self::STYLE,
|
||||||
"even_style" => self::STYLE,
|
"even_style" => self::STYLE,
|
||||||
|
@ -3,52 +3,19 @@ namespace nulib\ext\spout;
|
|||||||
|
|
||||||
class ref_params_xlsx extends ref_params {
|
class ref_params_xlsx extends ref_params {
|
||||||
const READER = [
|
const READER = [
|
||||||
"options" => self::READER_OPTIONS,
|
|
||||||
];
|
|
||||||
|
|
||||||
const READER_OPTIONS = [
|
|
||||||
"SHOULD_FORMAT_DATES" => "bool",
|
|
||||||
"SHOULD_PRESERVE_EMPTY_ROWS" => "bool",
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const WRITER = [
|
const WRITER = [
|
||||||
"->setCreator" => ["string"],
|
"->setDefaultColumnWidth" => ["float"],
|
||||||
"->setColumnWidth" => ["int", "int"],
|
"->setDefaultRowHeight" => ["float"],
|
||||||
|
"->setColumnWidth" => ["float", ["int", null]],
|
||||||
"->setColumnWidthForRange" => ["int", "int", "int"],
|
"->setColumnWidthForRange" => ["int", "int", "int"],
|
||||||
"options" => self::WRITER_OPTIONS,
|
"default_row_style" => self::STYLE,
|
||||||
"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",
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
const SHEET = [
|
const SHEET = [
|
||||||
"->setName" => ["string"],
|
"->setName" => ["string"],
|
||||||
"->setColumnWidth" => ["int", "int"],
|
"-setIsVisible" => ["bool"],
|
||||||
"->setColumnWidthForRange" => ["int", "int", "int"],
|
|
||||||
"view" => self::SHEET_VIEW,
|
"view" => self::SHEET_VIEW,
|
||||||
"header_style" => self::STYLE,
|
"header_style" => self::STYLE,
|
||||||
"odd_style" => self::STYLE,
|
"odd_style" => self::STYLE,
|
||||||
|
Loading…
Reference in New Issue
Block a user