Update Reader/WriterEntityFactory

Add `WriterEntityFactory::createWriterFromFile`, working like `ReaderEntityFactory::createReaderFromFile` (guessing writer type from file name).
Use static functions when needed.
This commit is contained in:
Adrien Loison 2019-05-17 13:13:58 +02:00 committed by Adrien Loison
parent e8693834a0
commit 4a9d0398ad
6 changed files with 124 additions and 19 deletions

View File

@ -13,25 +13,25 @@ class ReaderEntityFactory
/**
* 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
* @param string $readerType Type of the reader to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function createReader($readerType)
{
return (new ReaderFactory())->create($readerType);
return ReaderFactory::create($readerType);
}
/**
* Creates a reader by file extension
*
* @param string The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
* @param string $path The path to the spreadsheet file. Supported extensions are .csv, .ods and .xlsx
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function createReaderFromFile(string $path)
{
return (new ReaderFactory())->createFromFile($path);
return ReaderFactory::createFromFile($path);
}
}

View File

@ -60,21 +60,21 @@ class ReaderFactory
/**
* Creates a reader by file extension
*
* @param string The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
* @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function createFromFile(string $path)
{
if (!\is_file($path)) {
if (!is_file($path)) {
throw new IOException(
sprintf('Could not open "%s" for reading! File does not exist.', $path)
);
}
$ext = \pathinfo($path, PATHINFO_EXTENSION);
$ext = \strtolower($ext);
$ext = pathinfo($path, PATHINFO_EXTENSION);
$ext = strtolower($ext);
$readerType = self::$extensionReaderMap[$ext] ?? null;
if ($readerType === null) {
throw new UnsupportedTypeException(

View File

@ -22,7 +22,20 @@ class WriterEntityFactory
*/
public static function createWriter($writerType)
{
return (new WriterFactory())->create($writerType);
return WriterFactory::create($writerType);
}
/**
* This creates an instance of the appropriate writer, given the extension of the file to be written
*
* @param string $path The path to the spreadsheet file. Supported extensions are .csv, .ods and .xlsx
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/
public static function createWriterFromFile(string $path)
{
return WriterFactory::createFromFile($path);
}
/**

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type;
@ -26,6 +27,16 @@ use Box\Spout\Writer\XLSX\Writer as XLSXWriter;
*/
class WriterFactory
{
/**
* Map file extensions to reader types
* @var array
*/
private static $extensionReaderMap = [
'csv' => Type::CSV,
'ods' => Type::ODS,
'xlsx' => Type::XLSX,
];
/**
* This creates an instance of the appropriate writer, given the type of the file to be read
*
@ -33,21 +44,49 @@ class WriterFactory
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/
public function create($writerType)
public static function create($writerType)
{
switch ($writerType) {
case Type::CSV: return $this->getCSVWriter();
case Type::XLSX: return $this->getXLSXWriter();
case Type::ODS: return $this->getODSWriter();
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);
}
}
/**
* This creates an instance of the appropriate writer, given the extension of the file to be written
*
* @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
* @throws \Box\Spout\Common\Exception\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/
public static function createFromFile(string $path)
{
if (!is_file($path)) {
throw new IOException(
sprintf('Could not open "%s" for reading! File does not exist.', $path)
);
}
$ext = pathinfo($path, PATHINFO_EXTENSION);
$ext = strtolower($ext);
$readerType = self::$extensionReaderMap[$ext] ?? null;
if ($readerType === null) {
throw new UnsupportedTypeException(
sprintf('No readers supporting the file extension "%s".', $ext)
);
}
return self::create($readerType);
}
/**
* @return CSVWriter
*/
private function getCSVWriter()
private static function getCSVWriter()
{
$optionsManager = new CSVOptionsManager();
$globalFunctionsHelper = new GlobalFunctionsHelper();
@ -60,7 +99,7 @@ class WriterFactory
/**
* @return XLSXWriter
*/
private function getXLSXWriter()
private static function getXLSXWriter()
{
$styleBuilder = new StyleBuilder();
$optionsManager = new XLSXOptionsManager($styleBuilder);
@ -75,7 +114,7 @@ class WriterFactory
/**
* @return ODSWriter
*/
private function getODSWriter()
private static function getODSWriter()
{
$styleBuilder = new StyleBuilder();
$optionsManager = new ODSOptionsManager($styleBuilder);

View File

@ -71,7 +71,7 @@ class ReaderFactoryTest extends TestCase
{
$this->expectException(UnsupportedTypeException::class);
$invalid = $this->getResourcePath('test_unsupported_file_type.other');
$reader = ReaderFactory::createFromFile($invalid);
ReaderFactory::createFromFile($invalid);
}
/**
@ -81,6 +81,6 @@ class ReaderFactoryTest extends TestCase
{
$this->expectException(IOException::class);
$invalid = 'thereisnosuchfile.ext';
$reader = ReaderFactory::createFromFile($invalid);
ReaderFactory::createFromFile($invalid);
}
}

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase;
/**
@ -10,6 +11,48 @@ use PHPUnit\Framework\TestCase;
*/
class WriterFactoryTest extends TestCase
{
use TestUsingResource;
/**
* @return void
*/
public function testCreateFromFileCSV()
{
$validCsv = $this->getResourcePath('csv_test_create_from_file.csv');
$writer = WriterFactory::createFromFile($validCsv);
$this->assertInstanceOf('Box\Spout\Writer\CSV\Writer', $writer);
}
/**
* @return void
*/
public function testCreateFromFileCSVAllCaps()
{
$validCsv = $this->getResourcePath('csv_test_create_from_file.CSV');
$writer = WriterFactory::createFromFile($validCsv);
$this->assertInstanceOf('Box\Spout\Writer\CSV\Writer', $writer);
}
/**
* @return void
*/
public function testCreateFromFileODS()
{
$validOds = $this->getResourcePath('csv_test_create_from_file.ods');
$writer = WriterFactory::createFromFile($validOds);
$this->assertInstanceOf('Box\Spout\Writer\ODS\Writer', $writer);
}
/**
* @return void
*/
public function testCreateFromFileXLSX()
{
$validXlsx = $this->getResourcePath('csv_test_create_from_file.xlsx');
$writer = WriterFactory::createFromFile($validXlsx);
$this->assertInstanceOf('Box\Spout\Writer\XLSX\Writer', $writer);
}
/**
* @return void
*/
@ -17,6 +60,16 @@ class WriterFactoryTest extends TestCase
{
$this->expectException(UnsupportedTypeException::class);
(new WriterFactory())->create('unsupportedType');
WriterFactory::create('unsupportedType');
}
/**
* @return void
*/
public function testCreateFromFileUnsupported()
{
$this->expectException(UnsupportedTypeException::class);
$invalid = $this->getResourcePath('test_unsupported_file_type.other');
WriterFactory::createFromFile($invalid);
}
}