Skip to content

Commit adf20d3

Browse files
author
Roman Syroeshko
committed
1 parent 90295fe commit adf20d3

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
class TemplateProcessor
2727
{
28+
const MAXIMUM_REPLACEMENTS_DEFAULT = -1;
29+
2830
/**
2931
* ZipArchive object.
3032
*
@@ -62,6 +64,7 @@ class TemplateProcessor
6264
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception.
6365
*
6466
* @param string $documentTemplate The fully qualified template filename.
67+
*
6568
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
6669
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
6770
*/
@@ -104,7 +107,9 @@ public function __construct($documentTemplate)
104107
* @param \DOMDocument $xslDOMDocument
105108
* @param array $xslOptions
106109
* @param string $xslOptionsURI
110+
*
107111
* @return void
112+
*
108113
* @throws \PhpOffice\PhpWord\Exception\Exception
109114
*/
110115
public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '')
@@ -131,21 +136,22 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO
131136
}
132137

133138
/**
134-
* @param mixed $search
139+
* @param mixed $macro
135140
* @param mixed $replace
136141
* @param integer $limit
142+
*
137143
* @return void
138144
*/
139-
public function setValue($search, $replace, $limit = -1)
145+
public function setValue($macro, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
140146
{
141147
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);
143149
}
144150

145-
$this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $search, $replace, $limit);
151+
$this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $macro, $replace, $limit);
146152

147153
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);
149155
}
150156
}
151157

@@ -174,7 +180,9 @@ public function getVariables()
174180
*
175181
* @param string $search
176182
* @param integer $numberOfClones
183+
*
177184
* @return void
185+
*
178186
* @throws \PhpOffice\PhpWord\Exception\Exception
179187
*/
180188
public function cloneRow($search, $numberOfClones)
@@ -232,6 +240,7 @@ public function cloneRow($search, $numberOfClones)
232240
* @param string $blockname
233241
* @param integer $clones
234242
* @param boolean $replace
243+
*
235244
* @return string|null
236245
*/
237246
public function cloneBlock($blockname, $clones = 1, $replace = true)
@@ -267,6 +276,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
267276
*
268277
* @param string $blockname
269278
* @param string $replacement
279+
*
270280
* @return void
271281
*/
272282
public function replaceBlock($blockname, $replacement)
@@ -290,6 +300,7 @@ public function replaceBlock($blockname, $replacement)
290300
* Delete a block of text.
291301
*
292302
* @param string $blockname
303+
*
293304
* @return void
294305
*/
295306
public function deleteBlock($blockname)
@@ -301,6 +312,7 @@ public function deleteBlock($blockname)
301312
* Saves the result document.
302313
*
303314
* @return string
315+
*
304316
* @throws \PhpOffice\PhpWord\Exception\Exception
305317
*/
306318
public function save()
@@ -329,6 +341,7 @@ public function save()
329341
* @since 0.8.0
330342
*
331343
* @param string $fileName
344+
*
332345
* @return void
333346
*/
334347
public function saveAs($fileName)
@@ -342,7 +355,7 @@ public function saveAs($fileName)
342355
/*
343356
* Note: we do not use ``rename`` function here, because it looses file ownership data on Windows platform.
344357
* As a result, user cannot open the file directly getting "Access denied" message.
345-
*
358+
*
346359
* @see https://github.com/PHPOffice/PHPWord/issues/532
347360
*/
348361
copy($tempFileName, $fileName);
@@ -375,12 +388,13 @@ function ($match) {
375388
}
376389

377390
/**
378-
* Find and replace placeholders in the given XML section.
391+
* Find and replace macros in the given XML section.
379392
*
380393
* @param string $documentPartXML
381394
* @param string $search
382395
* @param string $replace
383396
* @param integer $limit
397+
*
384398
* @return string
385399
*/
386400
protected function setValueForPart($documentPartXML, $search, $replace, $limit)
@@ -393,15 +407,21 @@ protected function setValueForPart($documentPartXML, $search, $replace, $limit)
393407
$replace = utf8_encode($replace);
394408
}
395409

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+
}
399418
}
400419

401420
/**
402421
* Find all variables in $documentPartXML.
403422
*
404423
* @param string $documentPartXML
424+
*
405425
* @return string[]
406426
*/
407427
protected function getVariablesForPart($documentPartXML)
@@ -415,6 +435,7 @@ protected function getVariablesForPart($documentPartXML)
415435
* Get the name of the footer file for $index.
416436
*
417437
* @param integer $index
438+
*
418439
* @return string
419440
*/
420441
protected function getFooterName($index)
@@ -426,6 +447,7 @@ protected function getFooterName($index)
426447
* Get the name of the header file for $index.
427448
*
428449
* @param integer $index
450+
*
429451
* @return string
430452
*/
431453
protected function getHeaderName($index)
@@ -437,7 +459,9 @@ protected function getHeaderName($index)
437459
* Find the start position of the nearest table row before $offset.
438460
*
439461
* @param integer $offset
462+
*
440463
* @return integer
464+
*
441465
* @throws \PhpOffice\PhpWord\Exception\Exception
442466
*/
443467
protected function findRowStart($offset)
@@ -458,6 +482,7 @@ protected function findRowStart($offset)
458482
* Find the end position of the nearest table row after $offset.
459483
*
460484
* @param integer $offset
485+
*
461486
* @return integer
462487
*/
463488
protected function findRowEnd($offset)
@@ -470,6 +495,7 @@ protected function findRowEnd($offset)
470495
*
471496
* @param integer $startPosition
472497
* @param integer $endPosition
498+
*
473499
* @return string
474500
*/
475501
protected function getSlice($startPosition, $endPosition = 0)

tests/PhpWord/Tests/TemplateProcessorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public function testCloneRow()
171171
* @covers ::saveAs
172172
* @test
173173
*/
174-
public function testVariablesCanBeReplacedInHeaderAndFooter()
174+
public function testMacrosCanBeReplacedInHeaderAndFooter()
175175
{
176176
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
177177

0 commit comments

Comments
 (0)