spout/src/Spout/Writer/Common/Creator/WriterEntityFactory.php
Adrien Loison fbaea97e27 Update Reader/WriterEntityFactory
Add `WriterEntityFactory::createWriterFromFile`, working like `ReaderEntityFactory::createReaderFromFile` (guessing writer type from file name).
Use static functions when needed.
2019-05-17 13:19:09 +02:00

75 lines
2.0 KiB
PHP

<?php
namespace Box\Spout\Writer\Common\Creator;
use Box\Spout\Common\Entity\Cell;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Writer\WriterInterface;
/**
* Class WriterEntityFactory
* Factory to create external entities
*/
class WriterEntityFactory
{
/**
* This creates an instance of the appropriate writer, given the type of the file to be written
*
* @param string $writerType Type of the writer to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return WriterInterface
*/
public static function createWriter($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);
}
/**
* @param Cell[] $cells
* @param Style|null $rowStyle
* @return Row
*/
public static function createRow(array $cells = [], Style $rowStyle = null)
{
return new Row($cells, $rowStyle);
}
/**
* @param array $cellValues
* @param Style|null $rowStyle
* @return Row
*/
public static function createRowFromArray(array $cellValues = [], Style $rowStyle = null)
{
$cells = array_map(function ($cellValue) {
return new Cell($cellValue);
}, $cellValues);
return new Row($cells, $rowStyle);
}
/**
* @param mixed $cellValue
* @param Style|null $cellStyle
* @return Cell
*/
public static function createCell($cellValue, Style $cellStyle = null)
{
return new Cell($cellValue, $cellStyle);
}
}