@@ -35,28 +35,28 @@ class TemplateProcessor
35
35
/**
36
36
* @var string Temporary document filename (with path).
37
37
*/
38
- protected $ temporaryDocumentFilename ;
38
+ protected $ tempDocumentFilename ;
39
39
40
40
/**
41
41
* Content of main document part (in XML format) of the temporary document.
42
42
*
43
43
* @var string
44
44
*/
45
- protected $ temporaryDocumentMainPart ;
45
+ protected $ tempDocumentMainPart ;
46
46
47
47
/**
48
48
* Content of headers (in XML format) of the temporary document.
49
49
*
50
50
* @var string[]
51
51
*/
52
- protected $ temporaryDocumentHeaders = array ();
52
+ protected $ tempDocumentHeaders = array ();
53
53
54
54
/**
55
55
* Content of footers (in XML format) of the temporary document.
56
56
*
57
57
* @var string[]
58
58
*/
59
- protected $ temporaryDocumentFooters = array ();
59
+ protected $ tempDocumentFooters = array ();
60
60
61
61
/**
62
62
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception.
@@ -68,30 +68,34 @@ class TemplateProcessor
68
68
public function __construct ($ documentTemplate )
69
69
{
70
70
// Temporary document filename initialization
71
- $ this ->temporaryDocumentFilename = tempnam (Settings::getTempDir (), 'PhpWord ' );
72
- if (false === $ this ->temporaryDocumentFilename ) {
71
+ $ this ->tempDocumentFilename = tempnam (Settings::getTempDir (), 'PhpWord ' );
72
+ if (false === $ this ->tempDocumentFilename ) {
73
73
throw new CreateTemporaryFileException ();
74
74
}
75
75
76
76
// Template file cloning
77
- if (false === copy ($ documentTemplate , $ this ->temporaryDocumentFilename )) {
78
- throw new CopyFileException ($ documentTemplate , $ this ->temporaryDocumentFilename );
77
+ if (false === copy ($ documentTemplate , $ this ->tempDocumentFilename )) {
78
+ throw new CopyFileException ($ documentTemplate , $ this ->tempDocumentFilename );
79
79
}
80
80
81
81
// Temporary document content extraction
82
82
$ this ->zipClass = new ZipArchive ();
83
- $ this ->zipClass ->open ($ this ->temporaryDocumentFilename );
83
+ $ this ->zipClass ->open ($ this ->tempDocumentFilename );
84
84
$ index = 1 ;
85
85
while (false !== $ this ->zipClass ->locateName ($ this ->getHeaderName ($ index ))) {
86
- $ this ->temporaryDocumentHeaders [$ index ] = $ this ->zipClass ->getFromName ($ this ->getHeaderName ($ index ));
86
+ $ this ->tempDocumentHeaders [$ index ] = $ this ->fixBrokenMacros (
87
+ $ this ->zipClass ->getFromName ($ this ->getHeaderName ($ index ))
88
+ );
87
89
$ index ++;
88
90
}
89
91
$ index = 1 ;
90
92
while (false !== $ this ->zipClass ->locateName ($ this ->getFooterName ($ index ))) {
91
- $ this ->temporaryDocumentFooters [$ index ] = $ this ->zipClass ->getFromName ($ this ->getFooterName ($ index ));
93
+ $ this ->tempDocumentFooters [$ index ] = $ this ->fixBrokenMacros (
94
+ $ this ->zipClass ->getFromName ($ this ->getFooterName ($ index ))
95
+ );
92
96
$ index ++;
93
97
}
94
- $ this ->temporaryDocumentMainPart = $ this ->fixBrokenMacros ($ this ->zipClass ->getFromName ('word/document.xml ' ));
98
+ $ this ->tempDocumentMainPart = $ this ->fixBrokenMacros ($ this ->zipClass ->getFromName ('word/document.xml ' ));
95
99
}
96
100
97
101
/**
@@ -114,7 +118,7 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO
114
118
}
115
119
116
120
$ xmlDOMDocument = new \DOMDocument ();
117
- if (false === $ xmlDOMDocument ->loadXML ($ this ->temporaryDocumentMainPart )) {
121
+ if (false === $ xmlDOMDocument ->loadXML ($ this ->tempDocumentMainPart )) {
118
122
throw new Exception ('Could not load XML from the given template. ' );
119
123
}
120
124
@@ -123,7 +127,7 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO
123
127
throw new Exception ('Could not transform the given XML document. ' );
124
128
}
125
129
126
- $ this ->temporaryDocumentMainPart = $ xmlTransformed ;
130
+ $ this ->tempDocumentMainPart = $ xmlTransformed ;
127
131
}
128
132
129
133
/**
@@ -134,14 +138,14 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO
134
138
*/
135
139
public function setValue ($ search , $ replace , $ limit = -1 )
136
140
{
137
- foreach ($ this ->temporaryDocumentHeaders as $ index => $ headerXML ) {
138
- $ this ->temporaryDocumentHeaders [$ index ] = $ this ->setValueForPart ($ this ->temporaryDocumentHeaders [$ index ], $ search , $ replace , $ limit );
141
+ foreach ($ this ->tempDocumentHeaders as $ index => $ headerXML ) {
142
+ $ this ->tempDocumentHeaders [$ index ] = $ this ->setValueForPart ($ this ->tempDocumentHeaders [$ index ], $ search , $ replace , $ limit );
139
143
}
140
144
141
- $ this ->temporaryDocumentMainPart = $ this ->setValueForPart ($ this ->temporaryDocumentMainPart , $ search , $ replace , $ limit );
145
+ $ this ->tempDocumentMainPart = $ this ->setValueForPart ($ this ->tempDocumentMainPart , $ search , $ replace , $ limit );
142
146
143
- foreach ($ this ->temporaryDocumentFooters as $ index => $ headerXML ) {
144
- $ this ->temporaryDocumentFooters [$ index ] = $ this ->setValueForPart ($ this ->temporaryDocumentFooters [$ index ], $ search , $ replace , $ limit );
147
+ foreach ($ this ->tempDocumentFooters as $ index => $ headerXML ) {
148
+ $ this ->tempDocumentFooters [$ index ] = $ this ->setValueForPart ($ this ->tempDocumentFooters [$ index ], $ search , $ replace , $ limit );
145
149
}
146
150
}
147
151
@@ -152,13 +156,13 @@ public function setValue($search, $replace, $limit = -1)
152
156
*/
153
157
public function getVariables ()
154
158
{
155
- $ variables = $ this ->getVariablesForPart ($ this ->temporaryDocumentMainPart );
159
+ $ variables = $ this ->getVariablesForPart ($ this ->tempDocumentMainPart );
156
160
157
- foreach ($ this ->temporaryDocumentHeaders as $ headerXML ) {
161
+ foreach ($ this ->tempDocumentHeaders as $ headerXML ) {
158
162
$ variables = array_merge ($ variables , $ this ->getVariablesForPart ($ headerXML ));
159
163
}
160
164
161
- foreach ($ this ->temporaryDocumentFooters as $ footerXML ) {
165
+ foreach ($ this ->tempDocumentFooters as $ footerXML ) {
162
166
$ variables = array_merge ($ variables , $ this ->getVariablesForPart ($ footerXML ));
163
167
}
164
168
@@ -179,7 +183,7 @@ public function cloneRow($search, $numberOfClones)
179
183
$ search = '${ ' . $ search . '} ' ;
180
184
}
181
185
182
- $ tagPos = strpos ($ this ->temporaryDocumentMainPart , $ search );
186
+ $ tagPos = strpos ($ this ->tempDocumentMainPart , $ search );
183
187
if (!$ tagPos ) {
184
188
throw new Exception ("Can not clone row, template variable not found or variable contains markup. " );
185
189
}
@@ -219,7 +223,7 @@ public function cloneRow($search, $numberOfClones)
219
223
}
220
224
$ result .= $ this ->getSlice ($ rowEnd );
221
225
222
- $ this ->temporaryDocumentMainPart = $ result ;
226
+ $ this ->tempDocumentMainPart = $ result ;
223
227
}
224
228
225
229
/**
@@ -235,7 +239,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
235
239
$ xmlBlock = null ;
236
240
preg_match (
237
241
'/(<\?xml.*)(<w:p.*>\${ ' . $ blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/ ' . $ blockname . '}<\/w:.*?p>)/is ' ,
238
- $ this ->temporaryDocumentMainPart ,
242
+ $ this ->tempDocumentMainPart ,
239
243
$ matches
240
244
);
241
245
@@ -247,10 +251,10 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
247
251
}
248
252
249
253
if ($ replace ) {
250
- $ this ->temporaryDocumentMainPart = str_replace (
254
+ $ this ->tempDocumentMainPart = str_replace (
251
255
$ matches [2 ] . $ matches [3 ] . $ matches [4 ],
252
256
implode ('' , $ cloned ),
253
- $ this ->temporaryDocumentMainPart
257
+ $ this ->tempDocumentMainPart
254
258
);
255
259
}
256
260
}
@@ -269,15 +273,15 @@ public function replaceBlock($blockname, $replacement)
269
273
{
270
274
preg_match (
271
275
'/(<\?xml.*)(<w:p.*>\${ ' . $ blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/ ' . $ blockname . '}<\/w:.*?p>)/is ' ,
272
- $ this ->temporaryDocumentMainPart ,
276
+ $ this ->tempDocumentMainPart ,
273
277
$ matches
274
278
);
275
279
276
280
if (isset ($ matches [3 ])) {
277
- $ this ->temporaryDocumentMainPart = str_replace (
281
+ $ this ->tempDocumentMainPart = str_replace (
278
282
$ matches [2 ] . $ matches [3 ] . $ matches [4 ],
279
283
$ replacement ,
280
- $ this ->temporaryDocumentMainPart
284
+ $ this ->tempDocumentMainPart
281
285
);
282
286
}
283
287
}
@@ -301,22 +305,22 @@ public function deleteBlock($blockname)
301
305
*/
302
306
public function save ()
303
307
{
304
- foreach ($ this ->temporaryDocumentHeaders as $ index => $ headerXML ) {
305
- $ this ->zipClass ->addFromString ($ this ->getHeaderName ($ index ), $ this ->temporaryDocumentHeaders [$ index ]);
308
+ foreach ($ this ->tempDocumentHeaders as $ index => $ headerXML ) {
309
+ $ this ->zipClass ->addFromString ($ this ->getHeaderName ($ index ), $ this ->tempDocumentHeaders [$ index ]);
306
310
}
307
311
308
- $ this ->zipClass ->addFromString ('word/document.xml ' , $ this ->temporaryDocumentMainPart );
312
+ $ this ->zipClass ->addFromString ('word/document.xml ' , $ this ->tempDocumentMainPart );
309
313
310
- foreach ($ this ->temporaryDocumentFooters as $ index => $ headerXML ) {
311
- $ this ->zipClass ->addFromString ($ this ->getFooterName ($ index ), $ this ->temporaryDocumentFooters [$ index ]);
314
+ foreach ($ this ->tempDocumentFooters as $ index => $ headerXML ) {
315
+ $ this ->zipClass ->addFromString ($ this ->getFooterName ($ index ), $ this ->tempDocumentFooters [$ index ]);
312
316
}
313
317
314
318
// Close zip file
315
319
if (false === $ this ->zipClass ->close ()) {
316
320
throw new Exception ('Could not close zip file. ' );
317
321
}
318
322
319
- return $ this ->temporaryDocumentFilename ;
323
+ return $ this ->tempDocumentFilename ;
320
324
}
321
325
322
326
/**
0 commit comments