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\Reader\XLSX\Creator;
   4  
   5  use Box\Spout\Common\Entity\Cell;
   6  use Box\Spout\Common\Entity\Row;
   7  use Box\Spout\Reader\Common\Creator\InternalEntityFactoryInterface;
   8  use Box\Spout\Reader\Common\Entity\Options;
   9  use Box\Spout\Reader\Common\XMLProcessor;
  10  use Box\Spout\Reader\Wrapper\XMLReader;
  11  use Box\Spout\Reader\XLSX\Manager\SharedStringsManager;
  12  use Box\Spout\Reader\XLSX\RowIterator;
  13  use Box\Spout\Reader\XLSX\Sheet;
  14  use Box\Spout\Reader\XLSX\SheetIterator;
  15  
  16  /**
  17   * Class InternalEntityFactory
  18   * Factory to create entities
  19   */
  20  class InternalEntityFactory implements InternalEntityFactoryInterface
  21  {
  22      /** @var HelperFactory */
  23      private $helperFactory;
  24  
  25      /** @var ManagerFactory */
  26      private $managerFactory;
  27  
  28      /**
  29       * @param ManagerFactory $managerFactory
  30       * @param HelperFactory $helperFactory
  31       */
  32      public function __construct(ManagerFactory $managerFactory, HelperFactory $helperFactory)
  33      {
  34          $this->managerFactory = $managerFactory;
  35          $this->helperFactory = $helperFactory;
  36      }
  37  
  38      /**
  39       * @param string $filePath Path of the file to be read
  40       * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
  41       * @param SharedStringsManager $sharedStringsManager Manages shared strings
  42       * @return SheetIterator
  43       */
  44      public function createSheetIterator($filePath, $optionsManager, $sharedStringsManager)
  45      {
  46          $sheetManager = $this->managerFactory->createSheetManager(
  47              $filePath,
  48              $optionsManager,
  49              $sharedStringsManager,
  50              $this
  51          );
  52  
  53          return new SheetIterator($sheetManager);
  54      }
  55  
  56      /**
  57       * @param string $filePath Path of the XLSX file being read
  58       * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
  59       * @param int $sheetIndex Index of the sheet, based on order in the workbook (zero-based)
  60       * @param string $sheetName Name of the sheet
  61       * @param bool $isSheetActive Whether the sheet was defined as active
  62       * @param bool $isSheetVisible Whether the sheet is visible
  63       * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
  64       * @param SharedStringsManager $sharedStringsManager Manages shared strings
  65       * @return Sheet
  66       */
  67      public function createSheet(
  68          $filePath,
  69          $sheetDataXMLFilePath,
  70          $sheetIndex,
  71          $sheetName,
  72          $isSheetActive,
  73          $isSheetVisible,
  74          $optionsManager,
  75          $sharedStringsManager
  76      ) {
  77          $rowIterator = $this->createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager);
  78  
  79          return new Sheet($rowIterator, $sheetIndex, $sheetName, $isSheetActive, $isSheetVisible);
  80      }
  81  
  82      /**
  83       * @param string $filePath Path of the XLSX file being read
  84       * @param string $sheetDataXMLFilePath Path of the sheet data XML file as in [Content_Types].xml
  85       * @param \Box\Spout\Common\Manager\OptionsManagerInterface $optionsManager Reader's options manager
  86       * @param SharedStringsManager $sharedStringsManager Manages shared strings
  87       * @return RowIterator
  88       */
  89      private function createRowIterator($filePath, $sheetDataXMLFilePath, $optionsManager, $sharedStringsManager)
  90      {
  91          $xmlReader = $this->createXMLReader();
  92          $xmlProcessor = $this->createXMLProcessor($xmlReader);
  93  
  94          $styleManager = $this->managerFactory->createStyleManager($filePath, $this);
  95          $rowManager = $this->managerFactory->createRowManager($this);
  96          $shouldFormatDates = $optionsManager->getOption(Options::SHOULD_FORMAT_DATES);
  97          $shouldUse1904Dates = $optionsManager->getOption(Options::SHOULD_USE_1904_DATES);
  98  
  99          $cellValueFormatter = $this->helperFactory->createCellValueFormatter(
 100              $sharedStringsManager,
 101              $styleManager,
 102              $shouldFormatDates,
 103              $shouldUse1904Dates
 104          );
 105  
 106          $shouldPreserveEmptyRows = $optionsManager->getOption(Options::SHOULD_PRESERVE_EMPTY_ROWS);
 107  
 108          return new RowIterator(
 109              $filePath,
 110              $sheetDataXMLFilePath,
 111              $shouldPreserveEmptyRows,
 112              $xmlReader,
 113              $xmlProcessor,
 114              $cellValueFormatter,
 115              $rowManager,
 116              $this
 117          );
 118      }
 119  
 120      /**
 121       * @param Cell[] $cells
 122       * @return Row
 123       */
 124      public function createRow(array $cells = [])
 125      {
 126          return new Row($cells, null);
 127      }
 128  
 129      /**
 130       * @param mixed $cellValue
 131       * @return Cell
 132       */
 133      public function createCell($cellValue)
 134      {
 135          return new Cell($cellValue);
 136      }
 137  
 138      /**
 139       * @return \ZipArchive
 140       */
 141      public function createZipArchive()
 142      {
 143          return new \ZipArchive();
 144      }
 145  
 146      /**
 147       * @return XMLReader
 148       */
 149      public function createXMLReader()
 150      {
 151          return new XMLReader();
 152      }
 153  
 154      /**
 155       * @param $xmlReader
 156       * @return XMLProcessor
 157       */
 158      public function createXMLProcessor($xmlReader)
 159      {
 160          return new XMLProcessor($xmlReader);
 161      }
 162  }