Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  namespace Box\Spout\Reader\ODS;
   4  
   5  use Box\Spout\Common\Exception\IOException;
   6  use Box\Spout\Reader\ODS\Creator\InternalEntityFactory;
   7  use Box\Spout\Reader\ReaderAbstract;
   8  
   9  /**
  10   * Class Reader
  11   * This class provides support to read data from a ODS file
  12   */
  13  class Reader extends ReaderAbstract
  14  {
  15      /** @var \ZipArchive */
  16      protected $zip;
  17  
  18      /** @var SheetIterator To iterator over the ODS sheets */
  19      protected $sheetIterator;
  20  
  21      /**
  22       * Returns whether stream wrappers are supported
  23       *
  24       * @return bool
  25       */
  26      protected function doesSupportStreamWrapper()
  27      {
  28          return false;
  29      }
  30  
  31      /**
  32       * Opens the file at the given file path to make it ready to be read.
  33       *
  34       * @param  string $filePath Path of the file to be read
  35       * @throws \Box\Spout\Common\Exception\IOException If the file at the given path or its content cannot be read
  36       * @throws \Box\Spout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  37       * @return void
  38       */
  39      protected function openReader($filePath)
  40      {
  41          /** @var InternalEntityFactory $entityFactory */
  42          $entityFactory = $this->entityFactory;
  43  
  44          $this->zip = $entityFactory->createZipArchive();
  45  
  46          if ($this->zip->open($filePath) === true) {
  47              /** @var InternalEntityFactory $entityFactory */
  48              $entityFactory = $this->entityFactory;
  49              $this->sheetIterator = $entityFactory->createSheetIterator($filePath, $this->optionsManager);
  50          } else {
  51              throw new IOException("Could not open $filePath for reading.");
  52          }
  53      }
  54  
  55      /**
  56       * Returns an iterator to iterate over sheets.
  57       *
  58       * @return SheetIterator To iterate over sheets
  59       */
  60      protected function getConcreteSheetIterator()
  61      {
  62          return $this->sheetIterator;
  63      }
  64  
  65      /**
  66       * Closes the reader. To be used after reading the file.
  67       *
  68       * @return void
  69       */
  70      protected function closeReader()
  71      {
  72          if ($this->zip) {
  73              $this->zip->close();
  74          }
  75      }
  76  }