Add helper functions to create specific readers and writers

Removed the `ReaderEntityFactory::createReader(Type)` method and replaced it by 3 methods:
- `ReaderEntityFactory::createCSVReader()`
- `ReaderEntityFactory::createXLSXReader()`
- `ReaderEntityFactory::createODSReader()`

This has the advantage of enabling autocomplete in the IDE, as the return type is no longer the interface but the concrete type. Since readers may expose different options, this is pretty useful.

Similarly, removed the `WriterEntityFactory::createWriter(Type)` method and replaced it by 3 methods:
- `WriterEntityFactory::createCSVWriter()`
- `WriterEntityFactory::createXLSXWriter()`
- `WriterEntityFactory::createODSWriter()`

Since this is a breaking change, I also updated the Upgrade guide.
Finally, the doc is up to date too.
This commit is contained in:
Adrien Loison authored and Adrien Loison committed 2019-05-17 21:22:03 +02:00
1 parent 6104d41857
commit 40ee386edd
30 files changed
+235 -275

No files matched your search

+4 -9
View File
@@ -2,7 +2,6 @@
namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Common\Entity\Sheet;
@@ -65,8 +64,7 @@ class SheetTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$customSheetName = 'Sheet name';
@@ -104,8 +102,7 @@ class SheetTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$sheet = $writer->getCurrentSheet();
@@ -126,8 +123,7 @@ class SheetTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$writer->addRow($this->createRowFromValues(['xlsx--sheet1--11', 'xlsx--sheet1--12']));
@@ -148,8 +144,7 @@ class SheetTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$sheet = $writer->getCurrentSheet();
+1 -3
View File
@@ -2,7 +2,6 @@
namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use PHPUnit\Framework\TestCase;
@@ -52,8 +51,7 @@ class WriterPerfTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setShouldUseInlineStrings($shouldUseInlineStrings);
$writer->setShouldCreateNewSheetsAutomatically(true);
+13 -23
View File
@@ -6,7 +6,6 @@ use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Exception\InvalidArgumentException;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
@@ -34,7 +33,7 @@ class WriterTest extends TestCase
$this->createUnwritableFolderIfNeeded();
$filePath = $this->getGeneratedUnwritableResourcePath($fileName);
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
@$writer->openToFile($filePath);
}
@@ -45,7 +44,7 @@ class WriterTest extends TestCase
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
}
@@ -56,7 +55,7 @@ class WriterTest extends TestCase
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->addRows($this->createRowsFromValues([['xlsx--11', 'xlsx--12']]));
}
@@ -70,8 +69,7 @@ class WriterTest extends TestCase
$fileName = 'file_that_wont_be_written.xlsx';
$filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($filePath);
$writer->setTempFolder('');
@@ -87,8 +85,7 @@ class WriterTest extends TestCase
$fileName = 'file_that_wont_be_written.xlsx';
$filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($filePath);
$writer->setShouldUseInlineStrings(true);
@@ -104,8 +101,7 @@ class WriterTest extends TestCase
$fileName = 'file_that_wont_be_written.xlsx';
$filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($filePath);
$writer->setShouldCreateNewSheetsAutomatically(true);
@@ -158,8 +154,7 @@ class WriterTest extends TestCase
$this->recreateTempFolder();
$tempFolderPath = $this->getTempFolderPath();
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setTempFolder($tempFolderPath);
$writer->openToFile($resourcePath);
@@ -183,8 +178,7 @@ class WriterTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent();
$writer->close();
@@ -203,8 +197,7 @@ class WriterTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent();
@@ -227,7 +220,7 @@ class WriterTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->close(); // This call should not cause any error
$writer->openToFile($resourcePath);
@@ -404,8 +397,7 @@ class WriterTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setShouldUseInlineStrings(true);
$writer->openToFile($resourcePath);
@@ -544,8 +536,7 @@ class WriterTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setShouldUseInlineStrings($shouldUseInlineStrings);
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
@@ -569,8 +560,7 @@ class WriterTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setShouldUseInlineStrings($shouldUseInlineStrings);
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
@@ -2,12 +2,10 @@
namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Entity\Cell;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Entity\Style\Border;
use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
@@ -45,7 +43,7 @@ class WriterWithStyleTest extends TestCase
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle));
}
@@ -56,7 +54,7 @@ class WriterWithStyleTest extends TestCase
{
$this->expectException(WriterNotOpenedException::class);
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle));
}
@@ -518,8 +516,7 @@ class WriterWithStyleTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setShouldUseInlineStrings(true);
$writer->openToFile($resourcePath);
@@ -540,8 +537,7 @@ class WriterWithStyleTest extends TestCase
$this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterEntityFactory::createWriter(Type::XLSX);
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setShouldUseInlineStrings(true);
$writer->setDefaultRowStyle($defaultStyle);