diff --git a/src/Spout/Reader/CSV/Creator/InternalEntityFactory.php b/src/Spout/Reader/CSV/Creator/InternalEntityFactory.php index d0b9114..f4e3c53 100644 --- a/src/Spout/Reader/CSV/Creator/InternalEntityFactory.php +++ b/src/Spout/Reader/CSV/Creator/InternalEntityFactory.php @@ -65,6 +65,24 @@ class InternalEntityFactory implements InternalEntityFactoryInterface return new RowIterator($filePointer, $optionsManager, $encodingHelper, $this, $globalFunctionsHelper); } + /** + * @param Cell[] $cells + * @return Row + */ + public function createRow(array $cells = []) + { + return new Row($cells); + } + + /** + * @param mixed $cellValue + * @return Cell + */ + public function createCell($cellValue) + { + return new Cell($cellValue); + } + /** * @param array $cellValues * @return Row @@ -72,9 +90,9 @@ class InternalEntityFactory implements InternalEntityFactoryInterface public function createRowFromArray(array $cellValues = []) { $cells = array_map(function ($cellValue) { - return new Cell($cellValue); + return $this->createCell($cellValue); }, $cellValues); - return new Row($cells); + return $this->createRow($cells); } } diff --git a/src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php b/src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php index 1df1c1e..9f9fd04 100644 --- a/src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php +++ b/src/Spout/Reader/Common/Creator/InternalEntityFactoryInterface.php @@ -2,9 +2,23 @@ namespace Box\Spout\Reader\Common\Creator; +use Box\Spout\Reader\Common\Entity\Cell; +use Box\Spout\Reader\Common\Entity\Row; + /** * Interface EntityFactoryInterface */ interface InternalEntityFactoryInterface { + /** + * @param Cell[] $cells + * @return Row + */ + public function createRow(array $cells = []); + + /** + * @param mixed $cellValue + * @return Cell + */ + public function createCell($cellValue); } diff --git a/src/Spout/Reader/Common/Entity/Row.php b/src/Spout/Reader/Common/Entity/Row.php index a63414a..84ff56e 100644 --- a/src/Spout/Reader/Common/Entity/Row.php +++ b/src/Spout/Reader/Common/Entity/Row.php @@ -65,6 +65,14 @@ class Row return $this; } + /** + * @return int + */ + public function getNumCells() + { + return count($this->cells); + } + /** * @return array The row values, as array */ diff --git a/src/Spout/Reader/XLSX/Manager/RowManager.php b/src/Spout/Reader/Common/Manager/RowManager.php similarity index 74% rename from src/Spout/Reader/XLSX/Manager/RowManager.php rename to src/Spout/Reader/Common/Manager/RowManager.php index ec85bdc..f0b55ff 100644 --- a/src/Spout/Reader/XLSX/Manager/RowManager.php +++ b/src/Spout/Reader/Common/Manager/RowManager.php @@ -1,22 +1,22 @@ entityFactory = $entityFactory; } @@ -47,11 +47,11 @@ class RowManager */ public function fillMissingIndexesWithEmptyCells(Row $row) { - $rowCells = $row->getCells(); - if (count($rowCells) === 0) { + if ($row->getNumCells() === 0) { return $row; } + $rowCells = $row->getCells(); $maxCellIndex = max(array_keys($rowCells)); for ($cellIndex = 0; $cellIndex < $maxCellIndex; $cellIndex++) { diff --git a/src/Spout/Reader/ODS/Creator/InternalEntityFactory.php b/src/Spout/Reader/ODS/Creator/InternalEntityFactory.php index 7df4d8b..04c212c 100644 --- a/src/Spout/Reader/ODS/Creator/InternalEntityFactory.php +++ b/src/Spout/Reader/ODS/Creator/InternalEntityFactory.php @@ -73,7 +73,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface $shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES); $cellValueFormatter = $this->helperFactory->createCellValueFormatter($shouldFormatDates); $xmlProcessor = $this->createXMLProcessor($xmlReader); - $rowManager = $this->managerFactory->createRowManager(); + $rowManager = $this->managerFactory->createRowManager($this); return new RowIterator($xmlReader, $optionsManager, $cellValueFormatter, $xmlProcessor, $rowManager, $this); } @@ -82,7 +82,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface * @param Cell[] $cells * @return Row */ - public function createRow(array $cells) + public function createRow(array $cells = []) { return new Row($cells); } diff --git a/src/Spout/Reader/ODS/Creator/ManagerFactory.php b/src/Spout/Reader/ODS/Creator/ManagerFactory.php index 2543bdc..3644d15 100644 --- a/src/Spout/Reader/ODS/Creator/ManagerFactory.php +++ b/src/Spout/Reader/ODS/Creator/ManagerFactory.php @@ -2,7 +2,7 @@ namespace Box\Spout\Reader\ODS\Creator; -use Box\Spout\Reader\ODS\Manager\RowManager; +use Box\Spout\Reader\Common\Manager\RowManager; /** * Class ManagerFactory @@ -11,10 +11,11 @@ use Box\Spout\Reader\ODS\Manager\RowManager; class ManagerFactory { /** + * @param InternalEntityFactory $entityFactory Factory to create entities * @return RowManager */ - public function createRowManager() + public function createRowManager($entityFactory) { - return new RowManager(); + return new RowManager($entityFactory); } } diff --git a/src/Spout/Reader/ODS/Manager/RowManager.php b/src/Spout/Reader/ODS/Manager/RowManager.php deleted file mode 100644 index d9e8970..0000000 --- a/src/Spout/Reader/ODS/Manager/RowManager.php +++ /dev/null @@ -1,29 +0,0 @@ -getCells() as $cell) { - if (!$cell->isEmpty()) { - return false; - } - } - - return true; - } -} diff --git a/src/Spout/Reader/ODS/RowIterator.php b/src/Spout/Reader/ODS/RowIterator.php index e41a14d..ce61a32 100644 --- a/src/Spout/Reader/ODS/RowIterator.php +++ b/src/Spout/Reader/ODS/RowIterator.php @@ -7,6 +7,7 @@ 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\Manager\RowManager; use Box\Spout\Reader\Common\XMLProcessor; use Box\Spout\Reader\Exception\InvalidValueException; use Box\Spout\Reader\Exception\IteratorNotRewindableException; @@ -14,7 +15,6 @@ 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; /** @@ -190,7 +190,7 @@ class RowIterator implements IteratorInterface */ protected function readDataForNextRow() { - $this->currentlyProcessedRow = $this->entityFactory->createRow([]); + $this->currentlyProcessedRow = $this->entityFactory->createRow(); try { $this->xmlProcessor->readUntilStopped(); @@ -257,7 +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()); + $numCellsInCurrentlyProcessedRow = $this->currentlyProcessedRow->getNumCells(); // 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". diff --git a/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php b/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php index db402ad..16966b2 100644 --- a/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php +++ b/src/Spout/Reader/XLSX/Creator/InternalEntityFactory.php @@ -116,7 +116,7 @@ class InternalEntityFactory implements InternalEntityFactoryInterface * @param Cell[] $cells * @return Row */ - public function createRow(array $cells) + public function createRow(array $cells = []) { return new Row($cells); } diff --git a/src/Spout/Reader/XLSX/Creator/ManagerFactory.php b/src/Spout/Reader/XLSX/Creator/ManagerFactory.php index 20207cc..d23a53f 100644 --- a/src/Spout/Reader/XLSX/Creator/ManagerFactory.php +++ b/src/Spout/Reader/XLSX/Creator/ManagerFactory.php @@ -2,7 +2,7 @@ namespace Box\Spout\Reader\XLSX\Creator; -use Box\Spout\Reader\XLSX\Manager\RowManager; +use Box\Spout\Reader\Common\Manager\RowManager; use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory; use Box\Spout\Reader\XLSX\Manager\SharedStringsManager; use Box\Spout\Reader\XLSX\Manager\SheetManager; diff --git a/src/Spout/Reader/XLSX/RowIterator.php b/src/Spout/Reader/XLSX/RowIterator.php index 5f77f2f..aaa9789 100644 --- a/src/Spout/Reader/XLSX/RowIterator.php +++ b/src/Spout/Reader/XLSX/RowIterator.php @@ -5,6 +5,7 @@ namespace Box\Spout\Reader\XLSX; use Box\Spout\Common\Exception\IOException; use Box\Spout\Reader\Common\Entity\Cell; use Box\Spout\Reader\Common\Entity\Row; +use Box\Spout\Reader\Common\Manager\RowManager; use Box\Spout\Reader\Common\XMLProcessor; use Box\Spout\Reader\Exception\InvalidValueException; use Box\Spout\Reader\Exception\XMLProcessingException; @@ -13,7 +14,6 @@ use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory; use Box\Spout\Reader\XLSX\Helper\CellHelper; use Box\Spout\Reader\XLSX\Helper\CellValueFormatter; -use Box\Spout\Reader\XLSX\Manager\RowManager; /** * Class RowIterator @@ -47,7 +47,7 @@ class RowIterator implements IteratorInterface /** @var Helper\CellValueFormatter Helper to format cell values */ protected $cellValueFormatter; - /** @var \Box\Spout\Reader\XLSX\Manager\RowManager Manages rows */ + /** @var \Box\Spout\Reader\Common\Manager\RowManager Manages rows */ protected $rowManager; /** @var \Box\Spout\Reader\XLSX\Creator\InternalEntityFactory Factory to create entities */ @@ -215,7 +215,7 @@ class RowIterator implements IteratorInterface */ protected function readDataForNextRow() { - $this->currentlyProcessedRow = $this->entityFactory->createRow([]); + $this->currentlyProcessedRow = $this->entityFactory->createRow(); try { $this->xmlProcessor->readUntilStopped(); @@ -386,7 +386,7 @@ class RowIterator implements IteratorInterface if ($this->lastRowIndexProcessed !== $this->nextRowIndexToBeProcessed) { // return empty row if mismatch between last processed row // and the row that needs to be returned - $rowToBeProcessed = $this->entityFactory->createRow([]); + $rowToBeProcessed = $this->entityFactory->createRow(); } } diff --git a/src/Spout/Writer/Common/Entity/Row.php b/src/Spout/Writer/Common/Entity/Row.php index 821c42d..e32166c 100644 --- a/src/Spout/Writer/Common/Entity/Row.php +++ b/src/Spout/Writer/Common/Entity/Row.php @@ -81,4 +81,12 @@ class Row return $this; } + + /** + * @return int + */ + public function getNumCells() + { + return count($this->cells); + } } diff --git a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php index e01e61a..06f659f 100644 --- a/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php +++ b/src/Spout/Writer/Common/Manager/WorkbookManagerAbstract.php @@ -259,7 +259,7 @@ abstract class WorkbookManagerAbstract implements WorkbookManagerInterface // update max num columns for the worksheet $currentMaxNumColumns = $worksheet->getMaxNumColumns(); - $cellsCount = count($row->getCells()); + $cellsCount = $row->getNumCells(); $worksheet->setMaxNumColumns(max($currentMaxNumColumns, $cellsCount)); } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 1dfe580..9b260b0 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -111,7 +111,6 @@ class WorksheetManager implements WorksheetManagerInterface public function addRow(Worksheet $worksheet, Row $row) { $cells = $row->getCells(); - $cellsCount = count($cells); $rowStyle = $row->getStyle(); $data = ''; @@ -119,7 +118,7 @@ class WorksheetManager implements WorksheetManagerInterface $currentCellIndex = 0; $nextCellIndex = 1; - for ($i = 0; $i < $cellsCount; $i++) { + for ($i = 0; $i < $row->getNumCells(); $i++) { /** @var Cell $cell */ $cell = $cells[$currentCellIndex]; /** @var Cell|null $nextCell */ diff --git a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php index 2995fd5..702986e 100644 --- a/src/Spout/Writer/XLSX/Manager/WorksheetManager.php +++ b/src/Spout/Writer/XLSX/Manager/WorksheetManager.php @@ -156,7 +156,7 @@ EOD; $cellIndex = 0; $rowStyle = $row->getStyle(); $rowIndex = $worksheet->getLastWrittenRowIndex() + 1; - $numCells = count($row->getCells()); + $numCells = $row->getNumCells(); $rowXML = ''; diff --git a/tests/Spout/Reader/Common/Entity/RowTest.php b/tests/Spout/Reader/Common/Entity/RowTest.php index 81a55ca..964c6e7 100644 --- a/tests/Spout/Reader/Common/Entity/RowTest.php +++ b/tests/Spout/Reader/Common/Entity/RowTest.php @@ -28,7 +28,7 @@ class RowTest extends \PHPUnit\Framework\TestCase $row = new Row([], null); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); } /** @@ -39,11 +39,11 @@ class RowTest extends \PHPUnit\Framework\TestCase $row = new Row([], null); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); $row->setCells([$this->getCellMock()]); - $this->assertEquals(1, count($row->getCells())); + $this->assertEquals(1, $row->getNumCells()); } /** @@ -53,11 +53,11 @@ class RowTest extends \PHPUnit\Framework\TestCase { $row = new Row([], null); - $this->assertEquals(0, count($row->getCells())); + $this->assertEquals(0, $row->getNumCells()); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); } /** @@ -68,11 +68,11 @@ class RowTest extends \PHPUnit\Framework\TestCase $row = new Row([], null); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); $row->addCell($this->getCellMock()); - $this->assertEquals(3, count($row->getCells())); + $this->assertEquals(3, $row->getNumCells()); } /** diff --git a/tests/Spout/Reader/XLSX/Manager/RowManagerTest.php b/tests/Spout/Reader/Common/Manager/RowManagerTest.php similarity index 98% rename from tests/Spout/Reader/XLSX/Manager/RowManagerTest.php rename to tests/Spout/Reader/Common/Manager/RowManagerTest.php index 91dd240..b17eb04 100644 --- a/tests/Spout/Reader/XLSX/Manager/RowManagerTest.php +++ b/tests/Spout/Reader/Common/Manager/RowManagerTest.php @@ -1,6 +1,6 @@ assertEquals($expectedIsEmpty, $rowManager->isEmpty($row)); - } -} diff --git a/tests/Spout/Writer/Common/Entity/RowTest.php b/tests/Spout/Writer/Common/Entity/RowTest.php index c036813..8f1859a 100644 --- a/tests/Spout/Writer/Common/Entity/RowTest.php +++ b/tests/Spout/Writer/Common/Entity/RowTest.php @@ -39,7 +39,7 @@ class RowTest extends TestCase $row = new Row([], null); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); } /** @@ -50,11 +50,11 @@ class RowTest extends TestCase $row = new Row([], null); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); $row->setCells([$this->getCellMock()]); - $this->assertEquals(1, count($row->getCells())); + $this->assertEquals(1, $row->getNumCells()); } /** @@ -64,11 +64,11 @@ class RowTest extends TestCase { $row = new Row([], null); - $this->assertEquals(0, count($row->getCells())); + $this->assertEquals(0, $row->getNumCells()); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); } /** @@ -79,11 +79,11 @@ class RowTest extends TestCase $row = new Row([], null); $row->setCells([$this->getCellMock(), $this->getCellMock()]); - $this->assertEquals(2, count($row->getCells())); + $this->assertEquals(2, $row->getNumCells()); $row->addCell($this->getCellMock()); - $this->assertEquals(3, count($row->getCells())); + $this->assertEquals(3, $row->getNumCells()); } /**