| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\OpenSpout\Writer; |
| 4 |
|
| 5 |
use FluentCart\OpenSpout\Common\Creator\HelperFactory; |
| 6 |
use FluentCart\OpenSpout\Common\Entity\Row; |
| 7 |
use FluentCart\OpenSpout\Common\Exception\IOException; |
| 8 |
use FluentCart\OpenSpout\Common\Helper\GlobalFunctionsHelper; |
| 9 |
use FluentCart\OpenSpout\Common\Manager\OptionsManagerInterface; |
| 10 |
use FluentCart\OpenSpout\Writer\Common\Creator\ManagerFactoryInterface; |
| 11 |
use FluentCart\OpenSpout\Writer\Common\Entity\Options; |
| 12 |
use FluentCart\OpenSpout\Writer\Common\Entity\Sheet; |
| 13 |
use FluentCart\OpenSpout\Writer\Common\Manager\WorkbookManagerInterface; |
| 14 |
use FluentCart\OpenSpout\Writer\Exception\SheetNotFoundException; |
| 15 |
use FluentCart\OpenSpout\Writer\Exception\WriterAlreadyOpenedException; |
| 16 |
use FluentCart\OpenSpout\Writer\Exception\WriterNotOpenedException; |
| 17 |
abstract class WriterMultiSheetsAbstract extends WriterAbstract |
| 18 |
{ |
| 19 |
/** @var ManagerFactoryInterface */ |
| 20 |
private $managerFactory; |
| 21 |
/** @var null|WorkbookManagerInterface */ |
| 22 |
private $workbookManager; |
| 23 |
public function __construct(OptionsManagerInterface $optionsManager, GlobalFunctionsHelper $globalFunctionsHelper, HelperFactory $helperFactory, ManagerFactoryInterface $managerFactory) |
| 24 |
{ |
| 25 |
parent::__construct($optionsManager, $globalFunctionsHelper, $helperFactory); |
| 26 |
$this->managerFactory = $managerFactory; |
| 27 |
} |
| 28 |
/** |
| 29 |
* Sets whether new sheets should be automatically created when the max rows limit per sheet is reached. |
| 30 |
* This must be set before opening the writer. |
| 31 |
* |
| 32 |
* @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached |
| 33 |
* |
| 34 |
* @throws WriterAlreadyOpenedException If the writer was already opened |
| 35 |
* |
| 36 |
* @return WriterMultiSheetsAbstract |
| 37 |
*/ |
| 38 |
public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) |
| 39 |
{ |
| 40 |
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
| 41 |
$this->optionsManager->setOption(Options::SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY, $shouldCreateNewSheetsAutomatically); |
| 42 |
return $this; |
| 43 |
} |
| 44 |
/** |
| 45 |
* Returns all the workbook's sheets. |
| 46 |
* |
| 47 |
* @throws WriterNotOpenedException If the writer has not been opened yet |
| 48 |
* |
| 49 |
* @return Sheet[] All the workbook's sheets |
| 50 |
*/ |
| 51 |
public function getSheets() |
| 52 |
{ |
| 53 |
$this->throwIfWorkbookIsNotAvailable(); |
| 54 |
$externalSheets = []; |
| 55 |
$worksheets = $this->workbookManager->getWorksheets(); |
| 56 |
foreach ($worksheets as $worksheet) { |
| 57 |
$externalSheets[] = $worksheet->getExternalSheet(); |
| 58 |
} |
| 59 |
return $externalSheets; |
| 60 |
} |
| 61 |
/** |
| 62 |
* Creates a new sheet and make it the current sheet. The data will now be written to this sheet. |
| 63 |
* |
| 64 |
* @throws IOException |
| 65 |
* @throws WriterNotOpenedException If the writer has not been opened yet |
| 66 |
* |
| 67 |
* @return Sheet The created sheet |
| 68 |
*/ |
| 69 |
public function addNewSheetAndMakeItCurrent() |
| 70 |
{ |
| 71 |
$this->throwIfWorkbookIsNotAvailable(); |
| 72 |
$worksheet = $this->workbookManager->addNewSheetAndMakeItCurrent(); |
| 73 |
return $worksheet->getExternalSheet(); |
| 74 |
} |
| 75 |
/** |
| 76 |
* Returns the current sheet. |
| 77 |
* |
| 78 |
* @throws WriterNotOpenedException If the writer has not been opened yet |
| 79 |
* |
| 80 |
* @return Sheet The current sheet |
| 81 |
*/ |
| 82 |
public function getCurrentSheet() |
| 83 |
{ |
| 84 |
$this->throwIfWorkbookIsNotAvailable(); |
| 85 |
return $this->workbookManager->getCurrentWorksheet()->getExternalSheet(); |
| 86 |
} |
| 87 |
/** |
| 88 |
* Sets the given sheet as the current one. New data will be written to this sheet. |
| 89 |
* The writing will resume where it stopped (i.e. data won't be truncated). |
| 90 |
* |
| 91 |
* @param Sheet $sheet The sheet to set as current |
| 92 |
* |
| 93 |
* @throws SheetNotFoundException If the given sheet does not exist in the workbook |
| 94 |
* @throws WriterNotOpenedException If the writer has not been opened yet |
| 95 |
*/ |
| 96 |
public function setCurrentSheet($sheet) |
| 97 |
{ |
| 98 |
$this->throwIfWorkbookIsNotAvailable(); |
| 99 |
$this->workbookManager->setCurrentSheet($sheet); |
| 100 |
} |
| 101 |
/** |
| 102 |
* @throws WriterAlreadyOpenedException |
| 103 |
*/ |
| 104 |
public function setDefaultColumnWidth(float $width) |
| 105 |
{ |
| 106 |
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
| 107 |
$this->optionsManager->setOption(Options::DEFAULT_COLUMN_WIDTH, $width); |
| 108 |
} |
| 109 |
/** |
| 110 |
* @throws WriterAlreadyOpenedException |
| 111 |
*/ |
| 112 |
public function setDefaultRowHeight(float $height) |
| 113 |
{ |
| 114 |
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
| 115 |
$this->optionsManager->setOption(Options::DEFAULT_ROW_HEIGHT, $height); |
| 116 |
} |
| 117 |
/** |
| 118 |
* @param null|float $width |
| 119 |
* @param int ...$columns One or more columns with this width |
| 120 |
* |
| 121 |
* @throws WriterNotOpenedException |
| 122 |
*/ |
| 123 |
public function setColumnWidth($width, ...$columns) |
| 124 |
{ |
| 125 |
$this->throwIfWorkbookIsNotAvailable(); |
| 126 |
$this->workbookManager->setColumnWidth($width, ...$columns); |
| 127 |
} |
| 128 |
/** |
| 129 |
* @param float $width The width to set |
| 130 |
* @param int $start First column index of the range |
| 131 |
* @param int $end Last column index of the range |
| 132 |
* |
| 133 |
* @throws WriterNotOpenedException |
| 134 |
*/ |
| 135 |
public function setColumnWidthForRange(float $width, int $start, int $end) |
| 136 |
{ |
| 137 |
$this->throwIfWorkbookIsNotAvailable(); |
| 138 |
$this->workbookManager->setColumnWidthForRange($width, $start, $end); |
| 139 |
} |
| 140 |
/** |
| 141 |
* {@inheritdoc} |
| 142 |
*/ |
| 143 |
protected function openWriter() |
| 144 |
{ |
| 145 |
if (null === $this->workbookManager) { |
| 146 |
$this->workbookManager = $this->managerFactory->createWorkbookManager($this->optionsManager); |
| 147 |
$this->workbookManager->addNewSheetAndMakeItCurrent(); |
| 148 |
} |
| 149 |
} |
| 150 |
/** |
| 151 |
* Checks if the workbook has been created. Throws an exception if not created yet. |
| 152 |
* |
| 153 |
* @throws WriterNotOpenedException If the workbook is not created yet |
| 154 |
*/ |
| 155 |
protected function throwIfWorkbookIsNotAvailable() |
| 156 |
{ |
| 157 |
if (!$this->workbookManager->getWorkbook()) { |
| 158 |
throw new WriterNotOpenedException('The writer must be opened before performing this action.'); |
| 159 |
} |
| 160 |
} |
| 161 |
/** |
| 162 |
* {@inheritdoc} |
| 163 |
* |
| 164 |
* @throws Exception\WriterException |
| 165 |
*/ |
| 166 |
protected function addRowToWriter(Row $row) |
| 167 |
{ |
| 168 |
$this->throwIfWorkbookIsNotAvailable(); |
| 169 |
$this->workbookManager->addRowToCurrentWorksheet($row); |
| 170 |
} |
| 171 |
/** |
| 172 |
* {@inheritdoc} |
| 173 |
*/ |
| 174 |
protected function closeWriter() |
| 175 |
{ |
| 176 |
if (null !== $this->workbookManager) { |
| 177 |
$this->workbookManager->close($this->filePointer); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|