2014-03-22 10:06:08 +04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-03-26 16:33:20 +07:00
|
|
|
* PHPWord
|
2014-03-22 10:06:08 +04: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-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord;
|
|
|
|
|
|
2014-03-31 01:13:02 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-04-08 16:09:31 +07:00
|
|
|
use PhpOffice\PhpWord\Reader\ReaderInterface;
|
2014-08-23 20:19:48 +04:00
|
|
|
use PhpOffice\PhpWord\Writer\WriterInterface;
|
2014-03-22 10:06:08 +04:00
|
|
|
|
|
|
|
|
abstract class IOFactory
|
|
|
|
|
{
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Create new writer
|
|
|
|
|
*
|
2014-04-08 16:09:31 +07:00
|
|
|
* @param PhpWord $phpWord
|
2014-03-22 10:06:08 +04:00
|
|
|
* @param string $name
|
2014-04-08 16:09:31 +07:00
|
|
|
* @return WriterInterface
|
2014-08-23 20:19:48 +04:00
|
|
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
2014-04-03 11:34:06 +07:00
|
|
|
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-08-23 20:19:48 +04:00
|
|
|
if ($name !== 'WriterInterface' && !in_array($name, array('ODText', 'RTF', 'Word2007', 'HTML', 'PDF'), true)) {
|
2014-03-23 12:14:07 -04:00
|
|
|
throw new Exception("\"{$name}\" is not a valid writer.");
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
2014-03-23 12:14:07 -04:00
|
|
|
|
|
|
|
|
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
|
2014-08-23 20:19:48 +04:00
|
|
|
|
2014-03-23 12:14:07 -04:00
|
|
|
return new $fqName($phpWord);
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-03-24 00:26:10 +07:00
|
|
|
* Create new reader
|
|
|
|
|
*
|
2014-03-22 10:06:08 +04:00
|
|
|
* @param string $name
|
2014-04-08 16:09:31 +07:00
|
|
|
* @return ReaderInterface
|
2014-04-03 11:34:06 +07:00
|
|
|
* @throws Exception
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
2014-04-03 11:34:06 +07:00
|
|
|
public static function createReader($name = 'Word2007')
|
2014-03-22 10:06:08 +04:00
|
|
|
{
|
2014-06-02 23:47:44 +07:00
|
|
|
return self::createObject('Reader', $name);
|
|
|
|
|
}
|
2014-03-23 12:14:07 -04:00
|
|
|
|
2014-08-16 12:33:37 +02:00
|
|
|
/**
|
|
|
|
|
* Create new object
|
|
|
|
|
*
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param \PhpOffice\PhpWord\PhpWord $phpWord
|
|
|
|
|
* @return \PhpOffice\PhpWord\Writer\WriterInterface|\PhpOffice\PhpWord\Reader\ReaderInterface
|
|
|
|
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
|
|
|
|
*/
|
|
|
|
|
private static function createObject($type, $name, $phpWord = null)
|
|
|
|
|
{
|
|
|
|
|
$class = "PhpOffice\\PhpWord\\{$type}\\{$name}";
|
|
|
|
|
if (class_exists($class) && self::isConcreteClass($class)) {
|
|
|
|
|
return new $class($phpWord);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Exception("\"{$name}\" is not a valid {$type}.");
|
|
|
|
|
}
|
2014-03-22 10:06:08 +04:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Loads PhpWord from file
|
|
|
|
|
*
|
|
|
|
|
* @param string $filename The name of the file
|
|
|
|
|
* @param string $readerName
|
2014-08-16 12:33:37 +02:00
|
|
|
* @return \PhpOffice\PhpWord\PhpWord $phpWord
|
2014-03-22 10:06:08 +04:00
|
|
|
*/
|
|
|
|
|
public static function load($filename, $readerName = 'Word2007')
|
|
|
|
|
{
|
2014-08-16 12:33:37 +02:00
|
|
|
/** @var \PhpOffice\PhpWord\Reader\ReaderInterface $reader */
|
2014-03-22 10:06:08 +04:00
|
|
|
$reader = self::createReader($readerName);
|
|
|
|
|
return $reader->load($filename);
|
|
|
|
|
}
|
2014-08-16 12:33:37 +02:00
|
|
|
/**
|
|
|
|
|
* Check if it's a concrete class (not abstract nor interface)
|
|
|
|
|
*
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private static function isConcreteClass($class)
|
|
|
|
|
{
|
|
|
|
|
$reflection = new \ReflectionClass($class);
|
|
|
|
|
return !$reflection->isAbstract() && !$reflection->isInterface();
|
|
|
|
|
}
|
2014-03-23 17:37:26 +07:00
|
|
|
}
|