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 StyleHelperInterface */ |
||
35 | protected $styleHelper; |
||
36 | |||
37 | /** @var FileSystemWithRootFolderHelperInterface Helper to perform file system operations */ |
||
38 | protected $fileSystemHelper; |
||
39 | |||
40 | /** @var EntityFactory Factory to create entities */ |
||
41 | private $entityFactory; |
||
42 | |||
43 | /** @var Worksheet The worksheet where data will be written to */ |
||
44 | protected $currentWorksheet; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @param Workbook $workbook |
||
49 | * @param OptionsManagerInterface $optionsManager |
||
50 | * @param WorksheetManagerInterface $worksheetManager |
||
51 | * @param StyleHelperInterface $styleHelper |
||
52 | * @param FileSystemWithRootFolderHelperInterface $fileSystemHelper |
||
53 | * @param EntityFactory $entityFactory |
||
54 | */ |
||
55 | 89 | public function __construct( |
|
70 | |||
71 | /** |
||
72 | * @return int Maximum number of rows/columns a sheet can contain |
||
73 | */ |
||
74 | abstract protected function getMaxRowsPerWorksheet(); |
||
75 | |||
76 | /** |
||
77 | * @param Sheet $sheet |
||
78 | * @return string The file path where the data for the given sheet will be stored |
||
79 | */ |
||
80 | abstract protected function getWorksheetFilePath(Sheet $sheet); |
||
81 | |||
82 | /** |
||
83 | * @return Workbook |
||
84 | */ |
||
85 | 70 | public function getWorkbook() |
|
89 | |||
90 | /** |
||
91 | * Creates a new sheet in the workbook and make it the current sheet. |
||
92 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
93 | * |
||
94 | * @return Worksheet The created sheet |
||
95 | * @throws IOException If unable to open the sheet for writing |
||
96 | */ |
||
97 | 89 | public function addNewSheetAndMakeItCurrent() |
|
104 | |||
105 | /** |
||
106 | * Creates a new sheet in the workbook. The current sheet remains unchanged. |
||
107 | * |
||
108 | * @return Worksheet The created sheet |
||
109 | * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
||
110 | */ |
||
111 | 89 | private function addNewSheet() |
|
128 | |||
129 | /** |
||
130 | * @return Worksheet[] All the workbook's sheets |
||
131 | */ |
||
132 | 89 | public function getWorksheets() |
|
136 | |||
137 | /** |
||
138 | * Returns the current sheet |
||
139 | * |
||
140 | * @return Worksheet The current sheet |
||
141 | */ |
||
142 | 70 | public function getCurrentWorksheet() |
|
146 | |||
147 | /** |
||
148 | * Sets the given sheet as the current one. New data will be written to this sheet. |
||
149 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
150 | * |
||
151 | * @param Sheet $sheet The "external" sheet to set as current |
||
152 | * @return void |
||
153 | * @throws SheetNotFoundException If the given sheet does not exist in the workbook |
||
154 | */ |
||
155 | 4 | public function setCurrentSheet(Sheet $sheet) |
|
164 | |||
165 | /** |
||
166 | * @param Worksheet $worksheet |
||
167 | * @return void |
||
168 | */ |
||
169 | 89 | private function setCurrentWorksheet($worksheet) |
|
173 | |||
174 | /** |
||
175 | * Returns the worksheet associated to the given external sheet. |
||
176 | * |
||
177 | * @param Sheet $sheet |
||
178 | * @return Worksheet|null The worksheet associated to the given external sheet or null if not found. |
||
179 | */ |
||
180 | 4 | private function getWorksheetFromExternalSheet($sheet) |
|
193 | |||
194 | /** |
||
195 | * Adds data to the current sheet. |
||
196 | * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
||
197 | * with the creation of new worksheets if one worksheet has reached its maximum capicity. |
||
198 | * |
||
199 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
200 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
201 | * @param Style $style Style to be applied to the row. |
||
202 | * @return void |
||
203 | * @throws IOException If trying to create a new sheet and unable to open the sheet for writing |
||
204 | * @throws WriterException If unable to write data |
||
205 | */ |
||
206 | 64 | public function addRowToCurrentWorksheet($dataRow, Style $style) |
|
225 | |||
226 | /** |
||
227 | * @return bool Whether the current worksheet has reached the maximum number of rows per sheet. |
||
228 | */ |
||
229 | 64 | private function hasCurrentWorkseetReachedMaxRows() |
|
234 | |||
235 | /** |
||
236 | * Adds data with the given style to the given sheet. |
||
237 | * |
||
238 | * @param Worksheet $worksheet Worksheet to write the row to |
||
239 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
240 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
241 | * @param Style $style Style to be applied to the row. |
||
242 | * @return void |
||
243 | * @throws WriterException If unable to write data |
||
244 | */ |
||
245 | 64 | private function addRowWithStyleToWorksheet(Worksheet $worksheet, $dataRow, Style $style) |
|
256 | |||
257 | /** |
||
258 | * Closes the workbook and all its associated sheets. |
||
259 | * All the necessary files are written to disk and zipped together to create the final file. |
||
260 | * All the temporary files are then deleted. |
||
261 | * |
||
262 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
263 | * @return void |
||
264 | */ |
||
265 | 70 | public function close($finalFilePointer) |
|
272 | |||
273 | /** |
||
274 | * Closes custom objects that are still opened |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | 34 | protected function closeRemainingObjects() |
|
282 | |||
283 | /** |
||
284 | * Writes all the necessary files to disk and zip them together to create the final file. |
||
285 | * |
||
286 | * @param resource $finalFilePointer Pointer to the spreadsheet that will be created |
||
287 | * @return void |
||
288 | */ |
||
289 | abstract protected function writeAllFilesToDiskAndZipThem($finalFilePointer); |
||
290 | |||
291 | /** |
||
292 | * Closes all workbook's associated sheets. |
||
293 | * |
||
294 | * @return void |
||
295 | */ |
||
296 | 70 | private function closeAllWorksheets() |
|
304 | |||
305 | /** |
||
306 | * Deletes the root folder created in the temp folder and all its contents. |
||
307 | * |
||
308 | * @return void |
||
309 | */ |
||
310 | 70 | protected function cleanupTempFolder() |
|
315 | } |