|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer; |
|
4
|
|
|
|
|
5
|
|
|
use Box\Spout\Common\Creator\HelperFactory; |
|
6
|
|
|
use Box\Spout\Common\Entity\Row; |
|
7
|
|
|
use Box\Spout\Common\Exception\SpoutException; |
|
8
|
|
|
use Box\Spout\Common\Helper\GlobalFunctionsHelper; |
|
9
|
|
|
use Box\Spout\Common\Manager\OptionsManagerInterface; |
|
10
|
|
|
use Box\Spout\Writer\Common\Creator\ManagerFactoryInterface; |
|
11
|
|
|
use Box\Spout\Writer\Common\Entity\Options; |
|
12
|
|
|
use Box\Spout\Writer\Common\Entity\Sheet; |
|
13
|
|
|
use Box\Spout\Writer\Common\Entity\Worksheet; |
|
14
|
|
|
use Box\Spout\Writer\Common\Manager\WorkbookManagerInterface; |
|
15
|
|
|
use Box\Spout\Writer\Exception\SheetNotFoundException; |
|
16
|
|
|
use Box\Spout\Writer\Exception\WriterAlreadyOpenedException; |
|
17
|
|
|
use Box\Spout\Writer\Exception\WriterNotOpenedException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class WriterMultiSheetsAbstract |
|
21
|
|
|
* |
|
22
|
|
|
* @abstract |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class WriterMultiSheetsAbstract extends WriterAbstract |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ManagerFactoryInterface */ |
|
27
|
|
|
private $managerFactory; |
|
28
|
|
|
|
|
29
|
|
|
/** @var WorkbookManagerInterface */ |
|
30
|
|
|
private $workbookManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param OptionsManagerInterface $optionsManager |
|
34
|
|
|
* @param GlobalFunctionsHelper $globalFunctionsHelper |
|
35
|
|
|
* @param HelperFactory $helperFactory |
|
36
|
|
|
* @param ManagerFactoryInterface $managerFactory |
|
37
|
|
|
*/ |
|
38
|
77 |
|
public function __construct( |
|
39
|
|
|
OptionsManagerInterface $optionsManager, |
|
40
|
|
|
GlobalFunctionsHelper $globalFunctionsHelper, |
|
41
|
|
|
HelperFactory $helperFactory, |
|
42
|
|
|
ManagerFactoryInterface $managerFactory |
|
43
|
|
|
) { |
|
44
|
77 |
|
parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory); |
|
45
|
77 |
|
$this->managerFactory = $managerFactory; |
|
46
|
77 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Sets whether new sheets should be automatically created when the max rows limit per sheet is reached. |
|
50
|
|
|
* This must be set before opening the writer. |
|
51
|
|
|
* |
|
52
|
|
|
* @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached |
|
53
|
|
|
* @throws WriterAlreadyOpenedException If the writer was already opened |
|
54
|
|
|
* @return WriterMultiSheetsAbstract |
|
55
|
|
|
*/ |
|
56
|
29 |
|
public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) |
|
57
|
|
|
{ |
|
58
|
29 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
|
59
|
|
|
|
|
60
|
27 |
|
$this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically); |
|
61
|
|
|
|
|
62
|
27 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
67 |
|
protected function openWriter() |
|
69
|
|
|
{ |
|
70
|
67 |
|
if (!$this->workbookManager) { |
|
71
|
67 |
|
$this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager); |
|
72
|
67 |
|
$this->workbookManager->addNewSheetAndMakeItCurrent(); |
|
73
|
|
|
} |
|
74
|
67 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Returns all the workbook's sheets |
|
78
|
|
|
* |
|
79
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
80
|
|
|
* @return Sheet[] All the workbook's sheets |
|
81
|
|
|
*/ |
|
82
|
12 |
|
public function getSheets() |
|
83
|
|
|
{ |
|
84
|
12 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
85
|
|
|
|
|
86
|
12 |
|
$externalSheets = []; |
|
87
|
12 |
|
$worksheets = $this->workbookManager->getWorksheets(); |
|
88
|
|
|
|
|
89
|
|
|
/** @var Worksheet $worksheet */ |
|
90
|
12 |
|
foreach ($worksheets as $worksheet) { |
|
91
|
12 |
|
$externalSheets[] = $worksheet->getExternalSheet(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
12 |
|
return $externalSheets; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Creates a new sheet and make it the current sheet. The data will now be written to this sheet. |
|
99
|
|
|
* |
|
100
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
101
|
|
|
* @return Sheet The created sheet |
|
102
|
|
|
*/ |
|
103
|
15 |
|
public function addNewSheetAndMakeItCurrent() |
|
104
|
|
|
{ |
|
105
|
15 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
106
|
15 |
|
$worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent(); |
|
107
|
|
|
|
|
108
|
15 |
|
return $worksheet->getExternalSheet(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
55 |
|
public function startCurrentSheet() |
|
112
|
|
|
{ |
|
113
|
55 |
|
$this->workbookManager->startCurrentSheet(); |
|
114
|
55 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param Row $row |
|
118
|
|
|
* @throws WriterNotOpenedException|SpoutException |
|
119
|
|
|
* @return WriterAbstract |
|
120
|
|
|
*/ |
|
121
|
63 |
|
public function addRow(Row $row) |
|
122
|
|
|
{ |
|
123
|
63 |
|
if (!$this->workbookManager->getCurrentWorksheet()->getFilePointer()){ |
|
124
|
55 |
|
$this->startCurrentSheet(); |
|
125
|
|
|
} |
|
126
|
55 |
|
return parent::addRow($row); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns the current sheet |
|
131
|
|
|
* |
|
132
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
133
|
|
|
* @return Sheet The current sheet |
|
134
|
|
|
*/ |
|
135
|
10 |
|
public function getCurrentSheet() |
|
136
|
|
|
{ |
|
137
|
10 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
138
|
|
|
|
|
139
|
10 |
|
return $this->workbookManager->getCurrentWorksheet()->getExternalSheet(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Sets the given sheet as the current one. New data will be written to this sheet. |
|
144
|
|
|
* The writing will resume where it stopped (i.e. data won't be truncated). |
|
145
|
|
|
* |
|
146
|
|
|
* @param Sheet $sheet The sheet to set as current |
|
147
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
148
|
|
|
* @throws SheetNotFoundException If the given sheet does not exist in the workbook |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
4 |
|
public function setCurrentSheet($sheet) |
|
152
|
|
|
{ |
|
153
|
4 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
154
|
4 |
|
$this->workbookManager->setCurrentSheet($sheet); |
|
155
|
4 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Checks if the workbook has been created. Throws an exception if not created yet. |
|
159
|
|
|
* |
|
160
|
|
|
* @throws WriterNotOpenedException If the workbook is not created yet |
|
161
|
|
|
* @return void |
|
162
|
|
|
*/ |
|
163
|
61 |
|
protected function throwIfWorkbookIsNotAvailable() |
|
164
|
|
|
{ |
|
165
|
61 |
|
if (!$this->workbookManager->getWorkbook()) { |
|
166
|
|
|
throw new WriterNotOpenedException('The writer must be opened before performing this action.'); |
|
167
|
|
|
} |
|
168
|
61 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritdoc} |
|
172
|
|
|
*/ |
|
173
|
55 |
|
protected function addRowToWriter(Row $row) |
|
174
|
|
|
{ |
|
175
|
55 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
176
|
55 |
|
$this->workbookManager->addRowToCurrentWorksheet($row); |
|
177
|
53 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
*/ |
|
182
|
60 |
|
protected function closeWriter() |
|
183
|
|
|
{ |
|
184
|
60 |
|
if ($this->workbookManager) { |
|
185
|
60 |
|
$this->workbookManager->close($this->filePointer); |
|
186
|
|
|
} |
|
187
|
60 |
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param string $colFrom |
|
|
|
|
|
|
191
|
|
|
* @param string $colTo |
|
|
|
|
|
|
192
|
|
|
* @param float $width |
|
193
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setColWidth(int $colFrom, int $colTo, float $width) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->workbookManager->getCurrentWorksheet()->setColWidth($colFrom, $colTo, $width); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param float $width |
|
202
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
|
203
|
|
|
*/ |
|
204
|
|
|
public function setDefaultColWidth(float $width) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->workbookManager->getCurrentWorksheet()->setDefaultColWidth($width); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param float $height |
|
211
|
|
|
* @throws \Box\Spout\Common\Exception\IOException |
|
212
|
|
|
*/ |
|
213
|
|
|
public function setDefaultRowHeight(float $height) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->workbookManager->getCurrentWorksheet()->setDefaultRowHeight($height); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $leftCell |
|
220
|
|
|
* @param string $rightCell |
|
221
|
|
|
*/ |
|
222
|
|
|
public function mergeCells(string $leftCell, string $rightCell) |
|
223
|
|
|
{ |
|
224
|
|
|
$this->workbookManager->getCurrentWorksheet()->mergeCells($leftCell,$rightCell); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.