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
parent e8693834a0
commit fbaea97e27
6 changed files with 124 additions and 19 deletions

View File

@ -19,19 +19,19 @@ class ReaderEntityFactory
*/ */
public static function createReader($readerType) public static function createReader($readerType)
{ {
return (new ReaderFactory())->create($readerType); return ReaderFactory::create($readerType);
} }
/** /**
* Creates a reader by file extension * 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\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface * @return ReaderInterface
*/ */
public static function createReaderFromFile(string $path) 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 * 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\IOException
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface * @return ReaderInterface
*/ */
public static function createFromFile(string $path) public static function createFromFile(string $path)
{ {
if (!\is_file($path)) { if (!is_file($path)) {
throw new IOException( throw new IOException(
sprintf('Could not open "%s" for reading! File does not exist.', $path) sprintf('Could not open "%s" for reading! File does not exist.', $path)
); );
} }
$ext = \pathinfo($path, PATHINFO_EXTENSION); $ext = pathinfo($path, PATHINFO_EXTENSION);
$ext = \strtolower($ext); $ext = strtolower($ext);
$readerType = self::$extensionReaderMap[$ext] ?? null; $readerType = self::$extensionReaderMap[$ext] ?? null;
if ($readerType === null) { if ($readerType === null) {
throw new UnsupportedTypeException( throw new UnsupportedTypeException(

View File

@ -22,7 +22,20 @@ class WriterEntityFactory
*/ */
public static function createWriter($writerType) 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; namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Creator\HelperFactory; use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Helper\GlobalFunctionsHelper; use Box\Spout\Common\Helper\GlobalFunctionsHelper;
use Box\Spout\Common\Type; use Box\Spout\Common\Type;
@ -26,6 +27,16 @@ use Box\Spout\Writer\XLSX\Writer as XLSXWriter;
*/ */
class WriterFactory 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 * 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 * @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface * @return WriterInterface
*/ */
public function create($writerType) public static function create($writerType)
{ {
switch ($writerType) { switch ($writerType) {
case Type::CSV: return $this->getCSVWriter(); case Type::CSV: return self::getCSVWriter();
case Type::XLSX: return $this->getXLSXWriter(); case Type::XLSX: return self::getXLSXWriter();
case Type::ODS: return $this->getODSWriter(); case Type::ODS: return self::getODSWriter();
default: default:
throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType); 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 * @return CSVWriter
*/ */
private function getCSVWriter() private static function getCSVWriter()
{ {
$optionsManager = new CSVOptionsManager(); $optionsManager = new CSVOptionsManager();
$globalFunctionsHelper = new GlobalFunctionsHelper(); $globalFunctionsHelper = new GlobalFunctionsHelper();
@ -60,7 +99,7 @@ class WriterFactory
/** /**
* @return XLSXWriter * @return XLSXWriter
*/ */
private function getXLSXWriter() private static function getXLSXWriter()
{ {
$styleBuilder = new StyleBuilder(); $styleBuilder = new StyleBuilder();
$optionsManager = new XLSXOptionsManager($styleBuilder); $optionsManager = new XLSXOptionsManager($styleBuilder);
@ -75,7 +114,7 @@ class WriterFactory
/** /**
* @return ODSWriter * @return ODSWriter
*/ */
private function getODSWriter() private static function getODSWriter()
{ {
$styleBuilder = new StyleBuilder(); $styleBuilder = new StyleBuilder();
$optionsManager = new ODSOptionsManager($styleBuilder); $optionsManager = new ODSOptionsManager($styleBuilder);

View File

@ -71,7 +71,7 @@ class ReaderFactoryTest extends TestCase
{ {
$this->expectException(UnsupportedTypeException::class); $this->expectException(UnsupportedTypeException::class);
$invalid = $this->getResourcePath('test_unsupported_file_type.other'); $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); $this->expectException(IOException::class);
$invalid = 'thereisnosuchfile.ext'; $invalid = 'thereisnosuchfile.ext';
$reader = ReaderFactory::createFromFile($invalid); ReaderFactory::createFromFile($invalid);
} }
} }

View File

@ -3,6 +3,7 @@
namespace Box\Spout\Writer\Common\Creator; namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException; use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\TestUsingResource;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** /**
@ -10,6 +11,48 @@ use PHPUnit\Framework\TestCase;
*/ */
class WriterFactoryTest extends 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 * @return void
*/ */
@ -17,6 +60,16 @@ class WriterFactoryTest extends TestCase
{ {
$this->expectException(UnsupportedTypeException::class); $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);
} }
} }