Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Reader;
   4  
   5  use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
   6  use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
   7  use PhpOffice\PhpSpreadsheet\Shared\File;
   8  
   9  abstract class BaseReader implements IReader
  10  {
  11      /**
  12       * Read data only?
  13       * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  14       *        or whether it should read both data and formatting.
  15       *
  16       * @var bool
  17       */
  18      protected $readDataOnly = false;
  19  
  20      /**
  21       * Read empty cells?
  22       * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
  23       *         null value or empty string.
  24       *
  25       * @var bool
  26       */
  27      protected $readEmptyCells = true;
  28  
  29      /**
  30       * Read charts that are defined in the workbook?
  31       * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
  32       *
  33       * @var bool
  34       */
  35      protected $includeCharts = false;
  36  
  37      /**
  38       * Restrict which sheets should be loaded?
  39       * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  40       *
  41       * @var array of string
  42       */
  43      protected $loadSheetsOnly;
  44  
  45      /**
  46       * IReadFilter instance.
  47       *
  48       * @var IReadFilter
  49       */
  50      protected $readFilter;
  51  
  52      protected $fileHandle;
  53  
  54      /**
  55       * @var XmlScanner
  56       */
  57      protected $securityScanner;
  58  
  59      public function __construct()
  60      {
  61          $this->readFilter = new DefaultReadFilter();
  62      }
  63  
  64      public function getReadDataOnly()
  65      {
  66          return $this->readDataOnly;
  67      }
  68  
  69      public function setReadDataOnly($pValue)
  70      {
  71          $this->readDataOnly = (bool) $pValue;
  72  
  73          return $this;
  74      }
  75  
  76      public function getReadEmptyCells()
  77      {
  78          return $this->readEmptyCells;
  79      }
  80  
  81      public function setReadEmptyCells($pValue)
  82      {
  83          $this->readEmptyCells = (bool) $pValue;
  84  
  85          return $this;
  86      }
  87  
  88      public function getIncludeCharts()
  89      {
  90          return $this->includeCharts;
  91      }
  92  
  93      public function setIncludeCharts($pValue)
  94      {
  95          $this->includeCharts = (bool) $pValue;
  96  
  97          return $this;
  98      }
  99  
 100      public function getLoadSheetsOnly()
 101      {
 102          return $this->loadSheetsOnly;
 103      }
 104  
 105      public function setLoadSheetsOnly($value)
 106      {
 107          if ($value === null) {
 108              return $this->setLoadAllSheets();
 109          }
 110  
 111          $this->loadSheetsOnly = is_array($value) ? $value : [$value];
 112  
 113          return $this;
 114      }
 115  
 116      public function setLoadAllSheets()
 117      {
 118          $this->loadSheetsOnly = null;
 119  
 120          return $this;
 121      }
 122  
 123      public function getReadFilter()
 124      {
 125          return $this->readFilter;
 126      }
 127  
 128      public function setReadFilter(IReadFilter $pValue)
 129      {
 130          $this->readFilter = $pValue;
 131  
 132          return $this;
 133      }
 134  
 135      public function getSecurityScanner()
 136      {
 137          return $this->securityScanner;
 138      }
 139  
 140      /**
 141       * Open file for reading.
 142       *
 143       * @param string $pFilename
 144       */
 145      protected function openFile($pFilename): void
 146      {
 147          if ($pFilename) {
 148              File::assertFile($pFilename);
 149  
 150              // Open file
 151              $fileHandle = fopen($pFilename, 'rb');
 152          } else {
 153              $fileHandle = false;
 154          }
 155          if ($fileHandle !== false) {
 156              $this->fileHandle = $fileHandle;
 157          } else {
 158              throw new ReaderException('Could not open file ' . $pFilename . ' for reading.');
 159          }
 160      }
 161  }