1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\Common\Creator; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Cell; |
6
|
|
|
use Box\Spout\Common\Entity\Row; |
7
|
|
|
use Box\Spout\Common\Entity\Style\Style; |
8
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException; |
9
|
|
|
use Box\Spout\Common\Type; |
10
|
|
|
use Box\Spout\Writer\WriterInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class WriterEntityFactory |
14
|
|
|
* Factory to create external entities |
15
|
|
|
*/ |
16
|
|
|
class WriterEntityFactory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* This creates an instance of the appropriate writer, given the type of the file to be written |
20
|
|
|
* |
21
|
|
|
* @param string $writerType Type of the writer to instantiate |
22
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
23
|
|
|
* @return WriterInterface |
24
|
|
|
*/ |
25
|
|
|
public static function createWriter($writerType) |
26
|
|
|
{ |
27
|
|
|
return WriterFactory::createFromType($writerType); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This creates an instance of the appropriate writer, given the extension of the file to be written |
32
|
|
|
* |
33
|
|
|
* @param string $path The path to the spreadsheet file. Supported extensions are .csv, .ods and .xlsx |
34
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
35
|
|
|
* @return WriterInterface |
36
|
|
|
*/ |
37
|
|
|
public static function createWriterFromFile(string $path) |
38
|
|
|
{ |
39
|
|
|
return WriterFactory::createFromFile($path); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* This creates an instance of a CSV writer |
44
|
|
|
* |
45
|
|
|
* @return \Box\Spout\Writer\CSV\Writer |
46
|
|
|
*/ |
47
|
11 |
|
public static function createCSVWriter() |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
11 |
|
return WriterFactory::createFromType(Type::CSV); |
51
|
|
|
} catch (UnsupportedTypeException $e) { |
52
|
|
|
// should never happen |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* This creates an instance of a XLSX writer |
58
|
|
|
* |
59
|
|
|
* @return \Box\Spout\Writer\XLSX\Writer |
60
|
|
|
*/ |
61
|
48 |
|
public static function createXLSXWriter() |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
48 |
|
return WriterFactory::createFromType(Type::XLSX); |
65
|
|
|
} catch (UnsupportedTypeException $e) { |
66
|
|
|
// should never happen |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* This creates an instance of a ODS writer |
72
|
|
|
* |
73
|
|
|
* @return \Box\Spout\Writer\ODS\Writer |
74
|
|
|
*/ |
75
|
44 |
|
public static function createODSWriter() |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
44 |
|
return WriterFactory::createFromType(Type::ODS); |
79
|
|
|
} catch (UnsupportedTypeException $e) { |
80
|
|
|
// should never happen |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Cell[] $cells |
86
|
|
|
* @param Style|null $rowStyle |
87
|
|
|
* @return Row |
88
|
|
|
*/ |
89
|
4 |
|
public static function createRow(array $cells = [], Style $rowStyle = null) |
90
|
|
|
{ |
91
|
4 |
|
return new Row($cells, $rowStyle); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $cellValues |
96
|
|
|
* @param Style|null $rowStyle |
97
|
|
|
* @return Row |
98
|
|
|
*/ |
99
|
83 |
|
public static function createRowFromArray(array $cellValues = [], Style $rowStyle = null) |
100
|
|
|
{ |
101
|
|
|
$cells = \array_map(function ($cellValue) { |
102
|
83 |
|
return new Cell($cellValue); |
103
|
83 |
|
}, $cellValues); |
104
|
|
|
|
105
|
83 |
|
return new Row($cells, $rowStyle); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param mixed $cellValue |
110
|
|
|
* @param Style|null $cellStyle |
111
|
|
|
* @return Cell |
112
|
|
|
*/ |
113
|
4 |
|
public static function createCell($cellValue, Style $cellStyle = null) |
114
|
|
|
{ |
115
|
4 |
|
return new Cell($cellValue, $cellStyle); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|