25
25
* @version 0.8.0
26
26
*/
27
27
28
+ namespace PhpOffice \PhpWord ;
29
+
28
30
use PhpOffice \PhpWord \Exceptions \Exception ;
29
31
30
- /**
31
- * Class PHPWord_IOFactory
32
- */
33
- class PHPWord_IOFactory
32
+ abstract class IOFactory
34
33
{
35
34
/**
36
- * Search locations
37
- *
38
- * @var array
39
- */
40
- private static $ _searchLocations = array (
41
- array ('type ' => 'IWriter ' , 'path ' => 'PHPWord/Writer/{0}.php ' , 'class ' => 'PHPWord_Writer_{0} ' ),
42
- array ('type ' => 'IReader ' , 'path ' => 'PHPWord/Reader/{0}.php ' , 'class ' => 'PHPWord_Reader_{0} ' ),
43
- );
44
-
45
- /**
46
- * Autoresolve classes
47
- *
48
- * @var array
49
- */
50
- private static $ _autoResolveClasses = array (
51
- 'Serialized '
52
- );
53
-
54
- /**
55
- * Private constructor for PHPWord_IOFactory
56
- */
57
- private function __construct ()
58
- {
59
- }
60
-
61
- /**
62
- * Get search locations
63
- *
64
- * @return array
65
- */
66
- public static function getSearchLocations ()
67
- {
68
- return self ::$ _searchLocations ;
69
- }
70
-
71
- /**
72
- * Set search locations
73
- *
74
- * @param array $value
75
- * @throws Exception
76
- */
77
- public static function setSearchLocations (array $ value )
78
- {
79
- self ::$ _searchLocations = $ value ;
80
- }
81
-
82
- /**
83
- * Add search location
84
- *
85
- * @param string $type Example: IWriter
86
- * @param string $location Example: PHPWord/Writer/{0}.php
87
- * @param string $classname Example: PHPWord_Writer_{0}
88
- */
89
- public static function addSearchLocation ($ type = '' , $ location = '' , $ classname = '' )
90
- {
91
- self ::$ _searchLocations [] = array ('type ' => $ type , 'path ' => $ location , 'class ' => $ classname );
92
- }
93
-
94
- /**
95
- * Create PHPWord_Writer_IWriter
96
- *
97
- * @param PHPWord $PHPWord
98
- * @param string $writerType Example: Word2007
99
- * @return PHPWord_Writer_IWriter
100
- * @throws Exception
35
+ * @param PHPWord $phpWord
36
+ * @param string $name
37
+ * @return PhpOffice\PhpWord\Writer\IWriter
38
+ * @throws PhpOffice\PhpWord\Exceptions\Exception
101
39
*/
102
- public static function createWriter (PHPWord $ PHPWord , $ writerType = '' )
40
+ public static function createWriter (PHPWord $ phpWord , $ name )
103
41
{
104
- $ searchType = 'IWriter ' ;
42
+ try {
43
+ $ fqName = "PhpOffice \\PhpWord \\Writer \\{$ name }" ;
105
44
106
- foreach (self ::$ _searchLocations as $ searchLocation ) {
107
- if ($ searchLocation ['type ' ] == $ searchType ) {
108
- $ className = str_replace ('{0} ' , $ writerType , $ searchLocation ['class ' ]);
109
- $ classFile = str_replace ('{0} ' , $ writerType , $ searchLocation ['path ' ]);
110
-
111
- $ instance = new $ className ($ PHPWord );
112
- if (!is_null ($ instance )) {
113
- return $ instance ;
114
- }
115
- }
45
+ return new $ fqName ($ phpWord );
46
+ } catch (\Exception $ ex ) {
47
+ throw new Exception ("Could not instantiate \"{$ name }\" class. " );
116
48
}
117
-
118
- throw new Exception ("No $ searchType found for type $ writerType " );
119
49
}
120
50
121
51
/**
122
- * Create PHPWord_Reader_IReader
123
- *
124
- * @param string $readerType Example: Word2007
125
- * @return PHPWord_Reader_IReader
126
- * @throws Exception
52
+ * @param string $name
53
+ * @return PhpOffice\PhpWord\Reader\IReader
54
+ * @throws PhpOffice\PhpWord\Exceptions\Exception
127
55
*/
128
- public static function createReader ($ readerType = '' )
56
+ public static function createReader ($ name )
129
57
{
130
- $ searchType = 'IReader ' ;
58
+ try {
59
+ $ fqName = "PhpOffice \\PhpWord \\Reader \\{$ name }" ;
131
60
132
- foreach (self ::$ _searchLocations as $ searchLocation ) {
133
- if ($ searchLocation ['type ' ] == $ searchType ) {
134
- $ className = str_replace ('{0} ' , $ readerType , $ searchLocation ['class ' ]);
135
-
136
- $ instance = new $ className ();
137
- if ($ instance !== null ) {
138
- return $ instance ;
139
- }
140
- }
61
+ return new $ fqName ();
62
+ } catch (\Exception $ ex ) {
63
+ throw new Exception ("Could not instantiate \"{$ name }\" class. " );
141
64
}
142
-
143
- throw new Exception ("No $ searchType found for type $ readerType " );
144
65
}
145
66
146
67
/**
147
- * Loads PHPWord from file
68
+ * Loads PhpWord from file
148
69
*
149
- * @param string $pFilename The name of the file
150
- * @param string $readerType
151
- * @return PHPWord
70
+ * @param string $filename The name of the file
71
+ * @param string $readerName
72
+ * @return PhpOffice\ PHPWord
152
73
*/
153
- public static function load ($ pFilename , $ readerType = 'Word2007 ' )
74
+ public static function load ($ filename , $ readerName = 'Word2007 ' )
154
75
{
155
- $ reader = self ::createReader ($ readerType );
156
- return $ reader ->load ($ pFilename );
76
+ $ reader = self ::createReader ($ readerName );
77
+ return $ reader ->load ($ filename );
157
78
}
158
79
}
0 commit comments