Move ReaderFactory into Common/Creator

This commit is contained in:
Adrien Loison 2018-06-03 20:05:29 +02:00 committed by Adrien Loison
parent d25a4ebd6d
commit 1b64a06fbe
4 changed files with 90 additions and 77 deletions

View File

@ -2,7 +2,6 @@
namespace Box\Spout\Reader\Common\Creator; namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Reader\ReaderInterface; use Box\Spout\Reader\ReaderInterface;
/** /**

View File

@ -0,0 +1,89 @@
<?php
namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory as CSVInternalEntityFactory;
use Box\Spout\Reader\CSV\Manager\OptionsManager as CSVOptionsManager;
use Box\Spout\Reader\CSV\Reader as CSVReader;
use Box\Spout\Reader\ODS\Creator\HelperFactory as ODSHelperFactory;
use Box\Spout\Reader\ODS\Creator\InternalEntityFactory as ODSInternalEntityFactory;
use Box\Spout\Reader\ODS\Creator\ManagerFactory as ODSManagerFactory;
use Box\Spout\Reader\ODS\Manager\OptionsManager as ODSOptionsManager;
use Box\Spout\Reader\ODS\Reader as ODSReader;
use Box\Spout\Reader\ReaderInterface;
use Box\Spout\Reader\XLSX\Creator\HelperFactory as XLSXHelperFactory;
use Box\Spout\Reader\XLSX\Creator\InternalEntityFactory as XLSXInternalEntityFactory;
use Box\Spout\Reader\XLSX\Creator\ManagerFactory as XLSXManagerFactory;
use Box\Spout\Reader\XLSX\Manager\OptionsManager as XLSXOptionsManager;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
use Box\Spout\Reader\XLSX\Reader as XLSXReader;
/**
* Class ReaderFactory
* This factory is used to create readers, based on the type of the file to be read.
* It supports CSV, XLSX and ODS formats.
*/
class ReaderFactory
{
/**
* This creates an instance of the appropriate reader, given the type of the file to be read
*
* @param string $readerType Type of the reader to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function create($readerType)
{
switch ($readerType) {
case Type::CSV: return self::getCSVReader();
case Type::XLSX: return self::getXLSXReader();
case Type::ODS: return self::getODSReader();
default:
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
}
}
/**
* @return CSVReader
*/
private static function getCSVReader()
{
$optionsManager = new CSVOptionsManager();
$helperFactory = new HelperFactory();
$entityFactory = new CSVInternalEntityFactory($helperFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new CSVReader($optionsManager, $globalFunctionsHelper, $entityFactory);
}
/**
* @return XLSXReader
*/
private static function getXLSXReader()
{
$optionsManager = new XLSXOptionsManager();
$helperFactory = new XLSXHelperFactory();
$managerFactory = new XLSXManagerFactory($helperFactory, new CachingStrategyFactory());
$entityFactory = new XLSXInternalEntityFactory($managerFactory, $helperFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new XLSXReader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
}
/**
* @return ODSReader
*/
private static function getODSReader()
{
$optionsManager = new ODSOptionsManager();
$helperFactory = new ODSHelperFactory();
$managerFactory = new ODSManagerFactory();
$entityFactory = new ODSInternalEntityFactory($helperFactory, $managerFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new ODSReader($optionsManager, $globalFunctionsHelper, $entityFactory);
}
}

View File

@ -1,75 +0,0 @@
<?php
namespace Box\Spout\Reader;
use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\XLSX\Manager\SharedStringsCaching\CachingStrategyFactory;
/**
* Class ReaderFactory
* This factory is used to create readers, based on the type of the file to be read.
* It supports CSV and XLSX formats.
*/
class ReaderFactory
{
/**
* This creates an instance of the appropriate reader, given the type of the file to be read
*
* @param string $readerType Type of the reader to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function create($readerType)
{
switch ($readerType) {
case Type::CSV: return self::getCSVReader();
case Type::XLSX: return self::getXLSXReader();
case Type::ODS: return self::getODSReader();
default:
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
}
}
/**
* @return CSV\Reader
*/
private static function getCSVReader()
{
$optionsManager = new CSV\Manager\OptionsManager();
$helperFactory = new HelperFactory();
$entityFactory = new CSV\Creator\InternalEntityFactory($helperFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new CSV\Reader($optionsManager, $globalFunctionsHelper, $entityFactory);
}
/**
* @return XLSX\Reader
*/
private static function getXLSXReader()
{
$optionsManager = new XLSX\Manager\OptionsManager();
$helperFactory = new XLSX\Creator\HelperFactory();
$managerFactory = new XLSX\Creator\ManagerFactory($helperFactory, new CachingStrategyFactory());
$entityFactory = new XLSX\Creator\InternalEntityFactory($managerFactory, $helperFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new XLSX\Reader($optionsManager, $globalFunctionsHelper, $entityFactory, $managerFactory);
}
/**
* @return ODS\Reader
*/
private static function getODSReader()
{
$optionsManager = new ODS\Manager\OptionsManager();
$helperFactory = new ODS\Creator\HelperFactory();
$managerFactory = new ODS\Creator\ManagerFactory();
$entityFactory = new ODS\Creator\InternalEntityFactory($helperFactory, $managerFactory);
$globalFunctionsHelper = $helperFactory->createGlobalFunctionsHelper();
return new ODS\Reader($optionsManager, $globalFunctionsHelper, $entityFactory);
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Box\Spout\Reader; namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Exception\UnsupportedTypeException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;