1 | <?php |
||
14 | abstract class AbstractWorkbook implements WorkbookInterface |
||
15 | { |
||
16 | /** @var bool Whether new sheets should be automatically created when the max rows limit per sheet is reached */ |
||
17 | protected $shouldCreateNewSheetsAutomatically; |
||
18 | |||
19 | /** @var WorksheetInterface[] Array containing the workbook's sheets */ |
||
20 | protected $worksheets = []; |
||
21 | |||
22 | /** @var WorksheetInterface The worksheet where data will be written to */ |
||
23 | protected $currentWorksheet; |
||
24 | |||
25 | /** |
||
26 | * @param bool $shouldCreateNewSheetsAutomatically |
||
27 | * @param \Box\Spout\Writer\Style\Style $defaultRowStyle |
||
28 | * @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders |
||
29 | */ |
||
30 | 249 | public function __construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle) |
|
34 | |||
35 | /** |
||
36 | * @return \Box\Spout\Writer\Common\Helper\AbstractStyleHelper The specific style helper |
||
37 | */ |
||
38 | abstract protected function getStyleHelper(); |
||
39 | |||
40 | /** |
||
41 | * @return int Maximum number of rows/columns a sheet can contain |
||
42 | */ |
||
43 | abstract protected function getMaxRowsPerWorksheet(); |
||
44 | |||
45 | /** |
||
46 | * Creates a new sheet in the workbook. The current sheet remains unchanged. |
||
47 | * |
||
48 | * @return WorksheetInterface The created sheet |
||
49 | * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
||
50 | */ |
||
51 | abstract public function addNewSheet(); |
||
52 | |||
53 | /** |
||
54 | * Creates a new sheet in the workbook and make it the current sheet. |
||
55 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
56 | * |
||
57 | * @return WorksheetInterface The created sheet |
||
58 | * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
||
59 | */ |
||
60 | 249 | public function addNewSheetAndMakeItCurrent() |
|
67 | |||
68 | /** |
||
69 | * @return WorksheetInterface[] All the workbook's sheets |
||
70 | */ |
||
71 | 42 | public function getWorksheets() |
|
75 | |||
76 | /** |
||
77 | * Returns the current sheet |
||
78 | * |
||
79 | * @return WorksheetInterface The current sheet |
||
80 | */ |
||
81 | 195 | public function getCurrentWorksheet() |
|
85 | |||
86 | /** |
||
87 | * Sets the given sheet as the current one. New data will be written to this sheet. |
||
88 | * The writing will resume where it stopped (i.e. data won't be truncated). |
||
89 | * |
||
90 | * @param \Box\Spout\Writer\Common\Sheet $sheet The "external" sheet to set as current |
||
91 | * @return void |
||
92 | * @throws \Box\Spout\Writer\Exception\SheetNotFoundException If the given sheet does not exist in the workbook |
||
93 | */ |
||
94 | 12 | public function setCurrentSheet($sheet) |
|
95 | { |
||
96 | 12 | $worksheet = $this->getWorksheetFromExternalSheet($sheet); |
|
97 | 12 | if ($worksheet !== null) { |
|
98 | 12 | $this->currentWorksheet = $worksheet; |
|
99 | 12 | } else { |
|
100 | 3 | throw new SheetNotFoundException('The given sheet does not exist in the workbook.'); |
|
101 | } |
||
102 | 12 | } |
|
103 | |||
104 | /** |
||
105 | * @param WorksheetInterface $worksheet |
||
106 | * @return void |
||
107 | */ |
||
108 | 249 | protected function setCurrentWorksheet($worksheet) |
|
112 | |||
113 | /** |
||
114 | * Returns the worksheet associated to the given external sheet. |
||
115 | * |
||
116 | * @param \Box\Spout\Writer\Common\Sheet $sheet |
||
117 | * @return WorksheetInterface|null The worksheet associated to the given external sheet or null if not found. |
||
118 | */ |
||
119 | 12 | protected function getWorksheetFromExternalSheet($sheet) |
|
132 | |||
133 | /** |
||
134 | * Adds data to the current sheet. |
||
135 | * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination |
||
136 | * with the creation of new worksheets if one worksheet has reached its maximum capicity. |
||
137 | * |
||
138 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
139 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
140 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. |
||
141 | * @return void |
||
142 | * @throws \Box\Spout\Common\Exception\IOException If trying to create a new sheet and unable to open the sheet for writing |
||
143 | * @throws \Box\Spout\Writer\Exception\WriterException If unable to write data |
||
144 | */ |
||
145 | 177 | public function addRowToCurrentWorksheet($dataRow, $style) |
|
169 | |||
170 | /** |
||
171 | * @return bool Whether the current worksheet has reached the maximum number of rows per sheet. |
||
172 | */ |
||
173 | 177 | protected function hasCurrentWorkseetReachedMaxRows() |
|
178 | |||
179 | /** |
||
180 | * Closes the workbook and all its associated sheets. |
||
181 | * All the necessary files are written to disk and zipped together to create the ODS file. |
||
182 | * All the temporary files are then deleted. |
||
183 | * |
||
184 | * @param resource $finalFilePointer Pointer to the ODS that will be created |
||
185 | * @return void |
||
186 | */ |
||
187 | abstract public function close($finalFilePointer); |
||
188 | } |
||
189 |