Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Reader\ODS;
   6  
   7  use OpenSpout\Common\Exception\IOException;
   8  use OpenSpout\Common\Helper\Escaper\ODS;
   9  use OpenSpout\Reader\AbstractReader;
  10  use OpenSpout\Reader\ODS\Helper\SettingsHelper;
  11  use ZipArchive;
  12  
  13  /**
  14   * @extends AbstractReader<SheetIterator>
  15   */
  16  final class Reader extends AbstractReader
  17  {
  18      private ZipArchive $zip;
  19  
  20      private Options $options;
  21  
  22      /** @var SheetIterator To iterator over the ODS sheets */
  23      private SheetIterator $sheetIterator;
  24  
  25      public function __construct(?Options $options = null)
  26      {
  27          $this->options = $options ?? new Options();
  28      }
  29  
  30      public function getSheetIterator(): SheetIterator
  31      {
  32          $this->ensureStreamOpened();
  33  
  34          return $this->sheetIterator;
  35      }
  36  
  37      /**
  38       * Returns whether stream wrappers are supported.
  39       */
  40      protected function doesSupportStreamWrapper(): bool
  41      {
  42          return false;
  43      }
  44  
  45      /**
  46       * Opens the file at the given file path to make it ready to be read.
  47       *
  48       * @param string $filePath Path of the file to be read
  49       *
  50       * @throws \OpenSpout\Common\Exception\IOException            If the file at the given path or its content cannot be read
  51       * @throws \OpenSpout\Reader\Exception\NoSheetsFoundException If there are no sheets in the file
  52       */
  53      protected function openReader(string $filePath): void
  54      {
  55          $this->zip = new ZipArchive();
  56  
  57          if (true !== $this->zip->open($filePath)) {
  58              throw new IOException("Could not open {$filePath} for reading.");
  59          }
  60  
  61          $this->sheetIterator = new SheetIterator($filePath, $this->options, new ODS(), new SettingsHelper());
  62      }
  63  
  64      /**
  65       * Closes the reader. To be used after reading the file.
  66       */
  67      protected function closeReader(): void
  68      {
  69          $this->zip->close();
  70      }
  71  }