modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-06-11 17:36:08 +04:00
parent 6bffe89904
commit 375351a0b4
4 changed files with 28 additions and 26 deletions

View File

@ -2,6 +2,9 @@
namespace nur\sery\file\csv; namespace nur\sery\file\csv;
use IteratorAggregate; use IteratorAggregate;
use nur\sery\php\func;
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 SCHEMA = null;
@ -42,4 +45,13 @@ abstract class AbstractReader implements IReader {
$row = array_combine($this->headers, $row); $row = array_combine($this->headers, $row);
return true; return true;
} }
protected function verifixCol(&$col): void {
# conversion Date et DateTime
if (DateTime::isa_datetime($col, true)) {
$col = new DateTime($col);
} elseif (DateTime::isa_date($col, true)) {
$col = new Date($col);
}
}
} }

View File

@ -2,22 +2,14 @@
namespace nur\sery\file\csv; namespace nur\sery\file\csv;
use nur\sery\file\FileReader; use nur\sery\file\FileReader;
use nur\sery\php\time\Date;
use nur\sery\php\time\DateTime;
class CsvReader extends AbstractReader { class CsvReader extends AbstractReader {
function getIterator() { function getIterator() {
$reader = new FileReader($this->input); $reader = new FileReader($this->input);
while (($row = $reader->fgetcsv()) !== null) { while (($row = $reader->fgetcsv()) !== null) {
foreach ($row as &$col) { foreach ($row as &$col) {
# conversion Date et DateTime $this->verifixCol($col);
if (DateTime::isa_datetime($col, true)) {
$col = new DateTime($col);
} elseif (DateTime::isa_date($col, true)) {
$col = new Date($col);
}
}; unset($col); }; unset($col);
if ($this->cook($row)) { if ($this->cook($row)) {
yield $row; yield $row;
$this->idest++; $this->idest++;

View File

@ -77,9 +77,9 @@ class SsReader extends AbstractReader {
for ($ncol = 1; $ncol <= $nbCols; $ncol++) { for ($ncol = 1; $ncol <= $nbCols; $ncol++) {
if ($ws->cellExistsByColumnAndRow($ncol, $nrow)) { if ($ws->cellExistsByColumnAndRow($ncol, $nrow)) {
$cell = $ws->getCellByColumnAndRow($ncol, $nrow); $cell = $ws->getCellByColumnAndRow($ncol, $nrow);
$value = $cell->getValue(); $col = $cell->getValue();
if ($value instanceof RichText) { if ($col instanceof RichText) {
$value = $value->getPlainText(); $col = $col->getPlainText();
} else { } else {
$dataType = $cell->getDataType(); $dataType = $cell->getDataType();
if ($dataType == DataType::TYPE_NUMERIC || $dataType == DataType::TYPE_FORMULA) { if ($dataType == DataType::TYPE_NUMERIC || $dataType == DataType::TYPE_FORMULA) {
@ -101,18 +101,13 @@ class SsReader extends AbstractReader {
} }
} }
} }
$value = $cell->getFormattedValue(); $col = $cell->getFormattedValue();
# conversion Date et DateTime $this->verifixCol($col);
if (DateTime::isa_datetime($value, true)) {
$value = new DateTime($value);
} elseif (DateTime::isa_date($value, true)) {
$value = new Date($value);
}
} }
} else { } else {
$value = null; $col = null;
} }
$row[] = $value; $row[] = $col;
} }
if ($this->cook($row)) { if ($this->cook($row)) {
yield $row; yield $row;

View File

@ -35,6 +35,7 @@ class DateTime extends \DateTime {
const YMDHISZ_PATTERN = '/^((?:\d{2})?\d{2})(\d{2})(\d{2})[tT](\d{2})(\d{2})(\d{2})?([zZ])?$/'; const YMDHISZ_PATTERN = '/^((?:\d{2})?\d{2})(\d{2})(\d{2})[tT](\d{2})(\d{2})(\d{2})?([zZ])?$/';
static function isa($datetime): bool { static function isa($datetime): bool {
if ($datetime === null) return false;
if ($datetime instanceof DateTimeInterface) return true; if ($datetime instanceof DateTimeInterface) return true;
if (!is_int($datetime) && !is_string($datetime)) return false; if (!is_int($datetime) && !is_string($datetime)) return false;
return preg_match(self::DMY_PATTERN, $datetime) || return preg_match(self::DMY_PATTERN, $datetime) ||
@ -44,17 +45,19 @@ class DateTime extends \DateTime {
} }
static function isa_datetime($datetime, bool $frOnly=false): bool { static function isa_datetime($datetime, bool $frOnly=false): bool {
if ($datetime === null) return false;
if ($datetime instanceof DateTimeInterface) return true; if ($datetime instanceof DateTimeInterface) return true;
if (!is_int($datetime) && !is_string($datetime)) return false; if (!is_int($datetime) && !is_string($datetime)) return false;
return preg_match(self::DMYHIS_PATTERN, $datetime) || return preg_match(self::DMYHIS_PATTERN, $datetime) ||
(!$frOnly && preg_match(self::YMDHISZ_PATTERN, $datetime)); (!$frOnly && preg_match(self::YMDHISZ_PATTERN, $datetime));
} }
static function isa_date($datetime, bool $frOnly=false): bool { static function isa_date($date, bool $frOnly=false): bool {
if ($datetime instanceof DateTimeInterface) return true; if ($date === null) return false;
if (!is_int($datetime) && !is_string($datetime)) return false; if ($date instanceof DateTimeInterface) return true;
return preg_match(self::DMY_PATTERN, $datetime) || if (!is_int($date) && !is_string($date)) return false;
(!$frOnly && preg_match(self::YMD_PATTERN, $datetime)); return preg_match(self::DMY_PATTERN, $date) ||
(!$frOnly && preg_match(self::YMD_PATTERN, $date));
} }
static function _YmdHMSZ_format(\DateTime $datetime): string { static function _YmdHMSZ_format(\DateTime $datetime): string {