PHPWord/src/PhpWord/Reader/AbstractReader.php

113 lines
2.5 KiB
PHP
Raw Normal View History

2014-03-10 22:41:41 +07:00
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
2014-03-10 22:41:41 +07:00
*
* @see https://github.com/PHPOffice/PHPWord
2022-09-16 11:45:45 +02:00
*
2014-05-04 21:03:28 +04:00
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
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
/**
2022-09-16 11:45:45 +02:00
* Reader abstract class.
*
* @since 0.8.0
2015-11-15 21:34:36 +04:00
*
* @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
/**
2022-09-16 11:45:45 +02:00
* File pointer.
2014-03-17 07:26:25 +07:00
*
* @var bool|resource
*/
2014-05-27 23:41:15 +07:00
protected $fileHandle;
2014-03-10 22:41:41 +07:00
/**
* Read data only?
*
* @return bool
2014-03-10 22:41:41 +07:00
*/
2014-05-04 15:13:31 +07:00
public function isReadDataOnly()
{
// return $this->readDataOnly;
return true;
2014-03-10 22:41:41 +07:00
}
/**
2022-09-16 11:45:45 +02:00
* Set read data only.
2014-03-10 22:41:41 +07:00
*
2014-05-15 14:41:08 +07:00
* @param bool $value
2022-09-16 11:45:45 +02:00
*
* @return self
2014-03-10 22:41:41 +07:00
*/
2014-05-15 14:41:08 +07:00
public function setReadDataOnly($value = true)
{
2014-05-15 14:41:08 +07:00
$this->readDataOnly = $value;
2014-03-10 22:41:41 +07:00
return $this;
}
/**
2022-09-16 11:45:45 +02:00
* Open file for reading.
2014-03-10 22:41:41 +07:00
*
2014-05-15 14:41:08 +07:00
* @param string $filename
*
* @return resource
2014-03-10 22:41:41 +07:00
*/
2014-05-15 14:41:08 +07:00
protected function openFile($filename)
2014-03-10 22:41:41 +07:00
{
// Check if file exists
2014-05-15 14:41:08 +07:00
if (!file_exists($filename) || !is_readable($filename)) {
throw new Exception("Could not open $filename for reading! File does not exist.");
2014-03-10 22:41:41 +07:00
}
// Open file
2022-09-16 11:45:45 +02:00
$this->fileHandle = fopen($filename, 'rb');
if ($this->fileHandle === false) {
throw new Exception("Could not open file $filename 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
*
2014-05-15 14:41:08 +07:00
* @param string $filename
2022-09-16 11:45:45 +02:00
*
* @return bool
2014-03-10 22:41:41 +07:00
*/
2014-05-15 14:41:08 +07:00
public function canRead($filename)
2014-03-10 22:41:41 +07:00
{
// Check if file exists
try {
2014-05-15 14:41:08 +07:00
$this->openFile($filename);
2014-03-10 22:41:41 +07:00
} catch (Exception $e) {
return false;
2014-03-10 22:41:41 +07:00
}
2014-04-08 03:27:19 +07:00
if (is_resource($this->fileHandle)) {
fclose($this->fileHandle);
}
return true;
2014-03-10 22:41:41 +07:00
}
}