Fix zeros treated as missing values #184

This commit is contained in:
madflow 2016-03-16 20:28:27 +01:00
parent 2b1160bb33
commit 3ee7099c95

View File

@ -127,7 +127,7 @@ class RowIterator implements IteratorInterface
} else if ($this->xmlReader->isPositionedOnEndingNode(self::XML_NODE_ROW)) { } else if ($this->xmlReader->isPositionedOnEndingNode(self::XML_NODE_ROW)) {
// End of the row description // End of the row description
$isEmptyRow = ($numCellsRead <= 1 && empty($cellValue)); $isEmptyRow = ($numCellsRead <= 1 && $this->isEmptyCellValue($cellValue));
if ($isEmptyRow) { if ($isEmptyRow) {
// skip empty rows // skip empty rows
$this->next(); $this->next();
@ -138,7 +138,7 @@ class RowIterator implements IteratorInterface
// This is to avoid creating a lot of empty cells, as Excel adds a last empty "<table:table-cell>" // This is to avoid creating a lot of empty cells, as Excel adds a last empty "<table:table-cell>"
// with a number-columns-repeated value equals to the number of (supported columns - used columns). // 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. // In Excel, the number of supported columns is 16384, but we don't want to returns rows with always 16384 cells.
if (!empty($cellValue) || $numColumnsRepeated === 1) { if (!$this->isEmptyCellValue($cellValue) || $numColumnsRepeated === 1) {
for ($i = 0; $i < $numColumnsRepeated; $i++) { for ($i = 0; $i < $numColumnsRepeated; $i++) {
$rowData[] = $cellValue; $rowData[] = $cellValue;
} }
@ -181,6 +181,17 @@ class RowIterator implements IteratorInterface
return $this->cellValueFormatter->extractAndFormatNodeValue($node); return $this->cellValueFormatter->extractAndFormatNodeValue($node);
} }
/**
* empty() replacement that honours 0 as a valid value
*
* @param $value The cell value
* @return bool
*/
protected function isEmptyCellValue($value)
{
return (!isset($value) || trim($value) === '');
}
/** /**
* Return the current element, from the buffer. * Return the current element, from the buffer.
* @link http://php.net/manual/en/iterator.current.php * @link http://php.net/manual/en/iterator.current.php