@@ -27,35 +27,42 @@ abstract class IOFactory
27
27
/**
28
28
* Create new writer
29
29
*
30
- * @param PhpWord $phpWord
30
+ * @param \PhpOffice\PhpWord\ PhpWord $phpWord
31
31
* @param string $name
32
32
* @return \PhpOffice\PhpWord\Writer\WriterInterface
33
- * @throws \PhpOffice\PhpWord\Exception\Exception
34
33
*/
35
34
public static function createWriter (PhpWord $ phpWord , $ name = 'Word2007 ' )
36
35
{
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 );
43
37
}
44
38
45
39
/**
46
40
* Create new reader
47
41
*
48
42
* @param string $name
49
43
* @return \PhpOffice\PhpWord\Reader\ReaderInterface
50
- * @throws \PhpOffice\PhpWord\Exception\Exception
51
44
*/
52
45
public static function createReader ($ name = 'Word2007 ' )
53
46
{
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 }" ;
55
62
if (class_exists ($ class ) && self ::isConcreteClass ($ class )) {
56
- return new $ class ();
63
+ return new $ class ($ phpWord );
57
64
} else {
58
- throw new Exception ("\"{$ name }\" is not a valid reader . " );
65
+ throw new Exception ("\"{$ name }\" is not a valid { $ type } . " );
59
66
}
60
67
}
61
68
@@ -64,7 +71,7 @@ public static function createReader($name = 'Word2007')
64
71
*
65
72
* @param string $filename The name of the file
66
73
* @param string $readerName
67
- * @return PhpWord
74
+ * @return \PhpOffice\ PhpWord\PhpWord $phpWord
68
75
*/
69
76
public static function load ($ filename , $ readerName = 'Word2007 ' )
70
77
{
0 commit comments