|
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\IOException; |
|
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
|
107 |
|
public function __construct( |
|
39
|
|
|
OptionsManagerInterface $optionsManager, |
|
40
|
|
|
GlobalFunctionsHelper $globalFunctionsHelper, |
|
41
|
|
|
HelperFactory $helperFactory, |
|
42
|
|
|
ManagerFactoryInterface $managerFactory |
|
43
|
|
|
) { |
|
44
|
107 |
|
parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory); |
|
45
|
107 |
|
$this->managerFactory = $managerFactory; |
|
46
|
107 |
|
} |
|
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
|
35 |
|
public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) |
|
57
|
|
|
{ |
|
58
|
35 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
|
59
|
|
|
|
|
60
|
33 |
|
$this->optionsManager->setOption( |
|
61
|
33 |
|
Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, |
|
62
|
|
|
$shouldCreateNewSheetsAutomatically |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
33 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
93 |
|
protected function openWriter() |
|
72
|
|
|
{ |
|
73
|
93 |
|
if (!$this->workbookManager) { |
|
74
|
93 |
|
$this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager); |
|
75
|
93 |
|
$this->workbookManager->addNewSheetAndMakeItCurrent(); |
|
76
|
|
|
} |
|
77
|
93 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns all the workbook's sheets |
|
81
|
|
|
* |
|
82
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
83
|
|
|
* @return Sheet[] All the workbook's sheets |
|
84
|
|
|
*/ |
|
85
|
14 |
|
public function getSheets() |
|
86
|
|
|
{ |
|
87
|
14 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
88
|
|
|
|
|
89
|
14 |
|
$externalSheets = []; |
|
90
|
14 |
|
$worksheets = $this->workbookManager->getWorksheets(); |
|
91
|
|
|
|
|
92
|
|
|
/** @var Worksheet $worksheet */ |
|
93
|
14 |
|
foreach ($worksheets as $worksheet) { |
|
94
|
14 |
|
$externalSheets[] = $worksheet->getExternalSheet(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
14 |
|
return $externalSheets; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Creates a new sheet and make it the current sheet. The data will now be written to this sheet. |
|
102
|
|
|
* |
|
103
|
|
|
* @throws IOException |
|
104
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
105
|
|
|
* @return Sheet The created sheet |
|
106
|
|
|
*/ |
|
107
|
15 |
|
public function addNewSheetAndMakeItCurrent() |
|
108
|
|
|
{ |
|
109
|
15 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
110
|
15 |
|
$worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent(); |
|
111
|
|
|
|
|
112
|
15 |
|
return $worksheet->getExternalSheet(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the current sheet |
|
117
|
|
|
* |
|
118
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
119
|
|
|
* @return Sheet The current sheet |
|
120
|
|
|
*/ |
|
121
|
10 |
|
public function getCurrentSheet() |
|
122
|
|
|
{ |
|
123
|
10 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
124
|
|
|
|
|
125
|
10 |
|
return $this->workbookManager->getCurrentWorksheet()->getExternalSheet(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Sets the given sheet as the current one. New data will be written to this sheet. |
|
130
|
|
|
* The writing will resume where it stopped (i.e. data won't be truncated). |
|
131
|
|
|
* |
|
132
|
|
|
* @param Sheet $sheet The sheet to set as current |
|
133
|
|
|
* @throws SheetNotFoundException If the given sheet does not exist in the workbook |
|
134
|
|
|
* @throws WriterNotOpenedException If the writer has not been opened yet |
|
135
|
|
|
* @return void |
|
136
|
|
|
*/ |
|
137
|
4 |
|
public function setCurrentSheet($sheet) |
|
138
|
|
|
{ |
|
139
|
4 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
140
|
4 |
|
$this->workbookManager->setCurrentSheet($sheet); |
|
141
|
4 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param float $width |
|
145
|
|
|
* @throws WriterAlreadyOpenedException |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function setDefaultColumnWidth(float $width) |
|
148
|
|
|
{ |
|
149
|
3 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
|
150
|
3 |
|
$this->optionsManager->setOption( |
|
151
|
3 |
|
Options::DEFAULT_COLUMN_WIDTH, |
|
152
|
|
|
$width |
|
153
|
|
|
); |
|
154
|
3 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param float $height |
|
158
|
|
|
* @throws WriterAlreadyOpenedException |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function setDefaultRowHeight(float $height) |
|
161
|
|
|
{ |
|
162
|
2 |
|
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
|
163
|
2 |
|
$this->optionsManager->setOption( |
|
164
|
2 |
|
Options::DEFAULT_ROW_HEIGHT, |
|
165
|
|
|
$height |
|
166
|
|
|
); |
|
167
|
2 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param float|null $width |
|
171
|
|
|
* @param array $columns One or more columns with this width |
|
172
|
|
|
* @throws WriterNotOpenedException |
|
173
|
|
|
*/ |
|
174
|
6 |
|
public function setColumnWidth($width, ...$columns) |
|
175
|
|
|
{ |
|
176
|
6 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
177
|
6 |
|
$this->workbookManager->setColumnWidth($width, ...$columns); |
|
178
|
6 |
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param float $width The width to set |
|
182
|
|
|
* @param int $start First column index of the range |
|
183
|
|
|
* @param int $end Last column index of the range |
|
184
|
|
|
* @throws WriterNotOpenedException |
|
185
|
|
|
*/ |
|
186
|
2 |
|
public function setColumnWidthForRange(float $width, int $start, int $end) |
|
187
|
|
|
{ |
|
188
|
2 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
189
|
2 |
|
$this->workbookManager->setColumnWidthForRange($width, $start, $end); |
|
190
|
2 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Checks if the workbook has been created. Throws an exception if not created yet. |
|
194
|
|
|
* |
|
195
|
|
|
* @throws WriterNotOpenedException If the workbook is not created yet |
|
196
|
|
|
* @return void |
|
197
|
|
|
*/ |
|
198
|
87 |
|
protected function throwIfWorkbookIsNotAvailable() |
|
199
|
|
|
{ |
|
200
|
87 |
|
if (empty($this->workbookManager) || !$this->workbookManager->getWorkbook()) { |
|
201
|
|
|
throw new WriterNotOpenedException('The writer must be opened before performing this action.'); |
|
202
|
|
|
} |
|
203
|
87 |
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* {@inheritdoc} |
|
207
|
|
|
* |
|
208
|
|
|
* @throws Exception\WriterException |
|
209
|
|
|
*/ |
|
210
|
81 |
|
protected function addRowToWriter(Row $row) |
|
211
|
|
|
{ |
|
212
|
81 |
|
$this->throwIfWorkbookIsNotAvailable(); |
|
213
|
81 |
|
$this->workbookManager->addRowToCurrentWorksheet($row); |
|
214
|
79 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* {@inheritdoc} |
|
218
|
|
|
*/ |
|
219
|
86 |
|
protected function closeWriter() |
|
220
|
|
|
{ |
|
221
|
86 |
|
if ($this->workbookManager) { |
|
222
|
86 |
|
$this->workbookManager->close($this->filePointer); |
|
223
|
|
|
} |
|
224
|
86 |
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|