Consolidate external EntityFactory

All entities will now be created through a single factory (including the Writers).
Also, added a EntityFactory::createRowFromArray() to make it easier to create rows
This commit is contained in:
Adrien Loison 2017-11-05 13:06:57 +01:00
parent 2d2151ac8d
commit ec5335cc23
12 changed files with 201 additions and 170 deletions

View File

@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\RowManager; use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\WriterInterface;
/** /**
* Class EntityFactory * Class EntityFactory
@ -15,17 +16,19 @@ use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
class EntityFactory class EntityFactory
{ {
/** /**
* @param mixed $cellValue * This creates an instance of the appropriate writer, given the type of the file to be read
* @param Style|null $cellStyle *
* @return Cell * @param string $writerType Type of the writer to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/ */
public static function createCell($cellValue, Style $cellStyle = null) public static function createWriter($writerType)
{ {
return new Cell($cellValue, $cellStyle); return (new WriterFactory())->create($writerType);
} }
/** /**
* @param array $cells * @param Cell[] $cells
* @param Style|null $rowStyle * @param Style|null $rowStyle
* @return Row * @return Row
*/ */
@ -36,4 +39,31 @@ class EntityFactory
return new Row($cells, $rowStyle, $rowManager); return new Row($cells, $rowStyle, $rowManager);
} }
/**
* @param array $cellValues
* @param Style|null $rowStyle
* @return Row
*/
public static function createRowFromArray(array $cellValues = [], Style $rowStyle = null)
{
$styleMerger = new StyleMerger();
$rowManager = new RowManager($styleMerger);
$cells = array_map(function ($cellValue) {
return new Cell($cellValue);
}, $cellValues);
return new Row($cells, $rowStyle, $rowManager);
}
/**
* @param mixed $cellValue
* @param Style|null $cellStyle
* @return Cell
*/
public static function createCell($cellValue, Style $cellStyle = null)
{
return new Cell($cellValue, $cellStyle);
}
} }

View File

@ -0,0 +1,93 @@
<?php
namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager;
use Box\Spout\Writer\CSV\Writer as CSVWriter;
use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory;
use Box\Spout\Writer\ODS\Creator\ManagerFactory as ODSManagerFactory;
use Box\Spout\Writer\ODS\Manager\OptionsManager as ODSOptionsManager;
use Box\Spout\Writer\ODS\Writer as ODSWriter;
use Box\Spout\Writer\WriterInterface;
use Box\Spout\Writer\XLSX\Creator\HelperFactory as XLSXHelperFactory;
use Box\Spout\Writer\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
use Box\Spout\Writer\XLSX\Manager\OptionsManager as XLSXOptionsManager;
use Box\Spout\Writer\XLSX\Writer as XLSXWriter;
/**
* Class WriterFactory
* This factory is used to create writers, based on the type of the file to be read.
* It supports CSV, XLSX and ODS formats.
*/
class WriterFactory
{
/**
* This creates an instance of the appropriate writer, given the type of the file to be read
*
* @param string $writerType Type of the writer to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/
public function create($writerType)
{
switch ($writerType) {
case Type::CSV: return $this->getCSVWriter();
case Type::XLSX: return $this->getXLSXWriter();
case Type::ODS: return $this->getODSWriter();
default:
throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
}
}
/**
* @return CSVWriter
*/
private function getCSVWriter()
{
$optionsManager = new CSVOptionsManager();
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new HelperFactory();
return new CSVWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory);
}
/**
* @return XLSXWriter
*/
private function getXLSXWriter()
{
$styleBuilder = new StyleBuilder();
$optionsManager = new XLSXOptionsManager($styleBuilder);
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new XLSXHelperFactory();
$managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory);
return new XLSXWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
}
/**
* @return ODSWriter
*/
private function getODSWriter()
{
$styleBuilder = new StyleBuilder();
$optionsManager = new ODSOptionsManager($styleBuilder);
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new ODSHelperFactory();
$managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory);
return new ODSWriter($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
}
}

View File

@ -1,83 +0,0 @@
<?php
namespace Box\Spout\Writer;
use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type;
use Box\Spout\Writer\Common\Creator\InternalEntityFactory;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
/**
* Class WriterFactory
* This factory is used to create writers, based on the type of the file to be read.
* It supports CSV, XLSX and ODS formats.
*/
class WriterFactory
{
/**
* This creates an instance of the appropriate writer, given the type of the file to be read
*
* @param string $writerType Type of the writer to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/
public static function create($writerType)
{
switch ($writerType) {
case Type::CSV: return self::getCSVWriter();
case Type::XLSX: return self::getXLSXWriter();
case Type::ODS: return self::getODSWriter();
default:
throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType);
}
}
/**
* @return CSV\Writer
*/
private static function getCSVWriter()
{
$optionsManager = new CSV\Manager\OptionsManager();
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new HelperFactory();
return new CSV\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory);
}
/**
* @return XLSX\Writer
*/
private static function getXLSXWriter()
{
$styleBuilder = new StyleBuilder();
$optionsManager = new XLSX\Manager\OptionsManager($styleBuilder);
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new XLSX\Creator\HelperFactory();
$managerFactory = new XLSX\Creator\ManagerFactory(new InternalEntityFactory(), $helperFactory);
return new XLSX\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
}
/**
* @return ODS\Writer
*/
private static function getODSWriter()
{
$styleBuilder = new StyleBuilder();
$optionsManager = new ODS\Manager\OptionsManager($styleBuilder);
$styleMerger = new StyleMerger();
$globalFunctionsHelper = new GlobalFunctionsHelper();
$helperFactory = new ODS\Creator\HelperFactory();
$managerFactory = new ODS\Creator\ManagerFactory(new InternalEntityFactory(), $helperFactory);
return new ODS\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory);
}
}

View File

@ -7,10 +7,10 @@ use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Helper\EncodingHelper; use Box\Spout\Common\Helper\EncodingHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
/** /**
* Class WriterTest * Class WriterTest
@ -31,7 +31,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createUnwritableFolderIfNeeded(); $this->createUnwritableFolderIfNeeded();
$filePath = $this->getGeneratedUnwritableResourcePath($fileName); $filePath = $this->getGeneratedUnwritableResourcePath($fileName);
$writer = WriterFactory::create(Type::CSV); $writer = EntityFactory::createWriter(Type::CSV);
@$writer->openToFile($filePath); @$writer->openToFile($filePath);
$writer->addRow($this->createRowFromValues(['csv--11', 'csv--12'])); $writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close(); $writer->close();
@ -44,7 +44,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::CSV); $writer = EntityFactory::createWriter(Type::CSV);
$writer->addRow($this->createRowFromValues(['csv--11', 'csv--12'])); $writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close(); $writer->close();
} }
@ -56,7 +56,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::CSV); $writer = EntityFactory::createWriter(Type::CSV);
$writer->addRow($this->createRowFromValues(['csv--11', 'csv--12'])); $writer->addRow($this->createRowFromValues(['csv--11', 'csv--12']));
$writer->close(); $writer->close();
} }
@ -68,7 +68,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
$writer = WriterFactory::create(Type::CSV); $writer = EntityFactory::createWriter(Type::CSV);
$writer->addRows([['csv--11', 'csv--12']]); $writer->addRows([['csv--11', 'csv--12']]);
$writer->close(); $writer->close();
} }
@ -82,7 +82,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::CSV); $writer = EntityFactory::createWriter(Type::CSV);
$writer->close(); // This call should not cause any error $writer->close(); // This call should not cause any error
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -189,7 +189,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\CSV\Writer $writer */ /** @var \Box\Spout\Writer\CSV\Writer $writer */
$writer = WriterFactory::create(Type::CSV); $writer = EntityFactory::createWriter(Type::CSV);
$writer->setFieldDelimiter($fieldDelimiter); $writer->setFieldDelimiter($fieldDelimiter);
$writer->setFieldEnclosure($fieldEnclosure); $writer->setFieldEnclosure($fieldEnclosure);
$writer->setShouldAddBOM($shouldAddBOM); $writer->setShouldAddBOM($shouldAddBOM);

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Box\Spout\Writer; namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Exception\UnsupportedTypeException;
@ -16,6 +16,6 @@ class WriterFactoryTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(UnsupportedTypeException::class); $this->expectException(UnsupportedTypeException::class);
WriterFactory::create('unsupportedType'); (new WriterFactory())->create('unsupportedType');
} }
} }

View File

@ -4,10 +4,10 @@ namespace Box\Spout\Writer\ODS;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Exception\InvalidSheetNameException; use Box\Spout\Writer\Exception\InvalidSheetNameException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
/** /**
* Class SheetTest * Class SheetTest
@ -65,7 +65,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$customSheetName = 'Sheet name'; $customSheetName = 'Sheet name';
@ -89,7 +89,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$sheet = $writer->getCurrentSheet(); $sheet = $writer->getCurrentSheet();
@ -109,7 +109,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRow($this->createRowFromValues(['ods--sheet1--11', 'ods--sheet1--12'])); $writer->addRow($this->createRowFromValues(['ods--sheet1--11', 'ods--sheet1--12']));

View File

@ -8,13 +8,12 @@ use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Entity\Cell; use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Helper\ZipHelper; use Box\Spout\Writer\Common\Helper\ZipHelper;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
/** /**
* Class WriterTest * Class WriterTest
@ -35,7 +34,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createUnwritableFolderIfNeeded(); $this->createUnwritableFolderIfNeeded();
$filePath = $this->getGeneratedUnwritableResourcePath($fileName); $filePath = $this->getGeneratedUnwritableResourcePath($fileName);
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
@$writer->openToFile($filePath); @$writer->openToFile($filePath);
} }
@ -46,7 +45,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->addRow($this->createRowFromValues(['ods--11', 'ods--12'])); $writer->addRow($this->createRowFromValues(['ods--11', 'ods--12']));
} }
@ -57,7 +56,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->addRows([$this->createRowFromValues(['ods--11', 'ods--12'])]); $writer->addRows([$this->createRowFromValues(['ods--11', 'ods--12'])]);
} }
@ -72,7 +71,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$filePath = $this->getGeneratedResourcePath($fileName); $filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($filePath); $writer->openToFile($filePath);
$writer->setTempFolder(''); $writer->setTempFolder('');
@ -89,7 +88,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$filePath = $this->getGeneratedResourcePath($fileName); $filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($filePath); $writer->openToFile($filePath);
$writer->setShouldCreateNewSheetsAutomatically(true); $writer->setShouldCreateNewSheetsAutomatically(true);
@ -128,7 +127,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$tempFolderPath = $this->getTempFolderPath(); $tempFolderPath = $this->getTempFolderPath();
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->setTempFolder($tempFolderPath); $writer->setTempFolder($tempFolderPath);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -153,7 +152,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var Writer $writer */ /** @var Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$writer->close(); $writer->close();
@ -173,7 +172,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var Writer $writer */ /** @var Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
@ -196,7 +195,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->close(); // This call should not cause any error $writer->close(); // This call should not cause any error
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -350,7 +349,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows($dataRowsSheet1); $writer->addRows($dataRowsSheet1);
@ -495,7 +494,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically); $writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -518,7 +517,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically); $writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer\ODS;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
@ -13,7 +14,6 @@ use Box\Spout\Writer\Common\Entity\Style\Color;
use Box\Spout\Writer\Common\Entity\Style\Style; use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
/** /**
* Class WriterWithStyleTest * Class WriterWithStyleTest
@ -41,7 +41,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->addRow($this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle)); $writer->addRow($this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle));
} }
@ -52,7 +52,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->addRow($this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle)); $writer->addRow($this->createStyledRowFromValues(['ods--11', 'ods--12'], $this->defaultStyle));
} }
@ -77,8 +77,8 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
->build(); ->build();
$dataRows = [ $dataRows = [
$this->createRowFromValues(['ods--11', 'ods--12'], $style), $this->createStyledRowFromValues(['ods--11', 'ods--12'], $style),
$this->createRowFromValues(['ods--21', 'ods--22'], $style2), $this->createStyledRowFromValues(['ods--21', 'ods--22'], $style2),
]; ];
$this->writeToODSFile($dataRows, $fileName); $this->writeToODSFile($dataRows, $fileName);
@ -320,7 +320,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows($allRows); $writer->addRows($allRows);
@ -341,7 +341,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\ODS\Writer $writer */ /** @var \Box\Spout\Writer\ODS\Writer $writer */
$writer = WriterFactory::create(Type::ODS); $writer = EntityFactory::createWriter(Type::ODS);
$writer->setDefaultRowStyle($defaultStyle); $writer->setDefaultRowStyle($defaultStyle);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);

View File

@ -13,28 +13,30 @@ trait RowCreationHelper
{ {
/** /**
* @param array $cellValues * @param array $cellValues
* @param Style|null $rowStyle
* @return Row * @return Row
*/ */
protected function createRowFromValues(array $cellValues, Style $rowStyle = null) protected function createRowFromValues(array $cellValues)
{ {
$row = EntityFactory::createRow([], $rowStyle); return $this->createStyledRowFromValues($cellValues, null);
foreach ($cellValues as $cellValue) {
$row->addCell(EntityFactory::createCell($cellValue));
}
return $row;
} }
/** /**
* @param array $cellValues * @param array $cellValues
* @param Style $rowStyle * @param Style|null $rowStyle
* @return Row * @return Row
*/ */
protected function createStyledRowFromValues(array $cellValues, Style $rowStyle) protected function createStyledRowFromValues(array $cellValues, Style $rowStyle = null)
{ {
return $this->createRowFromValues($cellValues, $rowStyle); return EntityFactory::createRowFromArray($cellValues, $rowStyle);
}
/**
* @param array $rowValues
* @return Row[]
*/
protected function createRowsFromValues(array $rowValues)
{
return $this->createStyledRowsFromValues($rowValues, null);
} }
/** /**
@ -42,24 +44,14 @@ trait RowCreationHelper
* @param Style|null $rowsStyle * @param Style|null $rowsStyle
* @return Row[] * @return Row[]
*/ */
protected function createRowsFromValues(array $rowValues, Style $rowsStyle = null) protected function createStyledRowsFromValues(array $rowValues, Style $rowsStyle = null)
{ {
$rows = []; $rows = [];
foreach ($rowValues as $cellValues) { foreach ($rowValues as $cellValues) {
$rows[] = $this->createRowFromValues($cellValues, $rowsStyle); $rows[] = $this->createStyledRowFromValues($cellValues, $rowsStyle);
} }
return $rows; return $rows;
} }
/**
* @param array $rowValues
* @param Style $rowsStyle
* @return Row[]
*/
protected function createStyledRowsFromValues(array $rowValues, Style $rowsStyle)
{
return $this->createRowsFromValues($rowValues, $rowsStyle);
}
} }

View File

@ -4,10 +4,10 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Sheet; use Box\Spout\Writer\Common\Entity\Sheet;
use Box\Spout\Writer\Exception\InvalidSheetNameException; use Box\Spout\Writer\Exception\InvalidSheetNameException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
/** /**
* Class SheetTest * Class SheetTest
@ -65,7 +65,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$customSheetName = 'Sheet name'; $customSheetName = 'Sheet name';
@ -89,7 +89,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$sheet = $writer->getCurrentSheet(); $sheet = $writer->getCurrentSheet();
@ -111,7 +111,7 @@ class SheetTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRow($this->createRowFromValues(['xlsx--sheet1--11', 'xlsx--sheet1--12'])); $writer->addRow($this->createRowFromValues(['xlsx--sheet1--11', 'xlsx--sheet1--12']));

View File

@ -7,11 +7,11 @@ use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\SpoutException; use Box\Spout\Common\Exception\SpoutException;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; use Box\Spout\Writer\Exception\WriterAlreadyOpenedException;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\XLSX\Manager\WorksheetManager; use Box\Spout\Writer\XLSX\Manager\WorksheetManager;
/** /**
@ -33,7 +33,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createUnwritableFolderIfNeeded(); $this->createUnwritableFolderIfNeeded();
$filePath = $this->getGeneratedUnwritableResourcePath($fileName); $filePath = $this->getGeneratedUnwritableResourcePath($fileName);
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
@$writer->openToFile($filePath); @$writer->openToFile($filePath);
} }
@ -44,7 +44,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12'])); $writer->addRow($this->createRowFromValues(['xlsx--11', 'xlsx--12']));
} }
@ -55,7 +55,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->addRows($this->createRowsFromValues([['xlsx--11', 'xlsx--12']])); $writer->addRows($this->createRowsFromValues([['xlsx--11', 'xlsx--12']]));
} }
@ -70,7 +70,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$filePath = $this->getGeneratedResourcePath($fileName); $filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($filePath); $writer->openToFile($filePath);
$writer->setTempFolder(''); $writer->setTempFolder('');
@ -87,7 +87,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$filePath = $this->getGeneratedResourcePath($fileName); $filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($filePath); $writer->openToFile($filePath);
$writer->setShouldUseInlineStrings(true); $writer->setShouldUseInlineStrings(true);
@ -104,7 +104,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$filePath = $this->getGeneratedResourcePath($fileName); $filePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($filePath); $writer->openToFile($filePath);
$writer->setShouldCreateNewSheetsAutomatically(true); $writer->setShouldCreateNewSheetsAutomatically(true);
@ -158,7 +158,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$tempFolderPath = $this->getTempFolderPath(); $tempFolderPath = $this->getTempFolderPath();
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->setTempFolder($tempFolderPath); $writer->setTempFolder($tempFolderPath);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -183,7 +183,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
$writer->close(); $writer->close();
@ -203,7 +203,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addNewSheetAndMakeItCurrent(); $writer->addNewSheetAndMakeItCurrent();
@ -226,7 +226,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->close(); // This call should not cause any error $writer->close(); // This call should not cause any error
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -403,7 +403,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->setShouldUseInlineStrings(true); $writer->setShouldUseInlineStrings(true);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -543,7 +543,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->setShouldUseInlineStrings($shouldUseInlineStrings); $writer->setShouldUseInlineStrings($shouldUseInlineStrings);
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically); $writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);
@ -568,7 +568,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->setShouldUseInlineStrings($shouldUseInlineStrings); $writer->setShouldUseInlineStrings($shouldUseInlineStrings);
$writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically); $writer->setShouldCreateNewSheetsAutomatically($shouldCreateSheetsAutomatically);

View File

@ -5,6 +5,7 @@ namespace Box\Spout\Writer\XLSX;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
use Box\Spout\Reader\Wrapper\XMLReader; use Box\Spout\Reader\Wrapper\XMLReader;
use Box\Spout\TestUsingResource; use Box\Spout\TestUsingResource;
use Box\Spout\Writer\Common\Creator\EntityFactory;
use Box\Spout\Writer\Common\Creator\Style\BorderBuilder; use Box\Spout\Writer\Common\Creator\Style\BorderBuilder;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Entity\Row; use Box\Spout\Writer\Common\Entity\Row;
@ -14,7 +15,6 @@ use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\Exception\WriterNotOpenedException; use Box\Spout\Writer\Exception\WriterNotOpenedException;
use Box\Spout\Writer\RowCreationHelper; use Box\Spout\Writer\RowCreationHelper;
use Box\Spout\Writer\WriterFactory;
use Box\Spout\Writer\XLSX\Manager\OptionsManager; use Box\Spout\Writer\XLSX\Manager\OptionsManager;
/** /**
@ -43,7 +43,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle)); $writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle));
} }
@ -54,7 +54,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
{ {
$this->expectException(WriterNotOpenedException::class); $this->expectException(WriterNotOpenedException::class);
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle)); $writer->addRow($this->createStyledRowFromValues(['xlsx--11', 'xlsx--12'], $this->defaultStyle));
} }
@ -491,7 +491,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->setShouldUseInlineStrings(true); $writer->setShouldUseInlineStrings(true);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
@ -513,7 +513,7 @@ class WriterWithStyleTest extends \PHPUnit_Framework_TestCase
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
/** @var \Box\Spout\Writer\XLSX\Writer $writer */ /** @var \Box\Spout\Writer\XLSX\Writer $writer */
$writer = WriterFactory::create(Type::XLSX); $writer = EntityFactory::createWriter(Type::XLSX);
$writer->setShouldUseInlineStrings(true); $writer->setShouldUseInlineStrings(true);
$writer->setDefaultRowStyle($defaultStyle); $writer->setDefaultRowStyle($defaultStyle);