diff --git a/src/Spout/Reader/Common/Creator/ReaderEntityFactory.php b/src/Spout/Reader/Common/Creator/ReaderEntityFactory.php index e9e8063..37c7988 100644 --- a/src/Spout/Reader/Common/Creator/ReaderEntityFactory.php +++ b/src/Spout/Reader/Common/Creator/ReaderEntityFactory.php @@ -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); } } diff --git a/src/Spout/Reader/Common/Creator/ReaderFactory.php b/src/Spout/Reader/Common/Creator/ReaderFactory.php index 0442974..fbdc319 100644 --- a/src/Spout/Reader/Common/Creator/ReaderFactory.php +++ b/src/Spout/Reader/Common/Creator/ReaderFactory.php @@ -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( diff --git a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php index b61d899..113df5c 100644 --- a/src/Spout/Writer/Common/Creator/WriterEntityFactory.php +++ b/src/Spout/Writer/Common/Creator/WriterEntityFactory.php @@ -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); } /** diff --git a/src/Spout/Writer/Common/Creator/WriterFactory.php b/src/Spout/Writer/Common/Creator/WriterFactory.php index 19e3a62..c3e80f7 100644 --- a/src/Spout/Writer/Common/Creator/WriterFactory.php +++ b/src/Spout/Writer/Common/Creator/WriterFactory.php @@ -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); diff --git a/tests/Spout/Reader/Common/Creator/ReaderFactoryTest.php b/tests/Spout/Reader/Common/Creator/ReaderFactoryTest.php index 1efb292..e5cc68c 100644 --- a/tests/Spout/Reader/Common/Creator/ReaderFactoryTest.php +++ b/tests/Spout/Reader/Common/Creator/ReaderFactoryTest.php @@ -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); } } diff --git a/tests/Spout/Writer/Common/Creator/WriterFactoryTest.php b/tests/Spout/Writer/Common/Creator/WriterFactoryTest.php index ee6c375..d3041a5 100644 --- a/tests/Spout/Writer/Common/Creator/WriterFactoryTest.php +++ b/tests/Spout/Writer/Common/Creator/WriterFactoryTest.php @@ -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); } }