1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace MisterIcy\ExcelWriter; |
5
|
|
|
|
6
|
|
|
use MisterIcy\ExcelWriter\Generator\AbstractGenerator; |
7
|
|
|
use MisterIcy\ExcelWriter\Generator\BasicGenerator; |
8
|
|
|
use MisterIcy\ExcelWriter\Generator\GeneratorInterface; |
9
|
|
|
use MisterIcy\ExcelWriter\Properties\PropertyCollection; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ExcelWriter |
15
|
|
|
* @package MisterIcy\ExcelWriter |
16
|
|
|
*/ |
17
|
|
|
final class ExcelWriter |
18
|
|
|
{ |
19
|
|
|
/** @var PropertyCollection */ |
20
|
|
|
private $properties; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return PropertyCollection |
24
|
|
|
*/ |
25
|
|
|
public function getProperties(): PropertyCollection |
26
|
|
|
{ |
27
|
|
|
return $this->properties; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return GeneratorInterface |
32
|
|
|
*/ |
33
|
|
|
public function getGenerator(): GeneratorInterface |
34
|
|
|
{ |
35
|
|
|
return $this->generator; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** @var GeneratorInterface */ |
39
|
|
|
private $generator; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param GeneratorInterface|null $generator |
43
|
|
|
* @param PropertyCollection|null $properties |
44
|
|
|
* @return ExcelWriter |
45
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
46
|
|
|
*/ |
47
|
|
|
public static function createWriter( |
48
|
|
|
GeneratorInterface $generator = null, |
49
|
|
|
PropertyCollection $properties = null |
50
|
|
|
) : self { |
51
|
|
|
return new self($generator, $properties); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* ExcelWriter constructor. |
56
|
|
|
* @param AbstractGenerator|null $generator |
57
|
|
|
* @param PropertyCollection|null $properties |
58
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
59
|
|
|
*/ |
60
|
|
|
private function __construct( |
61
|
|
|
AbstractGenerator $generator = null, |
62
|
|
|
PropertyCollection $properties = null |
63
|
|
|
) { |
64
|
|
|
if (is_null($generator)) { |
65
|
|
|
/** Default generator is the basic one */ |
66
|
|
|
$this->generator = new BasicGenerator(); |
67
|
|
|
} else { |
68
|
|
|
$this->generator = $generator; |
69
|
|
|
} |
70
|
|
|
if (is_null($properties)) { |
71
|
|
|
$this->properties = new PropertyCollection(); |
72
|
|
|
} else { |
73
|
|
|
$this->properties = $properties; |
74
|
|
|
} |
75
|
|
|
$this->generator->setProperties($this->getProperties()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return Spreadsheet |
80
|
|
|
*/ |
81
|
|
|
public function generateSpreadsheet(array $data, Spreadsheet $spreadsheet = null) : Spreadsheet |
82
|
|
|
{ |
83
|
|
|
/** @var AbstractGenerator $generator */ |
84
|
|
|
$generator = $this->getGenerator(); |
85
|
|
|
|
86
|
|
|
$generator->setData($data); |
87
|
|
|
|
88
|
|
|
$generator->generate($spreadsheet); |
89
|
|
|
|
90
|
|
|
return $generator->getSpreadsheet(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $data |
95
|
|
|
* @param string|null $filename |
96
|
|
|
* @return \SplFileObject |
97
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
98
|
|
|
*/ |
99
|
|
|
public function generateFile(array $data, string $filename = null) : \SplFileObject |
100
|
|
|
{ |
101
|
|
|
$spreadsheet = $this->generateSpreadsheet($data); |
102
|
|
|
|
103
|
|
|
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); |
104
|
|
|
|
105
|
|
|
if (is_null($filename)) { |
106
|
|
|
$filename = uniqid() . ".xlsx"; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$path = sys_get_temp_dir() . "/" . $filename; |
110
|
|
|
|
111
|
|
|
$writer->save($path); |
112
|
|
|
|
113
|
|
|
return new \SplFileObject($path, 'r'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|