Add helper functions to create specific readers and writers

Removed the `ReaderEntityFactory::createReader(Type)` method and replaced it by 3 methods:
- `ReaderEntityFactory::createCSVReader()`
- `ReaderEntityFactory::createXLSXReader()`
- `ReaderEntityFactory::createODSReader()`

This has the advantage of enabling autocomplete in the IDE, as the return type is no longer the interface but the concrete type. Since readers may expose different options, this is pretty useful.

Similarly, removed the `WriterEntityFactory::createWriter(Type)` method and replaced it by 3 methods:
- `WriterEntityFactory::createCSVWriter()`
- `WriterEntityFactory::createXLSXWriter()`
- `WriterEntityFactory::createODSWriter()`

Since this is a breaking change, I also updated the Upgrade guide.
Finally, the doc is up to date too.
This commit is contained in:
Adrien Loison authored and Adrien Loison committed 2019-05-17 21:22:03 +02:00
1 parent 6104d41857
commit 40ee386edd
30 files changed
+235 -275

No files matched your search

@@ -2,6 +2,8 @@
namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\ReaderInterface;
/**
@@ -10,23 +12,10 @@ use Box\Spout\Reader\ReaderInterface;
*/
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
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function createReader($readerType)
{
return ReaderFactory::create($readerType);
}
/**
* Creates a reader by file extension
*
* @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
*/
@@ -34,4 +23,46 @@ class ReaderEntityFactory
{
return ReaderFactory::createFromFile($path);
}
/**
* This creates an instance of a CSV reader
*
* @return \Box\Spout\Reader\CSV\Reader
*/
public static function createCSVReader()
{
try {
return ReaderFactory::createFromType(Type::CSV);
} catch (UnsupportedTypeException $e) {
// should never happen
}
}
/**
* This creates an instance of a XLSX reader
*
* @return \Box\Spout\Reader\XLSX\Reader
*/
public static function createXLSXReader()
{
try {
return ReaderFactory::createFromType(Type::XLSX);
} catch (UnsupportedTypeException $e) {
// should never happen
}
}
/**
* This creates an instance of a ODS reader
*
* @return \Box\Spout\Reader\ODS\Reader
*/
public static function createODSReader()
{
try {
return ReaderFactory::createFromType(Type::ODS);
} catch (UnsupportedTypeException $e) {
// should never happen
}
}
}
@@ -3,7 +3,6 @@
namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Common\Creator\HelperFactory;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Common\Exception\UnsupportedTypeException;
use Box\Spout\Common\Type;
use Box\Spout\Reader\CSV\Creator\InternalEntityFactory as CSVInternalEntityFactory;
@@ -30,14 +29,18 @@ use Box\Spout\Reader\XLSX\Reader as XLSXReader;
class ReaderFactory
{
/**
* Map file extensions to reader types
* @var array
* Creates a reader by file extension
*
* @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
private static $extensionReaderMap = [
'csv' => Type::CSV,
'ods' => Type::ODS,
'xlsx' => Type::XLSX,
];
public static function createFromFile(string $path)
{
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
return self::createFromType($extension);
}
/**
* This creates an instance of the appropriate reader, given the type of the file to be read
@@ -46,49 +49,21 @@ class ReaderFactory
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function create($readerType)
public static function createFromType($readerType)
{
switch ($readerType) {
case Type::CSV: return self::getCSVReader();
case Type::XLSX: return self::getXLSXReader();
case Type::ODS: return self::getODSReader();
case Type::CSV: return self::createCSVReader();
case Type::XLSX: return self::createXLSXReader();
case Type::ODS: return self::createODSReader();
default:
throw new UnsupportedTypeException('No readers supporting the given type: ' . $readerType);
}
}
/**
* Creates a reader by file extension
*
* @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)) {
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 CSVReader
*/
private static function getCSVReader()
private static function createCSVReader()
{
$optionsManager = new CSVOptionsManager();
$helperFactory = new HelperFactory();
@@ -101,7 +76,7 @@ class ReaderFactory
/**
* @return XLSXReader
*/
private static function getXLSXReader()
private static function createXLSXReader()
{
$optionsManager = new XLSXOptionsManager();
$helperFactory = new XLSXHelperFactory();
@@ -115,7 +90,7 @@ class ReaderFactory
/**
* @return ODSReader
*/
private static function getODSReader()
private static function createODSReader()
{
$optionsManager = new ODSOptionsManager();
$helperFactory = new ODSHelperFactory();