From 66c73c07c5a50197025aa1cd27aa8e1a45604cda Mon Sep 17 00:00:00 2001 From: Gabriel Bull Date: Sun, 23 Mar 2014 12:14:07 -0400 Subject: [PATCH] Fixed exceptions in reader/writer --- src/PhpWord/IOFactory.php | 22 ++++++++++------------ test/PhpWord/Tests/IOFactoryTest.php | 2 -- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/PhpWord/IOFactory.php b/src/PhpWord/IOFactory.php index 854bcab4..261f6afe 100644 --- a/src/PhpWord/IOFactory.php +++ b/src/PhpWord/IOFactory.php @@ -37,13 +37,12 @@ abstract class IOFactory */ public static function createWriter(PhpWord $phpWord, $name) { - try { - $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}"; - - return new $fqName($phpWord); - } catch (\Exception $ex) { - throw new Exception("Could not instantiate \"{$name}\" class."); + if ($name !== 'IWriter' && $name !== 'ODText' && $name !== 'RTF' && $name !== 'Word2007') { + throw new Exception("\"{$name}\" is not a valid writer."); } + + $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}"; + return new $fqName($phpWord); } /** @@ -53,13 +52,12 @@ abstract class IOFactory */ public static function createReader($name) { - try { - $fqName = "PhpOffice\\PhpWord\\Reader\\{$name}"; - - return new $fqName(); - } catch (\Exception $ex) { - throw new Exception("Could not instantiate \"{$name}\" class."); + if ($name !== 'IReader' && $name !== 'Word2007') { + throw new Exception("\"{$name}\" is not a valid reader."); } + + $fqName = "PhpOffice\\PhpWord\\Reader\\{$name}"; + return new $fqName(); } /** diff --git a/test/PhpWord/Tests/IOFactoryTest.php b/test/PhpWord/Tests/IOFactoryTest.php index 810f00b5..dafc19ea 100644 --- a/test/PhpWord/Tests/IOFactoryTest.php +++ b/test/PhpWord/Tests/IOFactoryTest.php @@ -24,7 +24,6 @@ final class IOFactoryTest extends \PHPUnit_Framework_TestCase /** * @covers ::createWriter * @expectedException \PhpOffice\PhpWord\Exceptions\Exception - * @expectedExceptionMessage Could not instantiate "Word2006" class. */ final public function testNonexistentWriterCanNotBeCreated() { @@ -45,7 +44,6 @@ final class IOFactoryTest extends \PHPUnit_Framework_TestCase /** * @covers ::createReader * @expectedException \PhpOffice\PhpWord\Exceptions\Exception - * @expectedExceptionMessage Could not instantiate "Word2006" class. */ final public function testNonexistentReaderCanNotBeCreated() {