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.
69 lines
1.7 KiB
PHP
69 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Box\Spout\Reader\Common\Creator;
|
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException;
|
|
use Box\Spout\Common\Type;
|
|
use Box\Spout\Reader\ReaderInterface;
|
|
|
|
/**
|
|
* Class ReaderEntityFactory
|
|
* Factory to create external entities
|
|
*/
|
|
class ReaderEntityFactory
|
|
{
|
|
/**
|
|
* 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
|
|
*/
|
|
public static function createReaderFromFile(string $path)
|
|
{
|
|
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
|
|
}
|
|
}
|
|
}
|