Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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