Skip to content

Commit 9c8620e

Browse files
committed
Refactor IOFactory to remove duplication
1 parent 7030221 commit 9c8620e

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/PhpWord/IOFactory.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,42 @@ abstract class IOFactory
2727
/**
2828
* Create new writer
2929
*
30-
* @param PhpWord $phpWord
30+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
3131
* @param string $name
3232
* @return \PhpOffice\PhpWord\Writer\WriterInterface
33-
* @throws \PhpOffice\PhpWord\Exception\Exception
3433
*/
3534
public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
3635
{
37-
$class = 'PhpOffice\\PhpWord\\Writer\\' . $name;
38-
if (class_exists($class) && self::isConcreteClass($class)) {
39-
return new $class($phpWord);
40-
} else {
41-
throw new Exception("\"{$name}\" is not a valid writer.");
42-
}
36+
return self::createObject('Writer', $name, $phpWord);
4337
}
4438

4539
/**
4640
* Create new reader
4741
*
4842
* @param string $name
4943
* @return \PhpOffice\PhpWord\Reader\ReaderInterface
50-
* @throws \PhpOffice\PhpWord\Exception\Exception
5144
*/
5245
public static function createReader($name = 'Word2007')
5346
{
54-
$class = 'PhpOffice\\PhpWord\\Reader\\' . $name;
47+
return self::createObject('Reader', $name);
48+
}
49+
50+
/**
51+
* Create new object
52+
*
53+
* @param string $type
54+
* @param string $name
55+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
56+
* @return \PhpOffice\PhpWord\Writer\WriterInterface|\PhpOffice\PhpWord\Reader\ReaderInterface
57+
* @throws \PhpOffice\PhpWord\Exception\Exception
58+
*/
59+
private static function createObject($type, $name, $phpWord = null)
60+
{
61+
$class = "PhpOffice\\PhpWord\\{$type}\\{$name}";
5562
if (class_exists($class) && self::isConcreteClass($class)) {
56-
return new $class();
63+
return new $class($phpWord);
5764
} else {
58-
throw new Exception("\"{$name}\" is not a valid reader.");
65+
throw new Exception("\"{$name}\" is not a valid {$type}.");
5966
}
6067
}
6168

@@ -64,7 +71,7 @@ public static function createReader($name = 'Word2007')
6471
*
6572
* @param string $filename The name of the file
6673
* @param string $readerName
67-
* @return PhpWord
74+
* @return \PhpOffice\PhpWord\PhpWord $phpWord
6875
*/
6976
public static function load($filename, $readerName = 'Word2007')
7077
{

0 commit comments

Comments
 (0)