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-26 16:33:20 +07:00
|
|
|
* Copyright (c) 2014 PHPWord
|
2014-03-10 22:41:41 +07:00
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*
|
2014-03-26 16:33:20 +07:00
|
|
|
* @copyright Copyright (c) 2014 PHPWord
|
2014-03-10 22:41:41 +07:00
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
2014-03-26 15:47:27 +07:00
|
|
|
* @version 0.9.0
|
2014-03-10 22:41:41 +07:00
|
|
|
*/
|
|
|
|
|
|
2014-03-22 10:06:08 +04:00
|
|
|
namespace PhpOffice\PhpWord\Reader;
|
|
|
|
|
|
2014-03-15 09:27:48 -04:00
|
|
|
use PhpOffice\PhpWord\Exceptions\Exception;
|
|
|
|
|
|
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-03-22 10:06:08 +04:00
|
|
|
abstract class AbstractReader implements IReader
|
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-03-23 10:32:08 +04:00
|
|
|
* @return \PhpOffice\PhpWord\Reader\IReader
|
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-23 10:32:08 +04:00
|
|
|
* @throws \PhpOffice\PhpWord\Exceptions\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-03-24 00:26:10 +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-03-22 10:06:08 +04:00
|
|
|
* Can the current IReader 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
|
|
|
}
|