BaseReader.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Reader;
  3. use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
  4. use PhpOffice\PhpSpreadsheet\Shared\File;
  5. abstract class BaseReader implements IReader
  6. {
  7. /**
  8. * Read data only?
  9. * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
  10. * or whether it should read both data and formatting.
  11. *
  12. * @var bool
  13. */
  14. protected $readDataOnly = false;
  15. /**
  16. * Read empty cells?
  17. * Identifies whether the Reader should read data values for cells all cells, or should ignore cells containing
  18. * null value or empty string.
  19. *
  20. * @var bool
  21. */
  22. protected $readEmptyCells = true;
  23. /**
  24. * Read charts that are defined in the workbook?
  25. * Identifies whether the Reader should read the definitions for any charts that exist in the workbook;.
  26. *
  27. * @var bool
  28. */
  29. protected $includeCharts = false;
  30. /**
  31. * Restrict which sheets should be loaded?
  32. * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
  33. *
  34. * @var array of string
  35. */
  36. protected $loadSheetsOnly;
  37. /**
  38. * IReadFilter instance.
  39. *
  40. * @var IReadFilter
  41. */
  42. protected $readFilter;
  43. protected $fileHandle;
  44. /**
  45. * @var XmlScanner
  46. */
  47. protected $securityScanner;
  48. /**
  49. * Read data only?
  50. * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
  51. * If false (the default) it will read data and formatting.
  52. *
  53. * @return bool
  54. */
  55. public function getReadDataOnly()
  56. {
  57. return $this->readDataOnly;
  58. }
  59. /**
  60. * Set read data only
  61. * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
  62. * Set to false (the default) to advise the Reader to read both data and formatting for cells.
  63. *
  64. * @param bool $pValue
  65. *
  66. * @return IReader
  67. */
  68. public function setReadDataOnly($pValue)
  69. {
  70. $this->readDataOnly = (bool) $pValue;
  71. return $this;
  72. }
  73. /**
  74. * Read empty cells?
  75. * If this is true (the default), then the Reader will read data values for all cells, irrespective of value.
  76. * If false it will not read data for cells containing a null value or an empty string.
  77. *
  78. * @return bool
  79. */
  80. public function getReadEmptyCells()
  81. {
  82. return $this->readEmptyCells;
  83. }
  84. /**
  85. * Set read empty cells
  86. * Set to true (the default) to advise the Reader read data values for all cells, irrespective of value.
  87. * Set to false to advise the Reader to ignore cells containing a null value or an empty string.
  88. *
  89. * @param bool $pValue
  90. *
  91. * @return IReader
  92. */
  93. public function setReadEmptyCells($pValue)
  94. {
  95. $this->readEmptyCells = (bool) $pValue;
  96. return $this;
  97. }
  98. /**
  99. * Read charts in workbook?
  100. * If this is true, then the Reader will include any charts that exist in the workbook.
  101. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  102. * If false (the default) it will ignore any charts defined in the workbook file.
  103. *
  104. * @return bool
  105. */
  106. public function getIncludeCharts()
  107. {
  108. return $this->includeCharts;
  109. }
  110. /**
  111. * Set read charts in workbook
  112. * Set to true, to advise the Reader to include any charts that exist in the workbook.
  113. * Note that a ReadDataOnly value of false overrides, and charts won't be read regardless of the IncludeCharts value.
  114. * Set to false (the default) to discard charts.
  115. *
  116. * @param bool $pValue
  117. *
  118. * @return IReader
  119. */
  120. public function setIncludeCharts($pValue)
  121. {
  122. $this->includeCharts = (bool) $pValue;
  123. return $this;
  124. }
  125. /**
  126. * Get which sheets to load
  127. * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
  128. * indicating that all worksheets in the workbook should be loaded.
  129. *
  130. * @return mixed
  131. */
  132. public function getLoadSheetsOnly()
  133. {
  134. return $this->loadSheetsOnly;
  135. }
  136. /**
  137. * Set which sheets to load.
  138. *
  139. * @param mixed $value
  140. * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
  141. * If NULL, then it tells the Reader to read all worksheets in the workbook
  142. *
  143. * @return IReader
  144. */
  145. public function setLoadSheetsOnly($value)
  146. {
  147. if ($value === null) {
  148. return $this->setLoadAllSheets();
  149. }
  150. $this->loadSheetsOnly = is_array($value) ? $value : [$value];
  151. return $this;
  152. }
  153. /**
  154. * Set all sheets to load
  155. * Tells the Reader to load all worksheets from the workbook.
  156. *
  157. * @return IReader
  158. */
  159. public function setLoadAllSheets()
  160. {
  161. $this->loadSheetsOnly = null;
  162. return $this;
  163. }
  164. /**
  165. * Read filter.
  166. *
  167. * @return IReadFilter
  168. */
  169. public function getReadFilter()
  170. {
  171. return $this->readFilter;
  172. }
  173. /**
  174. * Set read filter.
  175. *
  176. * @param IReadFilter $pValue
  177. *
  178. * @return IReader
  179. */
  180. public function setReadFilter(IReadFilter $pValue)
  181. {
  182. $this->readFilter = $pValue;
  183. return $this;
  184. }
  185. public function getSecuritySCanner()
  186. {
  187. if (property_exists($this, 'securityScanner')) {
  188. return $this->securityScanner;
  189. }
  190. return null;
  191. }
  192. /**
  193. * Open file for reading.
  194. *
  195. * @param string $pFilename
  196. *
  197. * @throws Exception
  198. */
  199. protected function openFile($pFilename)
  200. {
  201. File::assertFile($pFilename);
  202. // Open file
  203. $this->fileHandle = fopen($pFilename, 'r');
  204. if ($this->fileHandle === false) {
  205. throw new Exception('Could not open file ' . $pFilename . ' for reading.');
  206. }
  207. }
  208. }