[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/spout/src/Spout/Writer/Common/Internal/ -> AbstractWorkbook.php (source)

   1  <?php
   2  
   3  namespace Box\Spout\Writer\Common\Internal;
   4  
   5  use Box\Spout\Writer\Exception\SheetNotFoundException;
   6  
   7  /**
   8   * Class Workbook
   9   * Represents a workbook within a spreadsheet file.
  10   * It provides the functions to work with worksheets.
  11   *
  12   * @package Box\Spout\Writer\Common
  13   */
  14  abstract class AbstractWorkbook implements WorkbookInterface
  15  {
  16      /** @var bool Whether new sheets should be automatically created when the max rows limit per sheet is reached */
  17      protected $shouldCreateNewSheetsAutomatically;
  18  
  19      /** @var WorksheetInterface[] Array containing the workbook's sheets */
  20      protected $worksheets = [];
  21  
  22      /** @var WorksheetInterface The worksheet where data will be written to */
  23      protected $currentWorksheet;
  24  
  25      /**
  26       * @param bool $shouldCreateNewSheetsAutomatically
  27       * @param \Box\Spout\Writer\Style\Style $defaultRowStyle
  28       * @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders
  29       */
  30      public function __construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle)
  31      {
  32          $this->shouldCreateNewSheetsAutomatically = $shouldCreateNewSheetsAutomatically;
  33      }
  34  
  35      /**
  36       * @return \Box\Spout\Writer\Common\Helper\AbstractStyleHelper The specific style helper
  37       */
  38      abstract protected function getStyleHelper();
  39  
  40      /**
  41       * @return int Maximum number of rows/columns a sheet can contain
  42       */
  43      abstract protected function getMaxRowsPerWorksheet();
  44  
  45      /**
  46       * Creates a new sheet in the workbook. The current sheet remains unchanged.
  47       *
  48       * @return WorksheetInterface The created sheet
  49       * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing
  50       */
  51      abstract public function addNewSheet();
  52  
  53      /**
  54       * Creates a new sheet in the workbook and make it the current sheet.
  55       * The writing will resume where it stopped (i.e. data won't be truncated).
  56       *
  57       * @return WorksheetInterface The created sheet
  58       * @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing
  59       */
  60      public function addNewSheetAndMakeItCurrent()
  61      {
  62          $worksheet = $this->addNewSheet();
  63          $this->setCurrentWorksheet($worksheet);
  64  
  65          return $worksheet;
  66      }
  67  
  68      /**
  69       * @return WorksheetInterface[] All the workbook's sheets
  70       */
  71      public function getWorksheets()
  72      {
  73          return $this->worksheets;
  74      }
  75  
  76      /**
  77       * Returns the current sheet
  78       *
  79       * @return WorksheetInterface The current sheet
  80       */
  81      public function getCurrentWorksheet()
  82      {
  83          return $this->currentWorksheet;
  84      }
  85  
  86      /**
  87       * Sets the given sheet as the current one. New data will be written to this sheet.
  88       * The writing will resume where it stopped (i.e. data won't be truncated).
  89       *
  90       * @param \Box\Spout\Writer\Common\Sheet $sheet The "external" sheet to set as current
  91       * @return void
  92       * @throws \Box\Spout\Writer\Exception\SheetNotFoundException If the given sheet does not exist in the workbook
  93       */
  94      public function setCurrentSheet($sheet)
  95      {
  96          $worksheet = $this->getWorksheetFromExternalSheet($sheet);
  97          if ($worksheet !== null) {
  98              $this->currentWorksheet = $worksheet;
  99          } else {
 100              throw new SheetNotFoundException('The given sheet does not exist in the workbook.');
 101          }
 102      }
 103  
 104      /**
 105       * @param WorksheetInterface $worksheet
 106       * @return void
 107       */
 108      protected function setCurrentWorksheet($worksheet)
 109      {
 110          $this->currentWorksheet = $worksheet;
 111      }
 112  
 113      /**
 114       * Returns the worksheet associated to the given external sheet.
 115       *
 116       * @param \Box\Spout\Writer\Common\Sheet $sheet
 117       * @return WorksheetInterface|null The worksheet associated to the given external sheet or null if not found.
 118       */
 119      protected function getWorksheetFromExternalSheet($sheet)
 120      {
 121          $worksheetFound = null;
 122  
 123          foreach ($this->worksheets as $worksheet) {
 124              if ($worksheet->getExternalSheet() === $sheet) {
 125                  $worksheetFound = $worksheet;
 126                  break;
 127              }
 128          }
 129  
 130          return $worksheetFound;
 131      }
 132  
 133      /**
 134       * Adds data to the current sheet.
 135       * If shouldCreateNewSheetsAutomatically option is set to true, it will handle pagination
 136       * with the creation of new worksheets if one worksheet has reached its maximum capicity.
 137       *
 138       * @param array $dataRow Array containing data to be written. Cannot be empty.
 139       *          Example $dataRow = ['data1', 1234, null, '', 'data5'];
 140       * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row.
 141       * @return void
 142       * @throws \Box\Spout\Common\Exception\IOException If trying to create a new sheet and unable to open the sheet for writing
 143       * @throws \Box\Spout\Writer\Exception\WriterException If unable to write data
 144       */
 145      public function addRowToCurrentWorksheet($dataRow, $style)
 146      {
 147          $currentWorksheet = $this->getCurrentWorksheet();
 148          $hasReachedMaxRows = $this->hasCurrentWorkseetReachedMaxRows();
 149          $styleHelper = $this->getStyleHelper();
 150  
 151          // if we reached the maximum number of rows for the current sheet...
 152          if ($hasReachedMaxRows) {
 153              // ... continue writing in a new sheet if option set
 154              if ($this->shouldCreateNewSheetsAutomatically) {
 155                  $currentWorksheet = $this->addNewSheetAndMakeItCurrent();
 156  
 157                  $updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, $dataRow);
 158                  $registeredStyle = $styleHelper->registerStyle($updatedStyle);
 159                  $currentWorksheet->addRow($dataRow, $registeredStyle);
 160              } else {
 161                  // otherwise, do nothing as the data won't be read anyways
 162              }
 163          } else {
 164              $updatedStyle = $styleHelper->applyExtraStylesIfNeeded($style, $dataRow);
 165              $registeredStyle = $styleHelper->registerStyle($updatedStyle);
 166              $currentWorksheet->addRow($dataRow, $registeredStyle);
 167          }
 168      }
 169  
 170      /**
 171       * @return bool Whether the current worksheet has reached the maximum number of rows per sheet.
 172       */
 173      protected function hasCurrentWorkseetReachedMaxRows()
 174      {
 175          $currentWorksheet = $this->getCurrentWorksheet();
 176          return ($currentWorksheet->getLastWrittenRowIndex() >= $this->getMaxRowsPerWorksheet());
 177      }
 178  
 179      /**
 180       * Closes the workbook and all its associated sheets.
 181       * All the necessary files are written to disk and zipped together to create the ODS file.
 182       * All the temporary files are then deleted.
 183       *
 184       * @param resource $finalFilePointer Pointer to the ODS that will be created
 185       * @return void
 186       */
 187      abstract public function close($finalFilePointer);
 188  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1