From 32ed6a3b19d0b6b94fc8f5e12585aa6557dd166a Mon Sep 17 00:00:00 2001 From: Roman Syroeshko Date: Wed, 19 Mar 2014 18:00:19 +0400 Subject: [PATCH] https://github.com/PHPOffice/PHPWord/issues/58 - Part V (IOFactory). --- Classes/PHPWord/IOFactory.php | 137 +++++++------------------------- Tests/PHPWord/IOFactoryTest.php | 68 ++++++++-------- Tests/_inc/TestHelperDOCX.php | 4 +- 3 files changed, 63 insertions(+), 146 deletions(-) diff --git a/Classes/PHPWord/IOFactory.php b/Classes/PHPWord/IOFactory.php index 9ffda349..aac25e28 100755 --- a/Classes/PHPWord/IOFactory.php +++ b/Classes/PHPWord/IOFactory.php @@ -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); } } \ No newline at end of file diff --git a/Tests/PHPWord/IOFactoryTest.php b/Tests/PHPWord/IOFactoryTest.php index 951497c6..d4338fba 100644 --- a/Tests/PHPWord/IOFactoryTest.php +++ b/Tests/PHPWord/IOFactoryTest.php @@ -1,59 +1,55 @@ 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'); } } \ No newline at end of file diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 086a1227..28975050 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -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;