Differences Between: [Versions 402 and 403]
1 <?php 2 3 declare(strict_types=1); 4 5 namespace OpenSpout\Writer; 6 7 use OpenSpout\Common\Entity\Row; 8 use OpenSpout\Common\Exception\IOException; 9 use OpenSpout\Writer\Common\Entity\Sheet; 10 use OpenSpout\Writer\Common\Manager\WorkbookManagerInterface; 11 use OpenSpout\Writer\Exception\SheetNotFoundException; 12 use OpenSpout\Writer\Exception\WriterNotOpenedException; 13 14 abstract class AbstractWriterMultiSheets extends AbstractWriter 15 { 16 private WorkbookManagerInterface $workbookManager; 17 18 /** 19 * Returns all the workbook's sheets. 20 * 21 * @return Sheet[] All the workbook's sheets 22 * 23 * @throws WriterNotOpenedException If the writer has not been opened yet 24 */ 25 final public function getSheets(): array 26 { 27 $this->throwIfWorkbookIsNotAvailable(); 28 29 $externalSheets = []; 30 $worksheets = $this->workbookManager->getWorksheets(); 31 32 foreach ($worksheets as $worksheet) { 33 $externalSheets[] = $worksheet->getExternalSheet(); 34 } 35 36 return $externalSheets; 37 } 38 39 /** 40 * Creates a new sheet and make it the current sheet. The data will now be written to this sheet. 41 * 42 * @return Sheet The created sheet 43 * 44 * @throws IOException 45 * @throws WriterNotOpenedException If the writer has not been opened yet 46 */ 47 final public function addNewSheetAndMakeItCurrent(): Sheet 48 { 49 $this->throwIfWorkbookIsNotAvailable(); 50 $worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent(); 51 52 return $worksheet->getExternalSheet(); 53 } 54 55 /** 56 * Returns the current sheet. 57 * 58 * @return Sheet The current sheet 59 * 60 * @throws WriterNotOpenedException If the writer has not been opened yet 61 */ 62 final public function getCurrentSheet(): Sheet 63 { 64 $this->throwIfWorkbookIsNotAvailable(); 65 66 return $this->workbookManager->getCurrentWorksheet()->getExternalSheet(); 67 } 68 69 /** 70 * Sets the given sheet as the current one. New data will be written to this sheet. 71 * The writing will resume where it stopped (i.e. data won't be truncated). 72 * 73 * @param Sheet $sheet The sheet to set as current 74 * 75 * @throws SheetNotFoundException If the given sheet does not exist in the workbook 76 * @throws WriterNotOpenedException If the writer has not been opened yet 77 */ 78 final public function setCurrentSheet(Sheet $sheet): void 79 { 80 $this->throwIfWorkbookIsNotAvailable(); 81 $this->workbookManager->setCurrentSheet($sheet); 82 } 83 84 abstract protected function createWorkbookManager(): WorkbookManagerInterface; 85 86 /** 87 * {@inheritdoc} 88 */ 89 protected function openWriter(): void 90 { 91 if (!isset($this->workbookManager)) { 92 $this->workbookManager = $this->createWorkbookManager(); 93 $this->workbookManager->addNewSheetAndMakeItCurrent(); 94 } 95 } 96 97 /** 98 * {@inheritdoc} 99 * 100 * @throws Exception\WriterException 101 */ 102 protected function addRowToWriter(Row $row): void 103 { 104 $this->throwIfWorkbookIsNotAvailable(); 105 $this->workbookManager->addRowToCurrentWorksheet($row); 106 } 107 108 /** 109 * {@inheritdoc} 110 */ 111 protected function closeWriter(): void 112 { 113 if (isset($this->workbookManager)) { 114 $this->workbookManager->close($this->filePointer); 115 } 116 } 117 118 /** 119 * Checks if the workbook has been created. Throws an exception if not created yet. 120 * 121 * @throws WriterNotOpenedException If the workbook is not created yet 122 */ 123 private function throwIfWorkbookIsNotAvailable(): void 124 { 125 if (!isset($this->workbookManager)) { 126 throw new WriterNotOpenedException('The writer must be opened before performing this action.'); 127 } 128 } 129 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body