vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/BaseReader.php line 177

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