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:
1 parent
e8693834a0
commit
4a9d0398ad
6 files changed
+124
-19
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
Reference in new issue
Block a user