1
|
|
|
<?php namespace Arcanedev\LaravelExcel\Exporters; |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
4
|
|
|
use Box\Spout\Writer\WriterFactory; |
5
|
|
|
use Arcanedev\LaravelExcel\Contracts\Serializer as SerializerContract; |
6
|
|
|
use Arcanedev\LaravelExcel\Contracts\Exporter as ExporterContract; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class AbstractExporter |
10
|
|
|
* |
11
|
|
|
* @package Arcanedev\LaravelExcel\Exporter |
12
|
|
|
* @author ARCANEDEV <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractExporter implements ExporterContract |
15
|
|
|
{ |
16
|
|
|
/* ------------------------------------------------------------------------------------------------ |
17
|
|
|
| Properties |
18
|
|
|
| ------------------------------------------------------------------------------------------------ |
19
|
|
|
*/ |
20
|
|
|
/** @var array */ |
21
|
|
|
protected $data = []; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $type; |
25
|
|
|
|
26
|
|
|
/** @var \Arcanedev\LaravelExcel\Contracts\Serializer */ |
27
|
|
|
protected $serializer; |
28
|
|
|
|
29
|
|
|
/** @var \Box\Spout\Writer\WriterInterface */ |
30
|
|
|
protected $writer; |
31
|
|
|
|
32
|
|
|
/** @var array */ |
33
|
|
|
protected $options = []; |
34
|
|
|
|
35
|
|
|
/* ------------------------------------------------------------------------------------------------ |
36
|
|
|
| Constructor |
37
|
|
|
| ------------------------------------------------------------------------------------------------ |
38
|
|
|
*/ |
39
|
|
|
/** |
40
|
|
|
* AbstractExporter constructor. |
41
|
|
|
* |
42
|
|
|
* @param array $options |
43
|
|
|
*/ |
44
|
18 |
|
public function __construct(array $options = []) |
45
|
|
|
{ |
46
|
18 |
|
$this->setSerializer(new DefaultSerializer); |
47
|
18 |
|
$this->setOptions($options); |
48
|
18 |
|
} |
49
|
|
|
|
50
|
|
|
/* ------------------------------------------------------------------------------------------------ |
51
|
|
|
| Getters & Setters |
52
|
|
|
| ------------------------------------------------------------------------------------------------ |
53
|
|
|
*/ |
54
|
|
|
/** |
55
|
|
|
* Set the serializer. |
56
|
|
|
* |
57
|
|
|
* @param \Arcanedev\LaravelExcel\Contracts\Serializer $serializer |
58
|
|
|
* |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
18 |
|
public function setSerializer(SerializerContract $serializer) |
62
|
|
|
{ |
63
|
18 |
|
$this->serializer = $serializer; |
64
|
|
|
|
65
|
18 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the file type. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
18 |
|
public function getType() |
74
|
|
|
{ |
75
|
18 |
|
return $this->type; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Set the writer options. |
80
|
|
|
* |
81
|
|
|
* @param array $options |
82
|
|
|
* |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
18 |
|
public function setOptions(array $options) |
86
|
|
|
{ |
87
|
18 |
|
$this->options = $options; |
88
|
|
|
|
89
|
18 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* ------------------------------------------------------------------------------------------------ |
93
|
|
|
| Main Functions |
94
|
|
|
| ------------------------------------------------------------------------------------------------ |
95
|
|
|
*/ |
96
|
|
|
/** |
97
|
|
|
* Load the data. |
98
|
|
|
* |
99
|
|
|
* @param Collection $data |
100
|
|
|
* |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
|
|
public function load(Collection $data) |
104
|
|
|
{ |
105
|
|
|
$this->data = $data; |
|
|
|
|
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Save the data to a file. |
112
|
|
|
* |
113
|
|
|
* @param string $filename |
114
|
|
|
*/ |
115
|
|
|
public function save($filename) |
116
|
|
|
{ |
117
|
|
|
$this->create(); |
118
|
|
|
$this->writer->openToFile($filename); |
119
|
|
|
$this->makeRows(); |
120
|
|
|
$this->close(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Output data directly to the browser. |
125
|
|
|
* |
126
|
|
|
* @param string $filename |
127
|
|
|
*/ |
128
|
|
|
public function stream($filename) |
129
|
|
|
{ |
130
|
|
|
$this->create(); |
131
|
|
|
$this->writer->openToBrowser($filename); |
132
|
|
|
$this->makeRows(); |
133
|
|
|
$this->close(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/* ------------------------------------------------------------------------------------------------ |
137
|
|
|
| Other Functions |
138
|
|
|
| ------------------------------------------------------------------------------------------------ |
139
|
|
|
*/ |
140
|
|
|
/** |
141
|
|
|
* Create the writer. |
142
|
|
|
*/ |
143
|
|
|
protected function create() |
144
|
|
|
{ |
145
|
|
|
$this->writer = WriterFactory::create($this->type); |
146
|
|
|
$this->loadOptions(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Load the writer options. |
151
|
|
|
*/ |
152
|
|
|
abstract protected function loadOptions(); |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Close the writer. |
156
|
|
|
*/ |
157
|
|
|
protected function close() |
158
|
|
|
{ |
159
|
|
|
$this->writer->close(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Make rows. |
164
|
|
|
*/ |
165
|
|
|
protected function makeRows() |
166
|
|
|
{ |
167
|
|
|
if ( ! empty($headerRow = $this->serializer->getHeader())) |
168
|
|
|
$this->writer->addRow($headerRow); |
169
|
|
|
|
170
|
|
|
foreach ($this->data as $record) { |
171
|
|
|
$this->writer->addRow( |
172
|
|
|
$this->serializer->getData($record) |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..