1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Creator\HelperFactory; |
6
|
|
|
use Box\Spout\Common\Exception\UnsupportedTypeException; |
7
|
|
|
use Box\Spout\Common\Helper\GlobalFunctionsHelper; |
8
|
|
|
use Box\Spout\Common\Type; |
9
|
|
|
use Box\Spout\Writer\Common\Creator\EntityFactory; |
10
|
|
|
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder; |
11
|
|
|
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class WriterFactory |
15
|
|
|
* This factory is used to create writers, based on the type of the file to be read. |
16
|
|
|
* It supports CSV, XLSX and ODS formats. |
17
|
|
|
*/ |
18
|
|
|
class WriterFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* This creates an instance of the appropriate writer, given the type of the file to be read |
22
|
|
|
* |
23
|
|
|
* @param string $writerType Type of the writer to instantiate |
24
|
|
|
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException |
25
|
|
|
* @return WriterInterface |
26
|
|
|
*/ |
27
|
95 |
|
public static function create($writerType) |
28
|
|
|
{ |
29
|
|
|
switch ($writerType) { |
30
|
95 |
|
case Type::CSV: return self::getCSVWriter(); |
31
|
84 |
|
case Type::XLSX: return self::getXLSXWriter(); |
32
|
41 |
|
case Type::ODS: return self::getODSWriter(); |
33
|
|
|
default: |
34
|
1 |
|
throw new UnsupportedTypeException('No writers supporting the given type: ' . $writerType); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return CSV\Writer |
40
|
|
|
*/ |
41
|
11 |
|
private static function getCSVWriter() |
42
|
|
|
{ |
43
|
11 |
|
$optionsManager = new CSV\Manager\OptionsManager(); |
44
|
11 |
|
$styleMerger = new StyleMerger(); |
45
|
11 |
|
$globalFunctionsHelper = new GlobalFunctionsHelper(); |
46
|
|
|
|
47
|
11 |
|
$helperFactory = new HelperFactory(); |
48
|
|
|
|
49
|
11 |
|
return new CSV\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return XLSX\Writer |
54
|
|
|
*/ |
55
|
43 |
|
private static function getXLSXWriter() |
56
|
|
|
{ |
57
|
43 |
|
$styleBuilder = new StyleBuilder(); |
58
|
43 |
|
$optionsManager = new XLSX\Manager\OptionsManager($styleBuilder); |
59
|
43 |
|
$styleMerger = new StyleMerger(); |
60
|
43 |
|
$globalFunctionsHelper = new GlobalFunctionsHelper(); |
61
|
|
|
|
62
|
43 |
|
$helperFactory = new XLSX\Creator\HelperFactory(); |
63
|
43 |
|
$managerFactory = new XLSX\Creator\ManagerFactory(new EntityFactory(), $helperFactory); |
64
|
|
|
|
65
|
43 |
|
return new XLSX\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return ODS\Writer |
70
|
|
|
*/ |
71
|
40 |
|
private static function getODSWriter() |
72
|
|
|
{ |
73
|
40 |
|
$styleBuilder = new StyleBuilder(); |
74
|
40 |
|
$optionsManager = new ODS\Manager\OptionsManager($styleBuilder); |
75
|
40 |
|
$styleMerger = new StyleMerger(); |
76
|
40 |
|
$globalFunctionsHelper = new GlobalFunctionsHelper(); |
77
|
|
|
|
78
|
40 |
|
$helperFactory = new ODS\Creator\HelperFactory(); |
79
|
40 |
|
$managerFactory = new ODS\Creator\ManagerFactory(new EntityFactory(), $helperFactory); |
80
|
|
|
|
81
|
40 |
|
return new ODS\Writer($optionsManager, $styleMerger, $globalFunctionsHelper, $helperFactory, $managerFactory); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|