25
25
26
26
class TemplateProcessor
27
27
{
28
+ const MAXIMUM_REPLACEMENTS_DEFAULT = -1 ;
29
+
28
30
/**
29
31
* ZipArchive object.
30
32
*
@@ -62,6 +64,7 @@ class TemplateProcessor
62
64
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception.
63
65
*
64
66
* @param string $documentTemplate The fully qualified template filename.
67
+ *
65
68
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
66
69
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
67
70
*/
@@ -104,7 +107,9 @@ public function __construct($documentTemplate)
104
107
* @param \DOMDocument $xslDOMDocument
105
108
* @param array $xslOptions
106
109
* @param string $xslOptionsURI
110
+ *
107
111
* @return void
112
+ *
108
113
* @throws \PhpOffice\PhpWord\Exception\Exception
109
114
*/
110
115
public function applyXslStyleSheet ($ xslDOMDocument , $ xslOptions = array (), $ xslOptionsURI = '' )
@@ -131,21 +136,22 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO
131
136
}
132
137
133
138
/**
134
- * @param mixed $search
139
+ * @param mixed $macro
135
140
* @param mixed $replace
136
141
* @param integer $limit
142
+ *
137
143
* @return void
138
144
*/
139
- public function setValue ($ search , $ replace , $ limit = - 1 )
145
+ public function setValue ($ macro , $ replace , $ limit = self :: MAXIMUM_REPLACEMENTS_DEFAULT )
140
146
{
141
147
foreach ($ this ->tempDocumentHeaders as $ index => $ headerXML ) {
142
- $ this ->tempDocumentHeaders [$ index ] = $ this ->setValueForPart ($ this ->tempDocumentHeaders [$ index ], $ search , $ replace , $ limit );
148
+ $ this ->tempDocumentHeaders [$ index ] = $ this ->setValueForPart ($ this ->tempDocumentHeaders [$ index ], $ macro , $ replace , $ limit );
143
149
}
144
150
145
- $ this ->tempDocumentMainPart = $ this ->setValueForPart ($ this ->tempDocumentMainPart , $ search , $ replace , $ limit );
151
+ $ this ->tempDocumentMainPart = $ this ->setValueForPart ($ this ->tempDocumentMainPart , $ macro , $ replace , $ limit );
146
152
147
153
foreach ($ this ->tempDocumentFooters as $ index => $ headerXML ) {
148
- $ this ->tempDocumentFooters [$ index ] = $ this ->setValueForPart ($ this ->tempDocumentFooters [$ index ], $ search , $ replace , $ limit );
154
+ $ this ->tempDocumentFooters [$ index ] = $ this ->setValueForPart ($ this ->tempDocumentFooters [$ index ], $ macro , $ replace , $ limit );
149
155
}
150
156
}
151
157
@@ -174,7 +180,9 @@ public function getVariables()
174
180
*
175
181
* @param string $search
176
182
* @param integer $numberOfClones
183
+ *
177
184
* @return void
185
+ *
178
186
* @throws \PhpOffice\PhpWord\Exception\Exception
179
187
*/
180
188
public function cloneRow ($ search , $ numberOfClones )
@@ -232,6 +240,7 @@ public function cloneRow($search, $numberOfClones)
232
240
* @param string $blockname
233
241
* @param integer $clones
234
242
* @param boolean $replace
243
+ *
235
244
* @return string|null
236
245
*/
237
246
public function cloneBlock ($ blockname , $ clones = 1 , $ replace = true )
@@ -267,6 +276,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
267
276
*
268
277
* @param string $blockname
269
278
* @param string $replacement
279
+ *
270
280
* @return void
271
281
*/
272
282
public function replaceBlock ($ blockname , $ replacement )
@@ -290,6 +300,7 @@ public function replaceBlock($blockname, $replacement)
290
300
* Delete a block of text.
291
301
*
292
302
* @param string $blockname
303
+ *
293
304
* @return void
294
305
*/
295
306
public function deleteBlock ($ blockname )
@@ -301,6 +312,7 @@ public function deleteBlock($blockname)
301
312
* Saves the result document.
302
313
*
303
314
* @return string
315
+ *
304
316
* @throws \PhpOffice\PhpWord\Exception\Exception
305
317
*/
306
318
public function save ()
@@ -329,6 +341,7 @@ public function save()
329
341
* @since 0.8.0
330
342
*
331
343
* @param string $fileName
344
+ *
332
345
* @return void
333
346
*/
334
347
public function saveAs ($ fileName )
@@ -342,7 +355,7 @@ public function saveAs($fileName)
342
355
/*
343
356
* Note: we do not use ``rename`` function here, because it looses file ownership data on Windows platform.
344
357
* As a result, user cannot open the file directly getting "Access denied" message.
345
- *
358
+ *
346
359
* @see https://github.com/PHPOffice/PHPWord/issues/532
347
360
*/
348
361
copy ($ tempFileName , $ fileName );
@@ -375,12 +388,13 @@ function ($match) {
375
388
}
376
389
377
390
/**
378
- * Find and replace placeholders in the given XML section.
391
+ * Find and replace macros in the given XML section.
379
392
*
380
393
* @param string $documentPartXML
381
394
* @param string $search
382
395
* @param string $replace
383
396
* @param integer $limit
397
+ *
384
398
* @return string
385
399
*/
386
400
protected function setValueForPart ($ documentPartXML , $ search , $ replace , $ limit )
@@ -393,15 +407,21 @@ protected function setValueForPart($documentPartXML, $search, $replace, $limit)
393
407
$ replace = utf8_encode ($ replace );
394
408
}
395
409
396
- $ regExpDelim = '/ ' ;
397
- $ escapedSearch = preg_quote ($ search , $ regExpDelim );
398
- return preg_replace ("{$ regExpDelim }{$ escapedSearch }{$ regExpDelim }u " , $ replace , $ documentPartXML , $ limit );
410
+ // Note: we can't use the same function for both cases here, because of performance considerations.
411
+ if (self ::MAXIMUM_REPLACEMENTS_DEFAULT === $ limit ) {
412
+ return str_replace ($ search , $ replace , $ documentPartXML );
413
+ } else {
414
+ $ regExpDelim = '/ ' ;
415
+ $ escapedSearch = preg_quote ($ search , $ regExpDelim );
416
+ return preg_replace ("{$ regExpDelim }{$ escapedSearch }{$ regExpDelim }u " , $ replace , $ documentPartXML , $ limit );
417
+ }
399
418
}
400
419
401
420
/**
402
421
* Find all variables in $documentPartXML.
403
422
*
404
423
* @param string $documentPartXML
424
+ *
405
425
* @return string[]
406
426
*/
407
427
protected function getVariablesForPart ($ documentPartXML )
@@ -415,6 +435,7 @@ protected function getVariablesForPart($documentPartXML)
415
435
* Get the name of the footer file for $index.
416
436
*
417
437
* @param integer $index
438
+ *
418
439
* @return string
419
440
*/
420
441
protected function getFooterName ($ index )
@@ -426,6 +447,7 @@ protected function getFooterName($index)
426
447
* Get the name of the header file for $index.
427
448
*
428
449
* @param integer $index
450
+ *
429
451
* @return string
430
452
*/
431
453
protected function getHeaderName ($ index )
@@ -437,7 +459,9 @@ protected function getHeaderName($index)
437
459
* Find the start position of the nearest table row before $offset.
438
460
*
439
461
* @param integer $offset
462
+ *
440
463
* @return integer
464
+ *
441
465
* @throws \PhpOffice\PhpWord\Exception\Exception
442
466
*/
443
467
protected function findRowStart ($ offset )
@@ -458,6 +482,7 @@ protected function findRowStart($offset)
458
482
* Find the end position of the nearest table row after $offset.
459
483
*
460
484
* @param integer $offset
485
+ *
461
486
* @return integer
462
487
*/
463
488
protected function findRowEnd ($ offset )
@@ -470,6 +495,7 @@ protected function findRowEnd($offset)
470
495
*
471
496
* @param integer $startPosition
472
497
* @param integer $endPosition
498
+ *
473
499
* @return string
474
500
*/
475
501
protected function getSlice ($ startPosition , $ endPosition = 0 )
0 commit comments