modifs.mineures sans commentaires
This commit is contained in:
parent
ab89d08933
commit
4285804be4
|
@ -4,6 +4,8 @@ namespace nur\sery\file\csv;
|
|||
use nur\sery\file\FileReader;
|
||||
|
||||
class CsvReader extends AbstractReader {
|
||||
use TAbstractReader;
|
||||
|
||||
function getIterator() {
|
||||
$reader = new FileReader($this->input);
|
||||
while (($row = $reader->fgetcsv()) !== null) {
|
||||
|
|
|
@ -3,5 +3,5 @@ namespace nur\sery\file\csv;
|
|||
|
||||
use IteratorAggregate;
|
||||
|
||||
interface IReader extends IteratorAggregate {
|
||||
interface IReader extends IteratorAggregate {
|
||||
}
|
||||
|
|
|
@ -11,13 +11,7 @@ use PhpOffice\PhpSpreadsheet\IOFactory;
|
|||
use PhpOffice\PhpSpreadsheet\RichText\RichText;
|
||||
|
||||
class SsReader extends AbstractReader {
|
||||
static function with(Upload $upload): IReader {
|
||||
if ($upload->isExt(".csv")) {
|
||||
return new CsvReader($upload->tmpName);
|
||||
} else {
|
||||
return new static($upload->tmpName);
|
||||
}
|
||||
}
|
||||
use TAbstractReader;
|
||||
|
||||
const DATETIME_FORMAT = 'dd/mm/yyyy hh:mm:ss';
|
||||
const DATE_FORMAT = 'dd/mm/yyyy';
|
||||
|
@ -66,6 +60,11 @@ class SsReader extends AbstractReader {
|
|||
|
||||
protected ?string $wsname;
|
||||
|
||||
function setWsname(?string $wsname): self {
|
||||
$this->wsname = $wsname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
function getIterator() {
|
||||
$ss = IOFactory::load($this->input);
|
||||
$ws = wsutils::get_ws($this->wsname, $ss);
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
namespace nur\sery\file\csv;
|
||||
|
||||
use nur\sery\file\web\Upload;
|
||||
use nur\sery\os\path;
|
||||
use nur\sery\ValueException;
|
||||
|
||||
trait TAbstractReader {
|
||||
/** @param Upload|string|array */
|
||||
static function with($reader): self {
|
||||
if ($reader instanceof self) return $reader;
|
||||
$class = null;
|
||||
if ($reader instanceof Upload) {
|
||||
if ($reader->isExt(".csv")) $class = CsvReader::class;
|
||||
else $class = static::class;
|
||||
return $class($reader->tmpName);
|
||||
}
|
||||
if (is_string($reader)) $reader = ["input" => $reader];
|
||||
if (!is_array($reader)) {
|
||||
throw ValueException::invalid_type($reader, self::class);
|
||||
}
|
||||
$input = $reader["input"] ?? null;
|
||||
if (is_string($input) && path::ext($input) === ".csv") {
|
||||
$class = CsvReader::class;
|
||||
}
|
||||
if ($class === null) $class = static::class;
|
||||
return $class($reader);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue