1 | <?php |
||
25 | abstract class WorkbookManagerAbstract implements WorkbookManagerInterface |
||
26 | { |
||
27 | /** @var Workbook The workbook to manage */ |
||
28 | protected $workbook; |
||
29 | |||
30 | /** @var OptionsManagerInterface */ |
||
31 | protected $optionManager; |
||
32 | |||
33 | /** @var WorksheetManagerInterface */ |
||
34 | protected $worksheetManager; |
||
35 | |||
36 | /** @var StyleManagerInterface Manages styles */ |
||
37 | protected $styleManager; |
||
38 | |||
39 | /** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */ |
||
40 | protected $fileSystemHelper; |
||
41 | |||
42 | /** @var EntityFactory Factory to create entities */ |
||
43 | protected $entityFactory; |
||
44 | |||
45 | /** @var ManagerFactoryInterface $managerFactory Factory to create managers */ |
||
46 | protected $managerFactory; |
||
47 | |||
48 | /** @var Worksheet The worksheet where data will be written to */ |
||
49 | protected $currentWorksheet; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @param Workbook $workbook |
||
54 | * @param OptionsManagerInterface $optionsManager |
||
55 | * @param WorksheetManagerInterface $worksheetManager |
||
56 | * @param StyleManagerInterface $styleManager |
||
57 | * @param FileSystemWithRootFolderHelperInterface $fileSystemHelper |
||
58 | * @param EntityFactory $entityFactory |
||
59 | * @param ManagerFactoryInterface $managerFactory |
||
60 | */ |
||
61 | 89 | public function __construct( |
|
78 | |||
79 | /** |
||
80 | * @return int Maximum number of rows/columns a sheet can contain |
||
81 | */ |
||
82 | abstract protected function getMaxRowsPerWorksheet(); |
||
83 | |||
84 | /** |
||
85 | * @param Sheet $sheet |
||
86 | * @return string The file path where the data for the given sheet will be stored |
||
87 | */ |
||
88 | abstract protected function getWorksheetFilePath(Sheet $sheet); |
||
89 | |||
90 | /** |
||
91 | * @return Workbook |
||
92 | */ |
||
93 | 70 | public function getWorkbook() |
|
97 | |||
98 | /** |
||
99 | * Creates a new sheet in the workbook and make it the current sheet. |
||
100 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
101 | * |
||
102 | * @return Worksheet The created sheet |
||
103 | * @throws IOException If unable to open the sheet for writing |
||
104 | */ |
||
105 | 89 | public function addNewSheetAndMakeItCurrent() |
|
112 | |||
113 | /** |
||
114 | * Creates a new sheet in the workbook. The current sheet remains unchanged. |
||
115 | * |
||
116 | * @return Worksheet The created sheet |
||
117 | * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
||
118 | */ |
||
119 | 89 | private function addNewSheet() |
|
137 | |||
138 | /** |
||
139 | * @return Worksheet[] All the workbook's sheets |
||
140 | */ |
||
141 | 89 | public function getWorksheets() |
|
145 | |||
146 | /** |
||
147 | * Returns the current sheet |
||
148 | * |
||
149 | * @return Worksheet The current sheet |
||
150 | */ |
||
151 | 70 | public function getCurrentWorksheet() |
|
155 | |||
156 | /** |
||
157 | * Sets the given sheet as the current one. New data will be written to this sheet. |
||
158 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
159 | * |
||
160 | * @param Sheet $sheet The "external" sheet to set as current |
||
161 | * @return void |
||
162 | * @throws SheetNotFoundException If the given sheet does not exist in the workbook |
||
163 | */ |
||
164 | 4 | public function setCurrentSheet(Sheet $sheet) |
|
173 | |||
174 | /** |
||
175 | * @param Worksheet $worksheet |
||
176 | * @return void |
||
177 | */ |
||
178 | 89 | private function setCurrentWorksheet($worksheet) |
|
182 | |||
183 | /** |
||
184 | * Returns the worksheet associated to the given external sheet. |
||
185 | * |
||
186 | * @param Sheet $sheet |
||
187 | * @return Worksheet|null The worksheet associated to the given external sheet or null if not found. |
||
188 | */ |
||
189 | 4 | private function getWorksheetFromExternalSheet($sheet) |
|
202 | |||
203 | /** |
||
204 | * Adds data to the current sheet. |
||
205 | * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
||
206 | * with the creation of new worksheets if one worksheet has reached its maximum capicity. |
||
207 | * |
||
208 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
209 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
210 | * @param Style $style Style to be applied to the row. |
||
211 | * @return void |
||
212 | * @throws IOException If trying to create a new sheet and unable to open the sheet for writing |
||
213 | * @throws WriterException If unable to write data |
||
214 | */ |
||
215 | 64 | public function addRowToCurrentWorksheet($dataRow, Style $style) |
|
234 | |||
235 | /** |
||
236 | * @return bool Whether the current worksheet has reached the maximum number of rows per sheet. |
||
237 | */ |
||
238 | 64 | private function hasCurrentWorkseetReachedMaxRows() |
|
243 | |||
244 | /** |
||
245 | * Adds data with the given style to the given sheet. |
||
246 | * |
||
247 | * @param Worksheet $worksheet Worksheet to write the row to |
||
248 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
249 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
250 | * @param Style $style Style to be applied to the row. |
||
251 | * @return void |
||
252 | * @throws WriterException If unable to write data |
||
253 | */ |
||
254 | 64 | private function addRowWithStyleToWorksheet(Worksheet $worksheet, $dataRow, Style $style) |
|
265 | |||
266 | /** |
||
267 | * Closes the workbook and all its associated sheets. |
||
268 | * All the necessary files are written to disk and zipped together to create the final file. |
||
269 | * All the temporary files are then deleted. |
||
270 | * |
||
271 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
272 | * @return void |
||
273 | */ |
||
274 | 70 | public function close($finalFilePointer) |
|
281 | |||
282 | /** |
||
283 | * Closes custom objects that are still opened |
||
284 | * |
||
285 | * @return void |
||
286 | */ |
||
287 | 34 | protected function closeRemainingObjects() |
|
291 | |||
292 | /** |
||
293 | * Writes all the necessary files to disk and zip them together to create the final file. |
||
294 | * |
||
295 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
296 | * @return void |
||
297 | */ |
||
298 | abstract protected function writeAllFilesToDiskAndZipThem($finalFilePointer); |
||
299 | |||
300 | /** |
||
301 | * Closes all workbook's associated sheets. |
||
302 | * |
||
303 | * @return void |
||
304 | */ |
||
305 | 70 | private function closeAllWorksheets() |
|
313 | |||
314 | /** |
||
315 | * Deletes the root folder created in the temp folder and all its contents. |
||
316 | * |
||
317 | * @return void |
||
318 | */ |
||
319 | 70 | protected function cleanupTempFolder() |
|
324 | } |
||
325 |