Make ODS reader return Row object
This commit is contained in:
1 parent
68a96367a8
commit
102e17159c
16 files changed
+301
-67
No files matched your search
@@ -3,12 +3,18 @@
|
||||
namespace Box\Spout\Reader\ODS;
|
||||
|
||||
use Box\Spout\Common\Exception\IOException;
|
||||
use Box\Spout\Common\Manager\OptionsManagerInterface;
|
||||
use Box\Spout\Reader\Common\Entity\Cell;
|
||||
use Box\Spout\Reader\Common\Entity\Options;
|
||||
use Box\Spout\Reader\Common\Entity\Row;
|
||||
use Box\Spout\Reader\Common\XMLProcessor;
|
||||
use Box\Spout\Reader\Exception\InvalidValueException;
|
||||
use Box\Spout\Reader\Exception\IteratorNotRewindableException;
|
||||
use Box\Spout\Reader\Exception\XMLProcessingException;
|
||||
use Box\Spout\Reader\IteratorInterface;
|
||||
use Box\Spout\Reader\ODS\Creator\InternalEntityFactory;
|
||||
use Box\Spout\Reader\ODS\Helper\CellValueFormatter;
|
||||
use Box\Spout\Reader\ODS\Manager\RowManager;
|
||||
use Box\Spout\Reader\Wrapper\XMLReader;
|
||||
|
||||
/**
|
||||
@@ -38,14 +44,20 @@ class RowIterator implements IteratorInterface
|
||||
/** @var Helper\CellValueFormatter Helper to format cell values */
|
||||
protected $cellValueFormatter;
|
||||
|
||||
/** @var RowManager Manages rows */
|
||||
protected $rowManager;
|
||||
|
||||
/** @var InternalEntityFactory Factory to create entities */
|
||||
protected $entityFactory;
|
||||
|
||||
/** @var bool Whether the iterator has already been rewound once */
|
||||
protected $hasAlreadyBeenRewound = false;
|
||||
|
||||
/** @var array Contains the data for the currently processed row (key = cell index, value = cell value) */
|
||||
protected $currentlyProcessedRowData = [];
|
||||
/** @var Row The currently processed row */
|
||||
protected $currentlyProcessedRow;
|
||||
|
||||
/** @var array|null Buffer used to store the row data, while checking if there are more rows to read */
|
||||
protected $rowDataBuffer;
|
||||
/** @var Row Buffer used to store the current row, while checking if there are more rows to read */
|
||||
protected $rowBuffer;
|
||||
|
||||
/** @var bool Indicates whether all rows have been read */
|
||||
protected $hasReachedEndOfFile = false;
|
||||
@@ -56,8 +68,8 @@ class RowIterator implements IteratorInterface
|
||||
/** @var int Row index to be processed next (one-based) */
|
||||
protected $nextRowIndexToBeProcessed = 1;
|
||||
|
||||
/** @var mixed|null Value of the last processed cell (because when reading cell at column N+1, cell N is processed) */
|
||||
protected $lastProcessedCellValue;
|
||||
/** @var Cell Last processed cell (because when reading cell at column N+1, cell N is processed) */
|
||||
protected $lastProcessedCell;
|
||||
|
||||
/** @var int Number of times the last processed row should be repeated */
|
||||
protected $numRowsRepeated = 1;
|
||||
@@ -70,15 +82,25 @@ class RowIterator implements IteratorInterface
|
||||
|
||||
/**
|
||||
* @param XMLReader $xmlReader XML Reader, positioned on the "<table:table>" element
|
||||
* @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
|
||||
* @param OptionsManagerInterface $optionsManager Reader's options manager
|
||||
* @param CellValueFormatter $cellValueFormatter Helper to format cell values
|
||||
* @param XMLProcessor $xmlProcessor Helper to process XML files
|
||||
* @param RowManager $rowManager Manages rows
|
||||
* @param InternalEntityFactory $entityFactory Factory to create entities
|
||||
*/
|
||||
public function __construct($xmlReader, $optionsManager, $cellValueFormatter, $xmlProcessor)
|
||||
{
|
||||
public function __construct(
|
||||
XMLReader $xmlReader,
|
||||
OptionsManagerInterface $optionsManager,
|
||||
CellValueFormatter $cellValueFormatter,
|
||||
XMLProcessor $xmlProcessor,
|
||||
RowManager $rowManager,
|
||||
InternalEntityFactory $entityFactory
|
||||
) {
|
||||
$this->xmlReader = $xmlReader;
|
||||
$this->shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
|
||||
$this->cellValueFormatter = $cellValueFormatter;
|
||||
$this->entityFactory = $entityFactory;
|
||||
$this->rowManager = $rowManager;
|
||||
|
||||
// Register all callbacks to process different nodes when reading the XML file
|
||||
$this->xmlProcessor = $xmlProcessor;
|
||||
@@ -108,7 +130,7 @@ class RowIterator implements IteratorInterface
|
||||
$this->hasAlreadyBeenRewound = true;
|
||||
$this->lastRowIndexProcessed = 0;
|
||||
$this->nextRowIndexToBeProcessed = 1;
|
||||
$this->rowDataBuffer = null;
|
||||
$this->rowBuffer = null;
|
||||
$this->hasReachedEndOfFile = false;
|
||||
|
||||
$this->next();
|
||||
@@ -168,7 +190,7 @@ class RowIterator implements IteratorInterface
|
||||
*/
|
||||
protected function readDataForNextRow()
|
||||
{
|
||||
$this->currentlyProcessedRowData = [];
|
||||
$this->currentlyProcessedRow = $this->entityFactory->createRow([]);
|
||||
|
||||
try {
|
||||
$this->xmlProcessor->readUntilStopped();
|
||||
@@ -176,7 +198,7 @@ class RowIterator implements IteratorInterface
|
||||
throw new IOException("The sheet's data cannot be read. [{$exception->getMessage()}]");
|
||||
}
|
||||
|
||||
$this->rowDataBuffer = $this->currentlyProcessedRowData;
|
||||
$this->rowBuffer = $this->currentlyProcessedRow;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,7 +209,7 @@ class RowIterator implements IteratorInterface
|
||||
{
|
||||
// Reset data from current row
|
||||
$this->hasAlreadyReadOneCellInCurrentRow = false;
|
||||
$this->lastProcessedCellValue = null;
|
||||
$this->lastProcessedCell = null;
|
||||
$this->numColumnsRepeated = 1;
|
||||
$this->numRowsRepeated = $this->getNumRowsRepeatedForCurrentNode($xmlReader);
|
||||
|
||||
@@ -204,17 +226,17 @@ class RowIterator implements IteratorInterface
|
||||
|
||||
// NOTE: expand() will automatically decode all XML entities of the child nodes
|
||||
$node = $xmlReader->expand();
|
||||
$currentCellValue = $this->getCellValue($node);
|
||||
$currentCell = $this->getCell($node);
|
||||
|
||||
// process cell N only after having read cell N+1 (see below why)
|
||||
if ($this->hasAlreadyReadOneCellInCurrentRow) {
|
||||
for ($i = 0; $i < $this->numColumnsRepeated; $i++) {
|
||||
$this->currentlyProcessedRowData[] = $this->lastProcessedCellValue;
|
||||
$this->currentlyProcessedRow->addCell($this->lastProcessedCell);
|
||||
}
|
||||
}
|
||||
|
||||
$this->hasAlreadyReadOneCellInCurrentRow = true;
|
||||
$this->lastProcessedCellValue = $currentCellValue;
|
||||
$this->lastProcessedCell = $currentCell;
|
||||
$this->numColumnsRepeated = $currentNumColumnsRepeated;
|
||||
|
||||
return XMLProcessor::PROCESSING_CONTINUE;
|
||||
@@ -225,7 +247,7 @@ class RowIterator implements IteratorInterface
|
||||
*/
|
||||
protected function processRowEndingNode()
|
||||
{
|
||||
$isEmptyRow = $this->isEmptyRow($this->currentlyProcessedRowData, $this->lastProcessedCellValue);
|
||||
$isEmptyRow = $this->isEmptyRow($this->currentlyProcessedRow, $this->lastProcessedCell);
|
||||
|
||||
// if the fetched row is empty and we don't want to preserve it...
|
||||
if (!$this->shouldPreserveEmptyRows && $isEmptyRow) {
|
||||
@@ -235,6 +257,7 @@ class RowIterator implements IteratorInterface
|
||||
|
||||
// if the row is empty, we don't want to return more than one cell
|
||||
$actualNumColumnsRepeated = (!$isEmptyRow) ? $this->numColumnsRepeated : 1;
|
||||
$numCellsInCurrentlyProcessedRow = count($this->currentlyProcessedRow->getCells());
|
||||
|
||||
// Only add the value if the last read cell is not a trailing empty cell repeater in Excel.
|
||||
// The current count of read columns is determined by counting the values in "$this->currentlyProcessedRowData".
|
||||
@@ -242,9 +265,9 @@ class RowIterator implements IteratorInterface
|
||||
// with a number-columns-repeated value equals to the number of (supported columns - used columns).
|
||||
// In Excel, the number of supported columns is 16384, but we don't want to returns rows with
|
||||
// always 16384 cells.
|
||||
if ((count($this->currentlyProcessedRowData) + $actualNumColumnsRepeated) !== self::MAX_COLUMNS_EXCEL) {
|
||||
if (($numCellsInCurrentlyProcessedRow + $actualNumColumnsRepeated) !== self::MAX_COLUMNS_EXCEL) {
|
||||
for ($i = 0; $i < $actualNumColumnsRepeated; $i++) {
|
||||
$this->currentlyProcessedRowData[] = $this->lastProcessedCellValue;
|
||||
$this->currentlyProcessedRow->addCell($this->lastProcessedCell);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,31 +314,39 @@ class RowIterator implements IteratorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the (unescaped) correctly marshalled, cell value associated to the given XML node.
|
||||
* Returns the cell with (unescaped) correctly marshalled, cell value associated to the given XML node.
|
||||
*
|
||||
* @param \DOMNode $node
|
||||
* @return string|int|float|bool|\DateTime|\DateInterval|null The value associated with the cell, empty string if cell's type is void/undefined, null on error
|
||||
* @return Cell The cell set with the associated with the cell
|
||||
*/
|
||||
protected function getCellValue($node)
|
||||
protected function getCell($node)
|
||||
{
|
||||
return $this->cellValueFormatter->extractAndFormatNodeValue($node);
|
||||
try {
|
||||
$cellValue = $this->cellValueFormatter->extractAndFormatNodeValue($node);
|
||||
$cell = $this->entityFactory->createCell($cellValue);
|
||||
} catch (InvalidValueException $exception) {
|
||||
$cell = $this->entityFactory->createCell($exception->getInvalidValue());
|
||||
$cell->setType(Cell::TYPE_ERROR);
|
||||
}
|
||||
|
||||
return $cell;
|
||||
}
|
||||
|
||||
/**
|
||||
* After finishing processing each cell, a row is considered empty if it contains
|
||||
* no cells or if the value of the last read cell is an empty string.
|
||||
* no cells or if the last read cell is empty.
|
||||
* After finishing processing each cell, the last read cell is not part of the
|
||||
* row data yet (as we still need to apply the "num-columns-repeated" attribute).
|
||||
*
|
||||
* @param array $rowData
|
||||
* @param string|int|float|bool|\DateTime|\DateInterval|null $lastReadCellValue The value of the last read cell
|
||||
* @param Row $currentRow
|
||||
* @param Cell $lastReadCell The last read cell
|
||||
* @return bool Whether the row is empty
|
||||
*/
|
||||
protected function isEmptyRow($rowData, $lastReadCellValue)
|
||||
protected function isEmptyRow($currentRow, $lastReadCell)
|
||||
{
|
||||
return (
|
||||
count($rowData) === 0 &&
|
||||
(!isset($lastReadCellValue) || trim($lastReadCellValue) === '')
|
||||
$this->rowManager->isEmpty($currentRow) &&
|
||||
(!isset($lastReadCell) || $lastReadCell->isEmpty())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -323,11 +354,11 @@ class RowIterator implements IteratorInterface
|
||||
* Return the current element, from the buffer.
|
||||
* @see http://php.net/manual/en/iterator.current.php
|
||||
*
|
||||
* @return array|null
|
||||
* @return Row
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->rowDataBuffer;
|
||||
return $this->rowBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user