From 23de4b4117ef53bdc6ff87fd3c6b6f17460befb9 Mon Sep 17 00:00:00 2001 From: madflow Date: Sun, 30 Jul 2017 21:09:55 +0200 Subject: [PATCH] first shot an row RowManager and make CSV tests pass --- .../Writer/Common/Creator/EntityFactory.php | 13 +++++ .../Writer/Common/Creator/ManagerFactory.php | 21 +++++++ src/Spout/Writer/Common/Entity/Row.php | 19 +++---- .../Writer/Common/Manager/RowManager.php | 13 +++++ src/Spout/Writer/WriterAbstract.php | 3 +- tests/Spout/Writer/CSV/WriterTest.php | 56 +++++++++++++++---- 6 files changed, 103 insertions(+), 22 deletions(-) diff --git a/src/Spout/Writer/Common/Creator/EntityFactory.php b/src/Spout/Writer/Common/Creator/EntityFactory.php index 0e553a0..e2e9e08 100644 --- a/src/Spout/Writer/Common/Creator/EntityFactory.php +++ b/src/Spout/Writer/Common/Creator/EntityFactory.php @@ -2,7 +2,9 @@ namespace Box\Spout\Writer\Common\Creator; +use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Sheet; +use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Workbook; use Box\Spout\Writer\Common\Entity\Worksheet; @@ -55,4 +57,15 @@ class EntityFactory $sheetManager = $this->managerFactory->createSheetManager(); return new Sheet($sheetIndex, $associatedWorkbookId, $sheetManager); } + + /** + * @param array $cells + * @param Style|null $style + * @return Row + */ + public function createRow(array $cells, Style $style = null) + { + $rowManager = $this->managerFactory->createRowManager(); + return new Row($cells, $style, $rowManager); + } } \ No newline at end of file diff --git a/src/Spout/Writer/Common/Creator/ManagerFactory.php b/src/Spout/Writer/Common/Creator/ManagerFactory.php index 6aa6939..79aa0ff 100644 --- a/src/Spout/Writer/Common/Creator/ManagerFactory.php +++ b/src/Spout/Writer/Common/Creator/ManagerFactory.php @@ -3,7 +3,10 @@ namespace Box\Spout\Writer\Common\Creator; use Box\Spout\Common\Helper\StringHelper; +use Box\Spout\Writer\Common\Manager\CellManager; +use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\SheetManager; +use Box\Spout\Writer\Common\Manager\Style\StyleMerger; /** * Class ManagerFactory @@ -13,6 +16,24 @@ use Box\Spout\Writer\Common\Manager\SheetManager; */ class ManagerFactory { + /** + * @return CellManager + */ + public function createCellManager() + { + $styleMerger = new StyleMerger(); + return new CellManager($styleMerger); + } + + /** + * @return RowManager + */ + public function createRowManager() + { + $styleMerger = new StyleMerger(); + return new RowManager($styleMerger); + } + /** * @return SheetManager */ diff --git a/src/Spout/Writer/Common/Entity/Row.php b/src/Spout/Writer/Common/Entity/Row.php index bc6f94f..2e60e27 100644 --- a/src/Spout/Writer/Common/Entity/Row.php +++ b/src/Spout/Writer/Common/Entity/Row.php @@ -3,6 +3,7 @@ namespace Box\Spout\Writer\Common\Entity; use Box\Spout\Writer\Common\Entity\Style\Style; +use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\Style\StyleMerger; class Row @@ -20,22 +21,24 @@ class Row protected $style = null; /** - * @var StyleMerger + * Thw row manager + * @var RowManager */ - protected $styleMerger; + protected $rowManager; /** * Row constructor. * @param Cell[] $cells * @param Style|null $style + * @param RowManager $rowManager */ - public function __construct(array $cells = [], Style $style = null) + public function __construct(array $cells = [], Style $style = null, RowManager $rowManager) { $this ->setCells($cells) ->setStyle($style); - $this->styleMerger = new StyleMerger(); + $this->rowManager = $rowManager; } /** @@ -86,11 +89,7 @@ class Row */ public function applyStyle(Style $style = null) { - if ($style === null) { - return $this; - } - $merged = $this->styleMerger->merge($this->getStyle(), $style); - $this->setStyle($merged); + $this->rowManager->applyStyle($this, $style); return $this; } @@ -112,6 +111,6 @@ class Row */ public function isEmpty() { - return count($this->cells) === 0 || (count($this->cells) === 1 && $this->cells[0]->isEmpty()); + return $this->rowManager->isEmpty($this); } } diff --git a/src/Spout/Writer/Common/Manager/RowManager.php b/src/Spout/Writer/Common/Manager/RowManager.php index 49c6e9d..ede6157 100644 --- a/src/Spout/Writer/Common/Manager/RowManager.php +++ b/src/Spout/Writer/Common/Manager/RowManager.php @@ -31,4 +31,17 @@ class RowManager $mergedStyle = $this->styleMerger->merge($row->getStyle(), $style); $row->setStyle($mergedStyle); } + + /** + * Detect whether a row is considered empty. + * An empty row has either no cells at all - or only one empty cell + * + * @param Row $row + * @return bool + */ + public function isEmpty(Row $row) + { + $cells = $row->getCells(); + return count($cells) === 0 || (count($cells) === 1 && $cells[0]->isEmpty()); + } } \ No newline at end of file diff --git a/src/Spout/Writer/WriterAbstract.php b/src/Spout/Writer/WriterAbstract.php index 47d3a64..e2590af 100644 --- a/src/Spout/Writer/WriterAbstract.php +++ b/src/Spout/Writer/WriterAbstract.php @@ -54,7 +54,8 @@ abstract class WriterAbstract implements WriterInterface OptionsManagerInterface $optionsManager, StyleMerger $styleMerger, GlobalFunctionsHelper $globalFunctionsHelper - ) { + ) + { $this->optionsManager = $optionsManager; $this->styleMerger = $styleMerger; $this->globalFunctionsHelper = $globalFunctionsHelper; diff --git a/tests/Spout/Writer/CSV/WriterTest.php b/tests/Spout/Writer/CSV/WriterTest.php index 4fa7bfc..522d455 100644 --- a/tests/Spout/Writer/CSV/WriterTest.php +++ b/tests/Spout/Writer/CSV/WriterTest.php @@ -5,6 +5,8 @@ namespace Box\Spout\Writer\CSV; use Box\Spout\TestUsingResource; use Box\Spout\Common\Type; use Box\Spout\Common\Helper\EncodingHelper; +use Box\Spout\Writer\Common\Creator\EntityFactory; +use Box\Spout\Writer\Common\Creator\ManagerFactory; use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\WriterFactory; @@ -17,6 +19,17 @@ class WriterTest extends \PHPUnit_Framework_TestCase { use TestUsingResource; + /** + * @var EntityFactory + */ + protected $entityFactory; + + protected function setUp() + { + $this->entityFactory = new EntityFactory(new ManagerFactory()); + parent::setUp(); + } + /** * @expectedException \Box\Spout\Common\Exception\IOException */ @@ -28,7 +41,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase $writer = WriterFactory::create(Type::CSV); @$writer->openToFile($filePath); - $writer->addRow(['csv--11', 'csv--12']); + $row = $this->entityFactory->createRow([ + new Cell('csv--11'), + new Cell('csv--12') + ]); + $writer->addRow($row); $writer->close(); } @@ -38,7 +55,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase public function testWriteShouldThrowExceptionIfCallAddRowBeforeOpeningWriter() { $writer = WriterFactory::create(Type::CSV); - $writer->addRow(['csv--11', 'csv--12']); + $row = $this->entityFactory->createRow([ + new Cell('csv--11'), + new Cell('csv--12') + ]); + $writer->addRow($row); $writer->close(); } @@ -48,17 +69,24 @@ class WriterTest extends \PHPUnit_Framework_TestCase public function testWriteShouldThrowExceptionIfCallAddRowsBeforeOpeningWriter() { $writer = WriterFactory::create(Type::CSV); - $writer->addRows([['csv--11', 'csv--12']]); + $row = $this->entityFactory->createRow([ + new Cell('csv--11'), + new Cell('csv--12') + ]); + $writer->addRows([$row]); $writer->close(); } - /** - * @expectedException \Box\Spout\Common\Exception\InvalidArgumentException - */ - public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfArrays() + public function testAddRowsShouldThrowExceptionIfRowsAreNotArrayOfRows() { + if (version_compare(PHP_VERSION, '7.0.0') >= 0) { + $this->expectException(\TypeError::class); + } else { + $this->markTestSkipped('PHP > 7.0 only'); + } $writer = WriterFactory::create(Type::CSV); - $writer->addRows(['csv--11', 'csv--12']); + $row = new \stdClass(); + $writer->addRows([$row]); $writer->close(); } @@ -192,11 +220,11 @@ class WriterTest extends \PHPUnit_Framework_TestCase } /** - * @param array $allRows + * @param array $allRows * @param string $fileName * @param string $fieldDelimiter * @param string $fieldEnclosure - * @param bool $shouldAddBOM + * @param bool $shouldAddBOM * @return null|string */ private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true) @@ -211,7 +239,13 @@ class WriterTest extends \PHPUnit_Framework_TestCase $writer->setShouldAddBOM($shouldAddBOM); $writer->openToFile($resourcePath); - $writer->addRows($allRows); + + $writer->addRows(array_map(function ($oneRow) { + $row = $this->entityFactory->createRow(array_map(function ($value) { + return new Cell($value); + }, $oneRow)); + return $row; + }, $allRows)); $writer->close(); return file_get_contents($resourcePath);