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.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader;
   4  
   5  use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
   6  use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
   7  use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
   8  use PhpOffice\PhpSpreadsheet\Shared\File;
   9  use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10  
  11  abstract class BaseReader implements IReader
  12  {
  13      /**
  14       * Read data only?
  15       * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  16       *        or whether it should read both data and formatting.
  17       *
  18       * @var bool
  19       */
  20      protected $readDataOnly = false;
  21  
  22      /**
  23       * Read empty cells?
  24       * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
  25       *         null value or empty string.
  26       *
  27       * @var bool
  28       */
  29      protected $readEmptyCells = true;
  30  
  31      /**
  32       * Read charts that are defined in the workbook?
  33       * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
  34       *
  35       * @var bool
  36       */
  37      protected $includeCharts = false;
  38  
  39      /**
  40       * Restrict which sheets should be loaded?
  41       * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  42       *
  43       * @var null|string[]
  44       */
  45      protected $loadSheetsOnly;
  46  
  47      /**
  48       * IReadFilter instance.
  49       *
  50       * @var IReadFilter
  51       */
  52      protected $readFilter;
  53  
  54      protected $fileHandle;
  55  
  56      /**
  57       * @var XmlScanner
  58       */
  59      protected $securityScanner;
  60  
  61      public function __construct()
  62      {
  63          $this->readFilter = new DefaultReadFilter();
  64      }
  65  
  66      public function getReadDataOnly()
  67      {
  68          return $this->readDataOnly;
  69      }
  70  
  71      public function setReadDataOnly($readCellValuesOnly)
  72      {
  73          $this->readDataOnly = (bool) $readCellValuesOnly;
  74  
  75          return $this;
  76      }
  77  
  78      public function getReadEmptyCells()
  79      {
  80          return $this->readEmptyCells;
  81      }
  82  
  83      public function setReadEmptyCells($readEmptyCells)
  84      {
  85          $this->readEmptyCells = (bool) $readEmptyCells;
  86  
  87          return $this;
  88      }
  89  
  90      public function getIncludeCharts()
  91      {
  92          return $this->includeCharts;
  93      }
  94  
  95      public function setIncludeCharts($includeCharts)
  96      {
  97          $this->includeCharts = (bool) $includeCharts;
  98  
  99          return $this;
 100      }
 101  
 102      public function getLoadSheetsOnly()
 103      {
 104          return $this->loadSheetsOnly;
 105      }
 106  
 107      public function setLoadSheetsOnly($sheetList)
 108      {
 109          if ($sheetList === null) {
 110              return $this->setLoadAllSheets();
 111          }
 112  
 113          $this->loadSheetsOnly = is_array($sheetList) ? $sheetList : [$sheetList];
 114  
 115          return $this;
 116      }
 117  
 118      public function setLoadAllSheets()
 119      {
 120          $this->loadSheetsOnly = null;
 121  
 122          return $this;
 123      }
 124  
 125      public function getReadFilter()
 126      {
 127          return $this->readFilter;
 128      }
 129  
 130      public function setReadFilter(IReadFilter $readFilter)
 131      {
 132          $this->readFilter = $readFilter;
 133  
 134          return $this;
 135      }
 136  
 137      public function getSecurityScanner()
 138      {
 139          return $this->securityScanner;
 140      }
 141  
 142      protected function processFlags(int $flags): void
 143      {
 144          if (((bool) ($flags & self::LOAD_WITH_CHARTS)) === true) {
 145              $this->setIncludeCharts(true);
 146          }
 147      }
 148  
 149      protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
 150      {
 151          throw new PhpSpreadsheetException('Reader classes must implement their own loadSpreadsheetFromFile() method');
 152      }
 153  
 154      /**
 155       * Loads Spreadsheet from file.
 156       *
 157       * @param int $flags the optional second parameter flags may be used to identify specific elements
 158       *                       that should be loaded, but which won't be loaded by default, using these values:
 159       *                            IReader::LOAD_WITH_CHARTS - Include any charts that are defined in the loaded file
 160       */
 161      public function load(string $filename, int $flags = 0): Spreadsheet
 162      {
 163          $this->processFlags($flags);
 164  
 165          try {
 166              return $this->loadSpreadsheetFromFile($filename);
 167          } catch (ReaderException $e) {
 168              throw $e;
 169          }
 170      }
 171  
 172      /**
 173       * Open file for reading.
 174       */
 175      protected function openFile(string $filename): void
 176      {
 177          $fileHandle = false;
 178          if ($filename) {
 179              File::assertFile($filename);
 180  
 181              // Open file
 182              $fileHandle = fopen($filename, 'rb');
 183          }
 184          if ($fileHandle === false) {
 185              throw new ReaderException('Could not open file ' . $filename . ' for reading.');
 186          }
 187  
 188          $this->fileHandle = $fileHandle;
 189      }
 190  }