modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-10 20:45:38 +04:00
parent 6bb82ec967
commit 94324c5622
5 changed files with 54 additions and 51 deletions

View File

@ -8,6 +8,9 @@
<PhpSpecSuiteConfiguration> <PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" /> <option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration> </PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
</suites> </suites>
</component> </component>
</project> </project>

View File

@ -6,10 +6,10 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class wsutils { class wsutils {
static function get_highest_coords(Worksheet $ws): array { static function get_highest_coords(Worksheet $ws): array {
$highestRow = $ws->getHighestRow();
$highestColumnA = $ws->getHighestColumn(); $highestColumnA = $ws->getHighestColumn();
$highestCol = Coordinate::columnIndexFromString($highestColumnA); $highestCol = Coordinate::columnIndexFromString($highestColumnA);
return [$highestRow, $highestCol]; $highestRow = $ws->getHighestRow();
return [$highestCol, $highestRow];
} }
/** /**
@ -24,18 +24,18 @@ class wsutils {
const MAX_EMPTY_THRESHOLD = 150; const MAX_EMPTY_THRESHOLD = 150;
static function compute_max_coords(Worksheet $ws): array { static function compute_max_coords(Worksheet $ws): array {
[$highestRow, $highestCol] = self::get_highest_coords($ws); [$highestCol, $highestRow] = self::get_highest_coords($ws);
$maxRow = 1;
$maxCol = 1; $maxCol = 1;
$maxRow = 1;
$maxEmptyRows = self::MAX_EMPTY_THRESHOLD; $maxEmptyRows = self::MAX_EMPTY_THRESHOLD;
for ($row = 1; $row <= $highestRow; $row++) { for ($row = 1; $row <= $highestRow; $row++) {
$emptyRow = true; $emptyRow = true;
$maxEmptyCols = self::MAX_EMPTY_THRESHOLD; $maxEmptyCols = self::MAX_EMPTY_THRESHOLD;
for ($col = 1; $col <= $highestCol; $col++) { for ($col = 1; $col <= $highestCol; $col++) {
$value = null; $value = null;
if ($ws->cellExists([$col, $row])) { if ($ws->cellExistsByColumnAndRow($col, $row)) {
$value = $ws->getCell([$col, $row])->getValue(); $value = $ws->getCellByColumnAndRow($col, $row)->getValue();
} }
if ($value === null) { if ($value === null) {
$maxEmptyCols--; $maxEmptyCols--;
@ -54,6 +54,6 @@ class wsutils {
$maxEmptyRows = self::MAX_EMPTY_THRESHOLD; $maxEmptyRows = self::MAX_EMPTY_THRESHOLD;
} }
} }
return [$maxRow, $maxCol]; return [$maxCol, $maxRow];
} }
} }

View File

@ -8,12 +8,7 @@ use nur\sery\web\http;
* Class CsvBuilder: construction d'un fichier CSV, pour envoi à l'utilisateur * Class CsvBuilder: construction d'un fichier CSV, pour envoi à l'utilisateur
*/ */
class CsvBuilder extends AbstractBuilder { class CsvBuilder extends AbstractBuilder {
static function with($builder): self { use TAbstractBuilder;
if ($builder instanceof self) return $builder;
elseif (is_string($builder)) return new static($builder);
elseif (is_array($builder)) return new static(null, $builder);
else throw ValueException::invalid_type($builder, self::class);
}
function __construct(?string $output, ?array $params=null) { function __construct(?string $output, ?array $params=null) {
$csvFlavour = $params["csv_flavour"] ?? null; $csvFlavour = $params["csv_flavour"] ?? null;

View File

@ -1,6 +1,7 @@
<?php <?php
namespace nur\sery\file\csv; namespace nur\sery\file\csv;
use nur\sery\ext\spreadsheet\wsutils;
use nur\sery\os\path; use nur\sery\os\path;
use nur\sery\ValueException; use nur\sery\ValueException;
use nur\sery\web\http; use nur\sery\web\http;
@ -16,73 +17,64 @@ use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
* Class SpreadsheetBuilder: construction d'une feuille de calcul, pour envoi * Class SpreadsheetBuilder: construction d'une feuille de calcul, pour envoi
* à l'utilisateur * à l'utilisateur
*/ */
class SpreadsheetBuilder extends AbstractBuilder { class SsBuilder extends AbstractBuilder {
static function with($builder): self { use TAbstractBuilder;
if ($builder instanceof self) return $builder;
elseif (is_string($builder)) return new static($builder);
elseif (is_array($builder)) return new static(null, $builder);
else throw ValueException::invalid_type($builder, self::class);
}
function __construct(?string $output, ?array $params=null) { function __construct(?string $output, ?array $params=null) {
parent::__construct($output, $params);
$this->ss = new Spreadsheet(); $this->ss = new Spreadsheet();
$this->valueBinder = new StringValueBinder(); $this->valueBinder = new StringValueBinder();
$this->wsname = $params["wsname"] ?? null; $this->setWsname($params["wsname"] ?? null);
parent::__construct($output, $params);
} }
protected Spreadsheet $ss; protected Spreadsheet $ss;
protected IValueBinder $valueBinder; protected IValueBinder $valueBinder;
protected ?string $wsname; protected ?Worksheet $ws;
protected ?Worksheet $ws = null; protected int $nrow;
protected function ws(): Worksheet { const STYLE_ROW = 0, STYLE_HEADER = 1;
$ws = $this->ws;
if ($ws !== null) return $ws; protected int $rowStyle;
function setWsname(?string $wsname): void {
$ss = $this->ss; $ss = $this->ss;
$wsname = $this->wsname; $this->ws = null;
$this->nrow = 0;
$this->rowStyle = self::STYLE_ROW;
if ($wsname === null) { if ($wsname === null) {
$ws = $ss->getActiveSheet(); $ws = $ss->getActiveSheet();
} else { } else {
$ws = $ss->getSheetByName($wsname); $ws = $ss->getSheetByName($wsname);
}
if ($ws === null) { if ($ws === null) {
$ws = $ss->createSheet()->setTitle($wsname); $ws = $ss->createSheet()->setTitle($wsname);
} else {
$maxRow = wsutils::compute_max_coords($ws)[1];
$this->nrow = $maxRow - 1;
} }
$this->ws = $ws;
} }
return $this->ws = $ws;
}
protected int $nrow = 0;
const STYLE_HEADER = 0, STYLE_ROW = 1;
protected int $style = self::STYLE_ROW;
function _write(array $row): void { function _write(array $row): void {
$ws = $this->ws(); $ws = $this->ws;
$styleHeader = $this->rowStyle === self::STYLE_HEADER;
$nrow = ++$this->nrow; $nrow = ++$this->nrow;
$ncol = 1; $ncol = 1;
foreach ($row as $col) { foreach ($row as $col) {
//$cell = $ws->getCell([$ncol++, $nrow]); $ws->getCellByColumnAndRow($ncol++, $nrow)->setValue($col, $this->valueBinder);
$cell = $ws->getCellByColumnAndRow($ncol++, $nrow);
$cell->setValue($col, $this->valueBinder);
switch ($this->style) {
case self::STYLE_HEADER:
$cell->getStyle()->getFont()->setBold(true);
break;
case self::STYLE_ROW:
break;
} }
if ($styleHeader) {
$ws->getStyle("$nrow:$nrow")->getFont()->setBold(true);
} }
} }
function writeHeaders(?array $headers=null): void { function writeHeaders(?array $headers=null): void {
$this->style = self::STYLE_HEADER; $this->rowStyle = self::STYLE_HEADER;
parent::writeHeaders($headers); parent::writeHeaders($headers);
$this->style = self::STYLE_ROW; $this->rowStyle = self::STYLE_ROW;
} }
function _sendContentType(): void { function _sendContentType(): void {

View File

@ -0,0 +1,13 @@
<?php
namespace nur\sery\file\csv;
use nur\sery\ValueException;
trait TAbstractBuilder {
static function with($builder): self {
if ($builder instanceof self) return $builder;
elseif (is_string($builder)) return new static($builder);
elseif (is_array($builder)) return new static(null, $builder);
else throw ValueException::invalid_type($builder, self::class);
}
}