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