2014-03-10 22:41:41 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
2014-03-27 23:55:06 +07:00
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
|
|
|
|
* @copyright 2014 PHPWord
|
|
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Reader;
|
|
|
|
|
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-03-15 09:27:48 -04:00
|
|
|
|
2014-03-10 22:41:41 +07:00
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Reader abstract class
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @codeCoverageIgnore Abstract class
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-04-08 00:23:49 +07:00
|
|
|
abstract class AbstractReader implements ReaderInterface
|
2014-03-10 22:41:41 +07:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Read data only?
|
|
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @var bool
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-03-11 19:26:56 +07:00
|
|
|
protected $readDataOnly = true;
|
2014-03-10 22:41:41 +07:00
|
|
|
|
2014-03-15 09:27:48 -04:00
|
|
|
/**
|
2014-03-17 07:26:25 +07:00
|
|
|
* File pointer
|
|
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @var bool|resource
|
|
|
|
|
*/
|
2014-03-11 19:26:56 +07:00
|
|
|
protected $fileHandle = true;
|
2014-03-10 22:41:41 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Read data only?
|
|
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @return bool
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-03-11 19:26:56 +07:00
|
|
|
public function getReadDataOnly()
|
|
|
|
|
{
|
2014-03-11 02:43:04 +07:00
|
|
|
// return $this->readDataOnly;
|
2014-03-11 19:26:56 +07:00
|
|
|
return true;
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set read data only
|
|
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @param bool $pValue
|
2014-04-08 00:23:49 +07:00
|
|
|
* @return \PhpOffice\PhpWord\Reader\ReaderInterface
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-03-11 19:26:56 +07:00
|
|
|
public function setReadDataOnly($pValue = true)
|
|
|
|
|
{
|
2014-03-11 02:43:04 +07:00
|
|
|
$this->readDataOnly = $pValue;
|
2014-03-10 22:41:41 +07:00
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Open file for reading
|
|
|
|
|
*
|
|
|
|
|
* @param string $pFilename
|
|
|
|
|
* @return resource
|
2014-03-31 01:13:02 +07:00
|
|
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
2014-03-11 02:43:04 +07:00
|
|
|
protected function openFile($pFilename)
|
2014-03-10 22:41:41 +07:00
|
|
|
{
|
|
|
|
|
// Check if file exists
|
2014-04-05 22:53:39 +07:00
|
|
|
if (!file_exists($pFilename) || !is_readable($pFilename)) {
|
2014-03-15 09:27:48 -04:00
|
|
|
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open file
|
2014-03-11 02:43:04 +07:00
|
|
|
$this->fileHandle = fopen($pFilename, 'r');
|
2014-03-11 19:26:56 +07:00
|
|
|
if ($this->fileHandle === false) {
|
2014-03-15 09:27:48 -04:00
|
|
|
throw new Exception("Could not open file " . $pFilename . " for reading.");
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-08 00:23:49 +07:00
|
|
|
* Can the current ReaderInterface read the file?
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
2014-03-15 09:27:48 -04:00
|
|
|
* @param string $pFilename
|
|
|
|
|
* @return bool
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
|
|
|
|
public function canRead($pFilename)
|
|
|
|
|
{
|
|
|
|
|
// Check if file exists
|
|
|
|
|
try {
|
2014-03-11 02:43:04 +07:00
|
|
|
$this->openFile($pFilename);
|
2014-03-10 22:41:41 +07:00
|
|
|
} catch (Exception $e) {
|
2014-03-11 19:26:56 +07:00
|
|
|
return false;
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
2014-03-11 19:26:56 +07:00
|
|
|
fclose($this->fileHandle);
|
2014-03-15 09:27:48 -04:00
|
|
|
return true;
|
2014-03-10 22:41:41 +07:00
|
|
|
}
|
2014-03-23 17:37:26 +07:00
|
|
|
}
|