123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /**
- * Created by PhpStorm.
- * User: phperstar
- * Date: 2020/8/10
- * Time: 4:24 PM
- */
- namespace Util\PHPExcel\Reader;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\ErrorCode;
- use Util\PHPExcel\Reader\IReader;
- use Util\PHPExcel\Reader\IReadFilter;
- abstract class ReaderAbstract implements IReader
- {
- protected $fileHandle = null;
- /**
- * Read data only?
- * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
- * or whether it should read both data and formatting
- *
- * @var boolean
- */
- protected $readDataOnly = false;
- /**
- * PHPExcel_Reader_IReadFilter instance
- *
- * @var IReadFilter
- */
- protected $readFilter;
- /**
- * Restrict which sheets should be loaded?
- * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
- *
- * @var array of string
- */
- protected $loadSheetsOnly;
- /**
- * Can the current PHPExcel_Reader_IReader read the file?
- *
- * @param string $pFilename
- * @return ResultWrapper
- */
- public function canRead($pFilename)
- {
- // Check if file exists
- $reuslt = $this->openFile($pFilename);
- if(!$reuslt->isSuccess()){
- return ResultWrapper::fail($reuslt->getData(), $reuslt->getErrorCode());
- }
- $readable = $this->isValidFormat();
- fclose($this->fileHandle);
- return ResultWrapper::success($readable);
- }
- /**
- * Open file for reading
- *
- * @param string $pFilename
- * @return ResultWrapper
- */
- protected function openFile($pFilename)
- {
- // Check if file exists
- if (!file_exists($pFilename) || !is_readable($pFilename)) {
- return ResultWrapper::fail('文件不存在',$pFilename, ErrorCode::$notAllowAccess);
- }
- // Open file
- $this->fileHandle = fopen($pFilename, 'r');
- if ($this->fileHandle === false) {
- return ResultWrapper::fail('读取文件失败',$pFilename, ErrorCode::$notAllowAccess);
- }
- }
- /**
- * 获取只读取数据模式配置
- * If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
- * If false (the default) it will read data and formatting.
- *
- * @return boolean
- */
- public function getReadDataOnly()
- {
- return $this->readDataOnly;
- }
- /**
- * 设置是否只读取数据
- * Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
- * Set to false (the default) to advise the Reader to read both data and formatting for cells.
- *
- * @param boolean $pValue
- *
- * @return PHPExcel_Reader_IReader
- */
- public function setReadDataOnly($pValue = false)
- {
- $this->readDataOnly = $pValue;
- return $this;
- }
- /**
- * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
- *
- * @param string $xml
- * @throws PHPExcel_Reader_Exception
- */
- public function securityScan($xml)
- {
- $pattern = '/\\0?' . implode('\\0?', str_split('<!DOCTYPE')) . '\\0?/';
- if (preg_match($pattern, $xml)) {
- throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
- }
- return $xml;
- }
- /**
- * Read filter
- *
- * @return PHPExcel_Reader_IReadFilter
- */
- public function getReadFilter()
- {
- return $this->readFilter;
- }
- /**
- * Set read filter
- *
- * @param PHPExcel_Reader_IReadFilter $pValue
- * @return PHPExcel_Reader_IReader
- */
- public function setReadFilter(IReadFilter $pValue)
- {
- $this->readFilter = $pValue;
- return $this;
- }
- /**
- * Get which sheets to load
- * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
- * indicating that all worksheets in the workbook should be loaded.
- *
- * @return mixed
- */
- public function getLoadSheetsOnly()
- {
- return $this->loadSheetsOnly;
- }
- /**
- * Set which sheets to load
- *
- * @param mixed $value
- * This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
- * If NULL, then it tells the Reader to read all worksheets in the workbook
- *
- * @return PHPExcel_Reader_IReader
- */
- public function setLoadSheetsOnly($value = null)
- {
- if ($value === null) {
- return $this->setLoadAllSheets();
- }
- $this->loadSheetsOnly = is_array($value) ? $value : array($value);
- return $this;
- }
- /**
- * Set all sheets to load
- * Tells the Reader to load all worksheets from the workbook.
- *
- * @return PHPExcel_Reader_IReader
- */
- public function setLoadAllSheets()
- {
- $this->loadSheetsOnly = null;
- return $this;
- }
- }
|