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 committed 2017-11-05 13:18:29 +01:00
1 parent 2d2151ac8d
commit 3d0f108b1d
12 files changed
+201 -170

No files matched your search

@@ -7,6 +7,7 @@ use Box\Spout\Writer\Common\Entity\Row;
use Box\Spout\Writer\Common\Entity\Style\Style;
use Box\Spout\Writer\Common\Manager\RowManager;
use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
use Box\Spout\Writer\WriterInterface;
/**
* Class EntityFactory
@@ -15,17 +16,19 @@ use Box\Spout\Writer\Common\Manager\Style\StyleMerger;
class EntityFactory
{
/**
* @param mixed $cellValue
* @param Style|null $cellStyle
* @return Cell
* 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 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
* @return Row
*/
@@ -36,4 +39,31 @@ class EntityFactory
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);
}
}
@@ -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);
}
}
-83
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);
}
}