| 1 |
<?php |
| 2 |
|
| 3 |
namespace Box\Spout\Writer; |
| 4 |
|
| 5 |
use Box\Spout\Writer\Exception\WriterNotOpenedException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class AbstractMultiSheetsWriter |
| 9 |
* |
| 10 |
* @package Box\Spout\Writer |
| 11 |
* @abstract |
| 12 |
*/ |
| 13 |
abstract class AbstractMultiSheetsWriter extends AbstractWriter |
| 14 |
{ |
| 15 |
/** @var bool Whether new sheets should be automatically created when the max rows limit per sheet is reached */ |
| 16 |
protected $shouldCreateNewSheetsAutomatically = true; |
| 17 |
|
| 18 |
/** |
| 19 |
* @return Common\Internal\WorkbookInterface The workbook representing the file to be written |
| 20 |
*/ |
| 21 |
abstract protected function getWorkbook(); |
| 22 |
|
| 23 |
/** |
| 24 |
* Sets whether new sheets should be automatically created when the max rows limit per sheet is reached. |
| 25 |
* This must be set before opening the writer. |
| 26 |
* |
| 27 |
* @api |
| 28 |
* @param bool $shouldCreateNewSheetsAutomatically Whether new sheets should be automatically created when the max rows limit per sheet is reached |
| 29 |
* @return AbstractMultiSheetsWriter |
| 30 |
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened |
| 31 |
*/ |
| 32 |
public function setShouldCreateNewSheetsAutomatically($shouldCreateNewSheetsAutomatically) |
| 33 |
{ |
| 34 |
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.'); |
| 35 |
|
| 36 |
$this->shouldCreateNewSheetsAutomatically = $shouldCreateNewSheetsAutomatically; |
| 37 |
return $this; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns all the workbook's sheets |
| 42 |
* |
| 43 |
* @api |
| 44 |
* @return Common\Sheet[] All the workbook's sheets |
| 45 |
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet |
| 46 |
*/ |
| 47 |
public function getSheets() |
| 48 |
{ |
| 49 |
$this->throwIfBookIsNotAvailable(); |
| 50 |
|
| 51 |
$externalSheets = []; |
| 52 |
$worksheets = $this->getWorkbook()->getWorksheets(); |
| 53 |
|
| 54 |
/** @var Common\Internal\WorksheetInterface $worksheet */ |
| 55 |
foreach ($worksheets as $worksheet) { |
| 56 |
$externalSheets[] = $worksheet->getExternalSheet(); |
| 57 |
} |
| 58 |
|
| 59 |
return $externalSheets; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Creates a new sheet and make it the current sheet. The data will now be written to this sheet. |
| 64 |
* |
| 65 |
* @api |
| 66 |
* @return Common\Sheet The created sheet |
| 67 |
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet |
| 68 |
*/ |
| 69 |
public function addNewSheetAndMakeItCurrent() |
| 70 |
{ |
| 71 |
$this->throwIfBookIsNotAvailable(); |
| 72 |
$worksheet = $this->getWorkbook()->addNewSheetAndMakeItCurrent(); |
| 73 |
|
| 74 |
return $worksheet->getExternalSheet(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the current sheet |
| 79 |
* |
| 80 |
* @api |
| 81 |
* @return Common\Sheet The current sheet |
| 82 |
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet |
| 83 |
*/ |
| 84 |
public function getCurrentSheet() |
| 85 |
{ |
| 86 |
$this->throwIfBookIsNotAvailable(); |
| 87 |
return $this->getWorkbook()->getCurrentWorksheet()->getExternalSheet(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Sets the given sheet as the current one. New data will be written to this sheet. |
| 92 |
* The writing will resume where it stopped (i.e. data won't be truncated). |
| 93 |
* |
| 94 |
* @api |
| 95 |
* @param Common\Sheet $sheet The sheet to set as current |
| 96 |
* @return void |
| 97 |
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the writer has not been opened yet |
| 98 |
* @throws \Box\Spout\Writer\Exception\SheetNotFoundException If the given sheet does not exist in the workbook |
| 99 |
*/ |
| 100 |
public function setCurrentSheet($sheet) |
| 101 |
{ |
| 102 |
$this->throwIfBookIsNotAvailable(); |
| 103 |
$this->getWorkbook()->setCurrentSheet($sheet); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Checks if the book has been created. Throws an exception if not created yet. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
* @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If the book is not created yet |
| 111 |
*/ |
| 112 |
protected function throwIfBookIsNotAvailable() |
| 113 |
{ |
| 114 |
if (!$this->getWorkbook()) { |
| 115 |
throw new WriterNotOpenedException('The writer must be opened before performing this action.'); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|