1 | <?php |
||
24 | abstract class WorkbookManagerAbstract implements WorkbookManagerInterface |
||
25 | { |
||
26 | /** @var Workbook The workbook to manage */ |
||
27 | protected $workbook; |
||
28 | |||
29 | /** @var OptionsManagerInterface */ |
||
30 | protected $optionsManager; |
||
31 | |||
32 | /** @var WorksheetManagerInterface */ |
||
33 | protected $worksheetManager; |
||
34 | |||
35 | /** @var StyleManagerInterface Manages styles */ |
||
36 | protected $styleManager; |
||
37 | |||
38 | /** @var StyleMerger Helper to merge styles */ |
||
39 | protected $styleMerger; |
||
40 | |||
41 | /** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */ |
||
42 | protected $fileSystemHelper; |
||
43 | |||
44 | /** @var InternalEntityFactory Factory to create entities */ |
||
45 | protected $entityFactory; |
||
46 | |||
47 | /** @var ManagerFactoryInterface $managerFactory Factory to create managers */ |
||
48 | protected $managerFactory; |
||
49 | |||
50 | /** @var Worksheet The worksheet where data will be written to */ |
||
51 | protected $currentWorksheet; |
||
52 | |||
53 | /** |
||
54 | * @param Workbook $workbook |
||
55 | * @param OptionsManagerInterface $optionsManager |
||
56 | * @param WorksheetManagerInterface $worksheetManager |
||
57 | * @param StyleManagerInterface $styleManager |
||
58 | * @param StyleMerger $styleMerger |
||
59 | * @param FileSystemWithRootFolderHelperInterface $fileSystemHelper |
||
60 | * @param InternalEntityFactory $entityFactory |
||
61 | * @param ManagerFactoryInterface $managerFactory |
||
62 | */ |
||
63 | 73 | public function __construct( |
|
82 | |||
83 | /** |
||
84 | * @return int Maximum number of rows/columns a sheet can contain |
||
85 | */ |
||
86 | abstract protected function getMaxRowsPerWorksheet(); |
||
87 | |||
88 | /** |
||
89 | * @param Sheet $sheet |
||
90 | * @return string The file path where the data for the given sheet will be stored |
||
91 | */ |
||
92 | abstract protected function getWorksheetFilePath(Sheet $sheet); |
||
93 | |||
94 | /** |
||
95 | * @return Workbook |
||
96 | */ |
||
97 | 65 | public function getWorkbook() |
|
101 | |||
102 | /** |
||
103 | * Creates a new sheet in the workbook and make it the current sheet. |
||
104 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
105 | * |
||
106 | * @throws IOException If unable to open the sheet for writing |
||
107 | * @return Worksheet The created sheet |
||
108 | */ |
||
109 | 73 | public function addNewSheetAndMakeItCurrent() |
|
116 | |||
117 | /** |
||
118 | * Creates a new sheet in the workbook. The current sheet remains unchanged. |
||
119 | * |
||
120 | * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
||
121 | * @return Worksheet The created sheet |
||
122 | */ |
||
123 | 73 | private function addNewSheet() |
|
141 | |||
142 | /** |
||
143 | * @return Worksheet[] All the workbook's sheets |
||
144 | */ |
||
145 | 73 | public function getWorksheets() |
|
149 | |||
150 | /** |
||
151 | * Returns the current sheet |
||
152 | * |
||
153 | * @return Worksheet The current sheet |
||
154 | */ |
||
155 | 65 | public function getCurrentWorksheet() |
|
159 | |||
160 | /** |
||
161 | * Sets the given sheet as the current one. New data will be written to this sheet. |
||
162 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
163 | * |
||
164 | * @param Sheet $sheet The "external" sheet to set as current |
||
165 | * @throws SheetNotFoundException If the given sheet does not exist in the workbook |
||
166 | * @return void |
||
167 | */ |
||
168 | 4 | public function setCurrentSheet(Sheet $sheet) |
|
177 | |||
178 | /** |
||
179 | * @param Worksheet $worksheet |
||
180 | * @return void |
||
181 | */ |
||
182 | 73 | private function setCurrentWorksheet($worksheet) |
|
186 | |||
187 | /** |
||
188 | * Returns the worksheet associated to the given external sheet. |
||
189 | * |
||
190 | * @param Sheet $sheet |
||
191 | * @return Worksheet|null The worksheet associated to the given external sheet or null if not found. |
||
192 | */ |
||
193 | 4 | private function getWorksheetFromExternalSheet($sheet) |
|
206 | |||
207 | /** |
||
208 | * Adds a row to the current sheet. |
||
209 | * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
||
210 | * with the creation of new worksheets if one worksheet has reached its maximum capicity. |
||
211 | * |
||
212 | * @param Row $row The row to be added |
||
213 | * @throws IOException If trying to create a new sheet and unable to open the sheet for writing |
||
214 | * @throws WriterException If unable to write data |
||
215 | * @return void |
||
216 | */ |
||
217 | 59 | public function addRowToCurrentWorksheet(Row $row) |
|
236 | |||
237 | /** |
||
238 | * @return bool Whether the current worksheet has reached the maximum number of rows per sheet. |
||
239 | */ |
||
240 | 59 | private function hasCurrentWorksheetReachedMaxRows() |
|
246 | |||
247 | /** |
||
248 | * Adds a row to the given sheet. |
||
249 | * |
||
250 | * @param Worksheet $worksheet Worksheet to write the row to |
||
251 | * @param Row $row The row to be added |
||
252 | * @throws WriterException If unable to write data |
||
253 | * @return void |
||
254 | */ |
||
255 | 59 | private function addRowToWorksheet(Worksheet $worksheet, Row $row) |
|
265 | |||
266 | /** |
||
267 | * @param Row $row |
||
268 | */ |
||
269 | 59 | private function applyDefaultRowStyle(Row $row) |
|
278 | |||
279 | /** |
||
280 | * Closes the workbook and all its associated sheets. |
||
281 | * All the necessary files are written to disk and zipped together to create the final file. |
||
282 | * All the temporary files are then deleted. |
||
283 | * |
||
284 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
285 | * @return void |
||
286 | */ |
||
287 | 66 | public function close($finalFilePointer) |
|
294 | |||
295 | /** |
||
296 | * Closes custom objects that are still opened |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | 32 | protected function closeRemainingObjects() |
|
304 | |||
305 | /** |
||
306 | * Writes all the necessary files to disk and zip them together to create the final file. |
||
307 | * |
||
308 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
309 | * @return void |
||
310 | */ |
||
311 | abstract protected function writeAllFilesToDiskAndZipThem($finalFilePointer); |
||
312 | |||
313 | /** |
||
314 | * Closes all workbook's associated sheets. |
||
315 | * |
||
316 | * @return void |
||
317 | */ |
||
318 | 66 | private function closeAllWorksheets() |
|
326 | |||
327 | /** |
||
328 | * Deletes the root folder created in the temp folder and all its contents. |
||
329 | * |
||
330 | * @return void |
||
331 | */ |
||
332 | 66 | protected function cleanupTempFolder() |
|
337 | } |
||
338 |