nur-sery/src/ext/spreadsheet/SpoutReader.php

56 lines
1.3 KiB
PHP

<?php
namespace nur\sery\ext\spreadsheet;
use nur\sery\file\csv\AbstractReader;
use nur\sery\file\csv\TAbstractReader;
use OpenSpout\Reader\Common\Creator\ReaderEntityFactory;
class SpoutReader extends AbstractReader {
/** @var string|int|null nom de la feuille depuis laquelle lire */
const WSNAME = null;
function __construct($input, ?array $params=null) {
parent::__construct($input, $params);
$this->wsname = $params["wsname"] ?? static::WSNAME;
}
protected $wsname;
/**
* @param string|int|null $wsname
*/
function setWsname($wsname): self {
$this->wsname = $wsname;
return $this;
}
function getIterator() {
$ss = ReaderEntityFactory::createReaderFromFile($this->input);
try {
$found = false;
foreach ($ss->getSheetIterator() as $ws) {
if ($this->wsname === null || $this->wsname === $ws->getName()) {
$found = true;
break;
}
}
if (!$found) return;
$this->isrc = $this->idest = 0;
foreach ($ws->getRowIterator() as $row) {
$row = $row->toArray();
foreach ($row as &$col) {
$this->verifixCol($col);
}; unset($col);
if ($this->cook($row)) {
yield $row;
$this->idest++;
}
$this->isrc++;
}
} finally {
$ss->close();
}
}
}