This commit is contained in:
Roman Syroeshko 2014-03-19 18:00:19 +04:00
parent 4e546d1a21
commit 32ed6a3b19
3 changed files with 63 additions and 146 deletions

View File

@ -25,134 +25,55 @@
* @version 0.8.0
*/
namespace PhpOffice\PhpWord;
use PhpOffice\PhpWord\Exceptions\Exception;
/**
* Class PHPWord_IOFactory
*/
class PHPWord_IOFactory
abstract class IOFactory
{
/**
* Search locations
*
* @var array
* @param PHPWord $phpWord
* @param string $name
* @return PhpOffice\PhpWord\Writer\IWriter
* @throws PhpOffice\PhpWord\Exceptions\Exception
*/
private static $_searchLocations = array(
array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}'),
array('type' => 'IReader', 'path' => 'PHPWord/Reader/{0}.php', 'class' => 'PHPWord_Reader_{0}' ),
);
/**
* Autoresolve classes
*
* @var array
*/
private static $_autoResolveClasses = array(
'Serialized'
);
/**
* Private constructor for PHPWord_IOFactory
*/
private function __construct()
public static function createWriter(PHPWord $phpWord, $name)
{
}
try {
$fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";
/**
* Get search locations
*
* @return array
*/
public static function getSearchLocations()
{
return self::$_searchLocations;
}
/**
* Set search locations
*
* @param array $value
* @throws Exception
*/
public static function setSearchLocations(array $value)
{
self::$_searchLocations = $value;
}
/**
* Add search location
*
* @param string $type Example: IWriter
* @param string $location Example: PHPWord/Writer/{0}.php
* @param string $classname Example: PHPWord_Writer_{0}
*/
public static function addSearchLocation($type = '', $location = '', $classname = '')
{
self::$_searchLocations[] = array('type' => $type, 'path' => $location, 'class' => $classname);
}
/**
* Create PHPWord_Writer_IWriter
*
* @param PHPWord $PHPWord
* @param string $writerType Example: Word2007
* @return PHPWord_Writer_IWriter
* @throws Exception
*/
public static function createWriter(PHPWord $PHPWord, $writerType = '')
{
$searchType = 'IWriter';
foreach (self::$_searchLocations as $searchLocation) {
if ($searchLocation['type'] == $searchType) {
$className = str_replace('{0}', $writerType, $searchLocation['class']);
$classFile = str_replace('{0}', $writerType, $searchLocation['path']);
$instance = new $className($PHPWord);
if (!is_null($instance)) {
return $instance;
}
}
return new $fqName($phpWord);
} catch (\Exception $ex) {
throw new Exception("Could not instantiate \"{$name}\" class.");
}
throw new Exception("No $searchType found for type $writerType");
}
/**
* Create PHPWord_Reader_IReader
*
* @param string $readerType Example: Word2007
* @return PHPWord_Reader_IReader
* @throws Exception
* @param string $name
* @return PhpOffice\PhpWord\Reader\IReader
* @throws PhpOffice\PhpWord\Exceptions\Exception
*/
public static function createReader($readerType = '')
public static function createReader($name)
{
$searchType = 'IReader';
try {
$fqName = "PhpOffice\\PhpWord\\Reader\\{$name}";
foreach (self::$_searchLocations as $searchLocation) {
if ($searchLocation['type'] == $searchType) {
$className = str_replace('{0}', $readerType, $searchLocation['class']);
$instance = new $className();
if ($instance !== null) {
return $instance;
}
}
return new $fqName();
} catch (\Exception $ex) {
throw new Exception("Could not instantiate \"{$name}\" class.");
}
throw new Exception("No $searchType found for type $readerType");
}
/**
* Loads PHPWord from file
* Loads PhpWord from file
*
* @param string $pFilename The name of the file
* @param string $readerType
* @return PHPWord
* @param string $filename The name of the file
* @param string $readerName
* @return PhpOffice\PHPWord
*/
public static function load($pFilename, $readerType = 'Word2007')
public static function load($filename, $readerName = 'Word2007')
{
$reader = self::createReader($readerType);
return $reader->load($pFilename);
$reader = self::createReader($readerName);
return $reader->load($filename);
}
}

View File

@ -1,59 +1,55 @@
<?php
namespace PHPWord\Tests;
use PHPWord;
use PhpOffice\PHPWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Writer\Word2007;
use Exception;
/**
* @@coversDefaultClass PhpOffice\PhpWord\IOFactory
* @package PHPWord\Tests
* @runTestsInSeparateProcesses
*/
class IOFactoryTest extends \PHPUnit_Framework_TestCase
final class IOFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testGetSearchLocations()
/**
* @covers ::createWriter
*/
final public function testExistingWriterCanBeCreated()
{
$this->assertAttributeEquals(
IOFactory::getSearchLocations(),
'_searchLocations',
'PhpOffice\\PhpWord\\IOFactory'
);
}
public function testSetSearchLocationsWithArray()
{
IOFactory::setSearchLocations(array());
$this->assertAttributeEquals(array(), '_searchLocations', 'PhpOffice\\PhpWord\\IOFactory');
}
public function testAddSearchLocation()
{
IOFactory::setSearchLocations(array());
IOFactory::addSearchLocation('interface', 'classname');
$this->assertAttributeEquals(
array(array('interface' => 'interface', 'class' => 'classname')),
'_searchLocations',
'PhpOffice\\PhpWord\\IOFactory'
$this->assertInstanceOf(
'PhpOffice\\PhpWord\\Writer\\Word2007',
IOFactory::createWriter(new PHPWord(), 'Word2007')
);
}
/**
* @expectedException Exception
* @expectedExceptionMessage No IWriter found for type
* @covers ::createWriter
* @expectedException PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage Could not instantiate "Word2006" class.
*/
public function testCreateWriterException()
final public function testNonexistentWriterCanNotBeCreated()
{
$oPHPWord = new PHPWord();
IOFactory::setSearchLocations(array());
IOFactory::createWriter($oPHPWord);
IOFactory::createWriter(new PHPWord(), 'Word2006');
}
public function testCreateWriter()
/**
* @covers ::createReader
*/
final public function testExistingReaderCanBeCreated()
{
$oPHPWord = new PHPWord();
$this->assertInstanceOf(
'PhpOffice\\PhpWord\\Reader\\Word2007',
IOFactory::createReader('Word2007')
);
}
$this->assertEquals(IOFactory::createWriter($oPHPWord, 'Word2007'), new Word2007($oPHPWord));
/**
* @covers ::createReader
* @expectedException PhpOffice\PhpWord\Exceptions\Exception
* @expectedExceptionMessage Could not instantiate "Word2006" class.
*/
final public function testNonexistentReaderCanNotBeCreated()
{
IOFactory::createReader('Word2006');
}
}

View File

@ -13,14 +13,14 @@ class TestHelperDOCX
* @param \PHPWord $PHPWord
* @return \PHPWord\Tests\XmlDocument
*/
public static function getDocument(PHPWord $PHPWord, $writer = 'Word2007')
public static function getDocument(PHPWord $PHPWord, $writerName = 'Word2007')
{
self::$file = tempnam(sys_get_temp_dir(), 'PHPWord');
if (!is_dir(sys_get_temp_dir() . '/PHPWord_Unit_Test/')) {
mkdir(sys_get_temp_dir() . '/PHPWord_Unit_Test/');
}
$xmlWriter = IOFactory::createWriter($PHPWord, $writer);
$xmlWriter = IOFactory::createWriter($PHPWord, $writerName);
$xmlWriter->save(self::$file);
$zip = new \ZipArchive;