PHPWord/src/PhpWord/Reader/AbstractReader.php

97 lines
2.0 KiB
PHP
Raw Normal View History

2014-03-10 22:41:41 +07:00
<?php
/**
* 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
*/
namespace PhpOffice\PhpWord\Reader;
use PhpOffice\PhpWord\Exception\Exception;
2014-03-10 22:41:41 +07:00
/**
* Reader abstract class
*
* @codeCoverageIgnore Abstract class
2014-03-10 22:41:41 +07:00
*/
abstract class AbstractReader implements ReaderInterface
2014-03-10 22:41:41 +07:00
{
/**
* Read data only?
*
* @var bool
2014-03-10 22:41:41 +07:00
*/
protected $readDataOnly = true;
2014-03-10 22:41:41 +07:00
/**
2014-03-17 07:26:25 +07:00
* File pointer
*
* @var bool|resource
*/
protected $fileHandle = true;
2014-03-10 22:41:41 +07:00
/**
* Read data only?
*
* @return bool
2014-03-10 22:41:41 +07:00
*/
public function getReadDataOnly()
{
// return $this->readDataOnly;
return true;
2014-03-10 22:41:41 +07:00
}
/**
* Set read data only
*
* @param bool $pValue
* @return \PhpOffice\PhpWord\Reader\ReaderInterface
2014-03-10 22:41:41 +07:00
*/
public function setReadDataOnly($pValue = true)
{
$this->readDataOnly = $pValue;
2014-03-10 22:41:41 +07:00
return $this;
}
/**
* Open file for reading
*
* @param string $pFilename
* @return resource
* @throws \PhpOffice\PhpWord\Exception\Exception
2014-03-10 22:41:41 +07:00
*/
protected function openFile($pFilename)
2014-03-10 22:41:41 +07:00
{
// Check if file exists
if (!file_exists($pFilename) || !is_readable($pFilename)) {
throw new Exception("Could not open " . $pFilename . " for reading! File does not exist.");
2014-03-10 22:41:41 +07:00
}
// Open file
$this->fileHandle = fopen($pFilename, 'r');
if ($this->fileHandle === false) {
throw new Exception("Could not open file " . $pFilename . " for reading.");
2014-03-10 22:41:41 +07:00
}
}
/**
* Can the current ReaderInterface read the file?
2014-03-10 22:41:41 +07:00
*
* @param string $pFilename
* @return bool
2014-03-10 22:41:41 +07:00
*/
public function canRead($pFilename)
{
// Check if file exists
try {
$this->openFile($pFilename);
2014-03-10 22:41:41 +07:00
} catch (Exception $e) {
return false;
2014-03-10 22:41:41 +07:00
}
fclose($this->fileHandle);
return true;
2014-03-10 22:41:41 +07:00
}
}