1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace OpenSpout\Writer\Common\Creator;
|
|
|
6 |
|
|
|
7 |
use OpenSpout\Common\Exception\UnsupportedTypeException;
|
|
|
8 |
use OpenSpout\Writer\CSV\Writer as CSVWriter;
|
|
|
9 |
use OpenSpout\Writer\ODS\Writer as ODSWriter;
|
|
|
10 |
use OpenSpout\Writer\WriterInterface;
|
|
|
11 |
use OpenSpout\Writer\XLSX\Writer as XLSXWriter;
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* This factory is used to create writers, based on the type of the file to be read.
|
|
|
15 |
* It supports CSV, XLSX and ODS formats.
|
|
|
16 |
*
|
|
|
17 |
* @deprecated Guessing mechanisms are brittle by nature and won't be provided by this library anymore
|
|
|
18 |
*/
|
|
|
19 |
final class WriterFactory
|
|
|
20 |
{
|
|
|
21 |
/**
|
|
|
22 |
* This creates an instance of the appropriate writer, given the extension of the file to be written.
|
|
|
23 |
*
|
|
|
24 |
* @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx
|
|
|
25 |
*
|
|
|
26 |
* @throws UnsupportedTypeException
|
|
|
27 |
*/
|
|
|
28 |
public static function createFromFile(string $path): WriterInterface
|
|
|
29 |
{
|
|
|
30 |
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
|
|
31 |
|
|
|
32 |
return match ($extension) {
|
|
|
33 |
'csv' => new CSVWriter(),
|
|
|
34 |
'xlsx' => new XLSXWriter(),
|
|
|
35 |
'ods' => new ODSWriter(),
|
|
|
36 |
default => throw new UnsupportedTypeException('No writers supporting the given type: '.$extension),
|
|
|
37 |
};
|
|
|
38 |
}
|
|
|
39 |
}
|