From 375351a0b4df45059ab08d3c0ac44c8480a3f39c Mon Sep 17 00:00:00 2001 From: Jephte Clain Date: Tue, 11 Jun 2024 17:36:08 +0400 Subject: [PATCH] modifs.mineures sans commentaires --- src/file/csv/AbstractReader.php | 12 ++++++++++++ src/file/csv/CsvReader.php | 10 +--------- src/file/csv/SsReader.php | 19 +++++++------------ src/php/time/DateTime.php | 13 ++++++++----- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/file/csv/AbstractReader.php b/src/file/csv/AbstractReader.php index e5b2a15..6a8444e 100644 --- a/src/file/csv/AbstractReader.php +++ b/src/file/csv/AbstractReader.php @@ -2,6 +2,9 @@ namespace nur\sery\file\csv; use IteratorAggregate; +use nur\sery\php\func; +use nur\sery\php\time\Date; +use nur\sery\php\time\DateTime; abstract class AbstractReader implements IReader { const SCHEMA = null; @@ -42,4 +45,13 @@ abstract class AbstractReader implements IReader { $row = array_combine($this->headers, $row); 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); + } + } } diff --git a/src/file/csv/CsvReader.php b/src/file/csv/CsvReader.php index f83c507..5ea0c8b 100644 --- a/src/file/csv/CsvReader.php +++ b/src/file/csv/CsvReader.php @@ -2,22 +2,14 @@ namespace nur\sery\file\csv; use nur\sery\file\FileReader; -use nur\sery\php\time\Date; -use nur\sery\php\time\DateTime; class CsvReader extends AbstractReader { function getIterator() { $reader = new FileReader($this->input); while (($row = $reader->fgetcsv()) !== null) { foreach ($row as &$col) { - # conversion Date et DateTime - if (DateTime::isa_datetime($col, true)) { - $col = new DateTime($col); - } elseif (DateTime::isa_date($col, true)) { - $col = new Date($col); - } + $this->verifixCol($col); }; unset($col); - if ($this->cook($row)) { yield $row; $this->idest++; diff --git a/src/file/csv/SsReader.php b/src/file/csv/SsReader.php index 3ce06e6..151b971 100644 --- a/src/file/csv/SsReader.php +++ b/src/file/csv/SsReader.php @@ -77,9 +77,9 @@ class SsReader extends AbstractReader { for ($ncol = 1; $ncol <= $nbCols; $ncol++) { if ($ws->cellExistsByColumnAndRow($ncol, $nrow)) { $cell = $ws->getCellByColumnAndRow($ncol, $nrow); - $value = $cell->getValue(); - if ($value instanceof RichText) { - $value = $value->getPlainText(); + $col = $cell->getValue(); + if ($col instanceof RichText) { + $col = $col->getPlainText(); } else { $dataType = $cell->getDataType(); if ($dataType == DataType::TYPE_NUMERIC || $dataType == DataType::TYPE_FORMULA) { @@ -101,18 +101,13 @@ class SsReader extends AbstractReader { } } } - $value = $cell->getFormattedValue(); - # conversion Date et DateTime - if (DateTime::isa_datetime($value, true)) { - $value = new DateTime($value); - } elseif (DateTime::isa_date($value, true)) { - $value = new Date($value); - } + $col = $cell->getFormattedValue(); + $this->verifixCol($col); } } else { - $value = null; + $col = null; } - $row[] = $value; + $row[] = $col; } if ($this->cook($row)) { yield $row; diff --git a/src/php/time/DateTime.php b/src/php/time/DateTime.php index 589f1dd..24310b8 100644 --- a/src/php/time/DateTime.php +++ b/src/php/time/DateTime.php @@ -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])?$/'; static function isa($datetime): bool { + if ($datetime === null) return false; if ($datetime instanceof DateTimeInterface) return true; if (!is_int($datetime) && !is_string($datetime)) return false; return preg_match(self::DMY_PATTERN, $datetime) || @@ -44,17 +45,19 @@ class DateTime extends \DateTime { } static function isa_datetime($datetime, bool $frOnly=false): bool { + if ($datetime === null) return false; if ($datetime instanceof DateTimeInterface) return true; if (!is_int($datetime) && !is_string($datetime)) return false; return preg_match(self::DMYHIS_PATTERN, $datetime) || (!$frOnly && preg_match(self::YMDHISZ_PATTERN, $datetime)); } - static function isa_date($datetime, bool $frOnly=false): bool { - if ($datetime instanceof DateTimeInterface) return true; - if (!is_int($datetime) && !is_string($datetime)) return false; - return preg_match(self::DMY_PATTERN, $datetime) || - (!$frOnly && preg_match(self::YMD_PATTERN, $datetime)); + static function isa_date($date, bool $frOnly=false): bool { + if ($date === null) return false; + if ($date instanceof DateTimeInterface) return true; + if (!is_int($date) && !is_string($date)) return false; + return preg_match(self::DMY_PATTERN, $date) || + (!$frOnly && preg_match(self::YMD_PATTERN, $date)); } static function _YmdHMSZ_format(\DateTime $datetime): string {