1 | <?php |
||
23 | abstract class WorkbookManagerAbstract implements WorkbookManagerInterface |
||
24 | { |
||
25 | /** @var Workbook The workbook to manage */ |
||
26 | protected $workbook; |
||
27 | |||
28 | /** @var OptionsManagerInterface */ |
||
29 | protected $optionManager; |
||
30 | |||
31 | /** @var WorksheetManagerInterface */ |
||
32 | protected $worksheetManager; |
||
33 | |||
34 | /** @var StyleManagerInterface Manages styles */ |
||
35 | protected $styleManager; |
||
36 | |||
37 | /** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */ |
||
38 | protected $fileSystemHelper; |
||
39 | |||
40 | /** @var EntityFactory Factory to create entities */ |
||
41 | protected $entityFactory; |
||
42 | |||
43 | /** @var ManagerFactoryInterface $managerFactory Factory to create managers */ |
||
44 | protected $managerFactory; |
||
45 | |||
46 | /** @var Worksheet The worksheet where data will be written to */ |
||
47 | protected $currentWorksheet; |
||
48 | |||
49 | /** |
||
50 | * @param Workbook $workbook |
||
51 | * @param OptionsManagerInterface $optionsManager |
||
52 | * @param WorksheetManagerInterface $worksheetManager |
||
53 | * @param StyleManagerInterface $styleManager |
||
54 | * @param FileSystemWithRootFolderHelperInterface $fileSystemHelper |
||
55 | * @param EntityFactory $entityFactory |
||
56 | * @param ManagerFactoryInterface $managerFactory |
||
57 | */ |
||
58 | 84 | public function __construct( |
|
75 | |||
76 | /** |
||
77 | * @return int Maximum number of rows/columns a sheet can contain |
||
78 | */ |
||
79 | abstract protected function getMaxRowsPerWorksheet(); |
||
80 | |||
81 | /** |
||
82 | * @param Sheet $sheet |
||
83 | * @return string The file path where the data for the given sheet will be stored |
||
84 | */ |
||
85 | abstract protected function getWorksheetFilePath(Sheet $sheet); |
||
86 | |||
87 | /** |
||
88 | * @return Workbook |
||
89 | */ |
||
90 | 67 | public function getWorkbook() |
|
94 | |||
95 | /** |
||
96 | * Creates a new sheet in the workbook and make it the current sheet. |
||
97 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
98 | * |
||
99 | * @throws IOException If unable to open the sheet for writing |
||
100 | * @return Worksheet The created sheet |
||
101 | */ |
||
102 | 84 | public function addNewSheetAndMakeItCurrent() |
|
109 | |||
110 | /** |
||
111 | * Creates a new sheet in the workbook. The current sheet remains unchanged. |
||
112 | * |
||
113 | * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
||
114 | * @return Worksheet The created sheet |
||
115 | */ |
||
116 | 84 | private function addNewSheet() |
|
134 | |||
135 | /** |
||
136 | * @return Worksheet[] All the workbook's sheets |
||
137 | */ |
||
138 | 84 | public function getWorksheets() |
|
142 | |||
143 | /** |
||
144 | * Returns the current sheet |
||
145 | * |
||
146 | * @return Worksheet The current sheet |
||
147 | */ |
||
148 | 67 | public function getCurrentWorksheet() |
|
152 | |||
153 | /** |
||
154 | * Sets the given sheet as the current one. New data will be written to this sheet. |
||
155 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
156 | * |
||
157 | * @param Sheet $sheet The "external" sheet to set as current |
||
158 | * @throws SheetNotFoundException If the given sheet does not exist in the workbook |
||
159 | * @return void |
||
160 | */ |
||
161 | 4 | public function setCurrentSheet(Sheet $sheet) |
|
170 | |||
171 | /** |
||
172 | * @param Worksheet $worksheet |
||
173 | * @return void |
||
174 | */ |
||
175 | 84 | private function setCurrentWorksheet($worksheet) |
|
179 | |||
180 | /** |
||
181 | * Returns the worksheet associated to the given external sheet. |
||
182 | * |
||
183 | * @param Sheet $sheet |
||
184 | * @return Worksheet|null The worksheet associated to the given external sheet or null if not found. |
||
185 | */ |
||
186 | 4 | private function getWorksheetFromExternalSheet($sheet) |
|
199 | |||
200 | /** |
||
201 | * Adds a row to the current sheet. |
||
202 | * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
||
203 | * with the creation of new worksheets if one worksheet has reached its maximum capicity. |
||
204 | * |
||
205 | * @param Row $row The row to added |
||
206 | * @return void |
||
207 | * @throws IOException If trying to create a new sheet and unable to open the sheet for writing |
||
208 | * @throws WriterException If unable to write data |
||
209 | * @return void |
||
210 | */ |
||
211 | 61 | public function addRowToCurrentWorksheet(Row $row) |
|
230 | |||
231 | /** |
||
232 | * @return bool Whether the current worksheet has reached the maximum number of rows per sheet. |
||
233 | */ |
||
234 | 61 | private function hasCurrentWorksheetReachedMaxRows() |
|
240 | |||
241 | /** |
||
242 | * Adds data with the given style to the given sheet. |
||
243 | * |
||
244 | * @param Worksheet $worksheet Worksheet to write the row to |
||
245 | * @param Row $row The row to be added |
||
246 | * @return void |
||
247 | * @throws WriterException If unable to write data |
||
248 | * @return void |
||
249 | */ |
||
250 | 61 | private function addRowToWorksheet(Worksheet $worksheet, Row $row) |
|
259 | |||
260 | /** |
||
261 | * Closes the workbook and all its associated sheets. |
||
262 | * All the necessary files are written to disk and zipped together to create the final file. |
||
263 | * All the temporary files are then deleted. |
||
264 | * |
||
265 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
266 | * @return void |
||
267 | */ |
||
268 | 69 | public function close($finalFilePointer) |
|
275 | |||
276 | /** |
||
277 | * Closes custom objects that are still opened |
||
278 | * |
||
279 | * @return void |
||
280 | */ |
||
281 | 34 | protected function closeRemainingObjects() |
|
285 | |||
286 | /** |
||
287 | * Writes all the necessary files to disk and zip them together to create the final file. |
||
288 | * |
||
289 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
290 | * @return void |
||
291 | */ |
||
292 | abstract protected function writeAllFilesToDiskAndZipThem($finalFilePointer); |
||
293 | |||
294 | /** |
||
295 | * Closes all workbook's associated sheets. |
||
296 | * |
||
297 | * @return void |
||
298 | */ |
||
299 | 69 | private function closeAllWorksheets() |
|
307 | |||
308 | /** |
||
309 | * Deletes the root folder created in the temp folder and all its contents. |
||
310 | * |
||
311 | * @return void |
||
312 | */ |
||
313 | 69 | protected function cleanupTempFolder() |
|
318 | } |
||
319 |