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 authored and Adrien Loison committed 2019-05-17 13:22:27 +02:00
1 parent e8693834a0
commit 4a9d0398ad
6 files changed
+124 -19

No files matched your search

@@ -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);
}
/**
@@ -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);