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>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
</suites>
</component>
</project>

View File

@ -6,10 +6,10 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class wsutils {
static function get_highest_coords(Worksheet $ws): array {
$highestRow = $ws->getHighestRow();
$highestColumnA = $ws->getHighestColumn();
$highestCol = Coordinate::columnIndexFromString($highestColumnA);
return [$highestRow, $highestCol];
$highestRow = $ws->getHighestRow();
return [$highestCol, $highestRow];
}
/**
@ -24,18 +24,18 @@ class wsutils {
const MAX_EMPTY_THRESHOLD = 150;
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;
$maxRow = 1;
$maxEmptyRows = self::MAX_EMPTY_THRESHOLD;
for ($row = 1; $row <= $highestRow; $row++) {
$emptyRow = true;
$maxEmptyCols = self::MAX_EMPTY_THRESHOLD;
for ($col = 1; $col <= $highestCol; $col++) {
$value = null;
if ($ws->cellExists([$col, $row])) {
$value = $ws->getCell([$col, $row])->getValue();
if ($ws->cellExistsByColumnAndRow($col, $row)) {
$value = $ws->getCellByColumnAndRow($col, $row)->getValue();
}
if ($value === null) {
$maxEmptyCols--;
@ -54,6 +54,6 @@ class wsutils {
$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 extends AbstractBuilder {
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);
}
use TAbstractBuilder;
function __construct(?string $output, ?array $params=null) {
$csvFlavour = $params["csv_flavour"] ?? null;

View File

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