1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Common\Creator; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Creator\HelperFactory; |
6
|
|
|
use Box\Spout\Common\Exception\IOException; |
7
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException; |
8
|
|
|
use Box\Spout\Common\Helper\GlobalFunctionsHelper; |
9
|
|
|
use Box\Spout\Common\Type; |
10
|
|
|
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; |
11
|
|
|
use Box\Spout\Writer\CSV\Manager\OptionsManager as CSVOptionsManager; |
12
|
|
|
use Box\Spout\Writer\CSV\Writer as CSVWriter; |
13
|
|
|
use Box\Spout\Writer\ODS\Creator\HelperFactory as ODSHelperFactory; |
14
|
|
|
use Box\Spout\Writer\ODS\Creator\ManagerFactory as ODSManagerFactory; |
15
|
|
|
use Box\Spout\Writer\ODS\Manager\OptionsManager as ODSOptionsManager; |
16
|
|
|
use Box\Spout\Writer\ODS\Writer as ODSWriter; |
17
|
|
|
use Box\Spout\Writer\WriterInterface; |
18
|
|
|
use Box\Spout\Writer\XLSX\Creator\HelperFactory as XLSXHelperFactory; |
19
|
|
|
use Box\Spout\Writer\XLSX\Creator\ManagerFactory as XLSXManagerFactory; |
20
|
|
|
use Box\Spout\Writer\XLSX\Manager\OptionsManager as XLSXOptionsManager; |
21
|
|
|
use Box\Spout\Writer\XLSX\Writer as XLSXWriter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class WriterFactory |
25
|
|
|
* This factory is used to create writers, based on the type of the file to be read. |
26
|
|
|
* It supports CSV, XLSX and ODS formats. |
27
|
|
|
*/ |
28
|
|
|
class WriterFactory |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Map file extensions to reader types |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $extensionReaderMap = [ |
35
|
|
|
'csv' => Type::CSV, |
36
|
|
|
'ods' => Type::ODS, |
37
|
|
|
'xlsx' => Type::XLSX, |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This creates an instance of the appropriate writer, given the type of the file to be read |
42
|
|
|
* |
43
|
|
|
* @param string $writerType Type of the writer to instantiate |
44
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
45
|
|
|
* @return WriterInterface |
46
|
|
|
*/ |
47
|
101 |
|
public static function create($writerType) |
48
|
|
|
{ |
49
|
|
|
switch ($writerType) { |
50
|
101 |
|
case Type::CSV: return self::getCSVWriter(); |
51
|
88 |
|
case Type::XLSX: return self::getXLSXWriter(); |
52
|
43 |
|
case Type::ODS: return self::getODSWriter(); |
53
|
|
|
default: |
54
|
1 |
|
throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This creates an instance of the appropriate writer, given the extension of the file to be written |
60
|
|
|
* |
61
|
|
|
* @param string $path The path to the spreadsheet file. Supported extensions are .csv,.ods and .xlsx |
62
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
63
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
64
|
|
|
* @return WriterInterface |
65
|
|
|
*/ |
66
|
5 |
|
public static function createFromFile(string $path) |
67
|
|
|
{ |
68
|
5 |
|
if (!is_file($path)) { |
69
|
|
|
throw new IOException( |
70
|
|
|
sprintf('Could not open "%s" for reading! File does not exist.', $path) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
$ext = pathinfo($path, PATHINFO_EXTENSION); |
75
|
5 |
|
$ext = strtolower($ext); |
76
|
5 |
|
$readerType = self::$extensionReaderMap[$ext] ?? null; |
77
|
5 |
|
if ($readerType === null) { |
78
|
1 |
|
throw new UnsupportedTypeException( |
79
|
1 |
|
sprintf('No readers supporting the file extension "%s".', $ext) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return self::create($readerType); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return CSVWriter |
88
|
|
|
*/ |
89
|
13 |
|
private static function getCSVWriter() |
90
|
|
|
{ |
91
|
13 |
|
$optionsManager = new CSVOptionsManager(); |
92
|
13 |
|
$globalFunctionsHelper = new GlobalFunctionsHelper(); |
93
|
|
|
|
94
|
13 |
|
$helperFactory = new HelperFactory(); |
95
|
|
|
|
96
|
13 |
|
return new CSVWriter($optionsManager, $globalFunctionsHelper, $helperFactory); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return XLSXWriter |
101
|
|
|
*/ |
102
|
45 |
|
private static function getXLSXWriter() |
103
|
|
|
{ |
104
|
45 |
|
$styleBuilder = new StyleBuilder(); |
105
|
45 |
|
$optionsManager = new XLSXOptionsManager($styleBuilder); |
106
|
45 |
|
$globalFunctionsHelper = new GlobalFunctionsHelper(); |
107
|
|
|
|
108
|
45 |
|
$helperFactory = new XLSXHelperFactory(); |
109
|
45 |
|
$managerFactory = new XLSXManagerFactory(new InternalEntityFactory(), $helperFactory); |
110
|
|
|
|
111
|
45 |
|
return new XLSXWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return ODSWriter |
116
|
|
|
*/ |
117
|
42 |
|
private static function getODSWriter() |
118
|
|
|
{ |
119
|
42 |
|
$styleBuilder = new StyleBuilder(); |
120
|
42 |
|
$optionsManager = new ODSOptionsManager($styleBuilder); |
121
|
42 |
|
$globalFunctionsHelper = new GlobalFunctionsHelper(); |
122
|
|
|
|
123
|
42 |
|
$helperFactory = new ODSHelperFactory(); |
124
|
42 |
|
$managerFactory = new ODSManagerFactory(new InternalEntityFactory(), $helperFactory); |
125
|
|
|
|
126
|
42 |
|
return new ODSWriter($optionsManager, $globalFunctionsHelper, $helperFactory, $managerFactory); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|