Skip to content

Commit

Permalink
Merged PHPOffice#513.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Syroeshko committed Aug 30, 2015
1 parent 48a88f9 commit 3d78ee7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
44 changes: 35 additions & 9 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

class TemplateProcessor
{
const MAXIMUM_REPLACEMENTS_DEFAULT = -1;

/**
* ZipArchive object.
*
Expand Down Expand Up @@ -62,6 +64,7 @@ class TemplateProcessor
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception.
*
* @param string $documentTemplate The fully qualified template filename.
*
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
*/
Expand Down Expand Up @@ -104,7 +107,9 @@ public function __construct($documentTemplate)
* @param \DOMDocument $xslDOMDocument
* @param array $xslOptions
* @param string $xslOptionsURI
*
* @return void
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslOptionsURI = '')
Expand All @@ -131,21 +136,22 @@ public function applyXslStyleSheet($xslDOMDocument, $xslOptions = array(), $xslO
}

/**
* @param mixed $search
* @param mixed $macro
* @param mixed $replace
* @param integer $limit
*
* @return void
*/
public function setValue($search, $replace, $limit = -1)
public function setValue($macro, $replace, $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT)
{
foreach ($this->tempDocumentHeaders as $index => $headerXML) {
$this->tempDocumentHeaders[$index] = $this->setValueForPart($this->tempDocumentHeaders[$index], $search, $replace, $limit);
$this->tempDocumentHeaders[$index] = $this->setValueForPart($this->tempDocumentHeaders[$index], $macro, $replace, $limit);
}

$this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $search, $replace, $limit);
$this->tempDocumentMainPart = $this->setValueForPart($this->tempDocumentMainPart, $macro, $replace, $limit);

foreach ($this->tempDocumentFooters as $index => $headerXML) {
$this->tempDocumentFooters[$index] = $this->setValueForPart($this->tempDocumentFooters[$index], $search, $replace, $limit);
$this->tempDocumentFooters[$index] = $this->setValueForPart($this->tempDocumentFooters[$index], $macro, $replace, $limit);
}
}

Expand Down Expand Up @@ -174,7 +180,9 @@ public function getVariables()
*
* @param string $search
* @param integer $numberOfClones
*
* @return void
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function cloneRow($search, $numberOfClones)
Expand Down Expand Up @@ -232,6 +240,7 @@ public function cloneRow($search, $numberOfClones)
* @param string $blockname
* @param integer $clones
* @param boolean $replace
*
* @return string|null
*/
public function cloneBlock($blockname, $clones = 1, $replace = true)
Expand Down Expand Up @@ -267,6 +276,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
*
* @param string $blockname
* @param string $replacement
*
* @return void
*/
public function replaceBlock($blockname, $replacement)
Expand All @@ -290,6 +300,7 @@ public function replaceBlock($blockname, $replacement)
* Delete a block of text.
*
* @param string $blockname
*
* @return void
*/
public function deleteBlock($blockname)
Expand All @@ -301,6 +312,7 @@ public function deleteBlock($blockname)
* Saves the result document.
*
* @return string
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function save()
Expand Down Expand Up @@ -329,6 +341,7 @@ public function save()
* @since 0.8.0
*
* @param string $fileName
*
* @return void
*/
public function saveAs($fileName)
Expand Down Expand Up @@ -375,12 +388,13 @@ function ($match) {
}

/**
* Find and replace placeholders in the given XML section.
* Find and replace macros in the given XML section.
*
* @param string $documentPartXML
* @param string $search
* @param string $replace
* @param integer $limit
*
* @return string
*/
protected function setValueForPart($documentPartXML, $search, $replace, $limit)
Expand All @@ -393,15 +407,21 @@ protected function setValueForPart($documentPartXML, $search, $replace, $limit)
$replace = utf8_encode($replace);
}

$regExpDelim = '/';
$escapedSearch = preg_quote($search, $regExpDelim);
return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit);
// Note: we can't use the same function for both cases here, because of performance considerations.
if (self::MAXIMUM_REPLACEMENTS_DEFAULT === $limit) {
return str_replace($search, $replace, $documentPartXML);
} else {
$regExpDelim = '/';
$escapedSearch = preg_quote($search, $regExpDelim);
return preg_replace("{$regExpDelim}{$escapedSearch}{$regExpDelim}u", $replace, $documentPartXML, $limit);
}
}

/**
* Find all variables in $documentPartXML.
*
* @param string $documentPartXML
*
* @return string[]
*/
protected function getVariablesForPart($documentPartXML)
Expand All @@ -415,6 +435,7 @@ protected function getVariablesForPart($documentPartXML)
* Get the name of the footer file for $index.
*
* @param integer $index
*
* @return string
*/
protected function getFooterName($index)
Expand All @@ -426,6 +447,7 @@ protected function getFooterName($index)
* Get the name of the header file for $index.
*
* @param integer $index
*
* @return string
*/
protected function getHeaderName($index)
Expand All @@ -437,7 +459,9 @@ protected function getHeaderName($index)
* Find the start position of the nearest table row before $offset.
*
* @param integer $offset
*
* @return integer
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
protected function findRowStart($offset)
Expand All @@ -458,6 +482,7 @@ protected function findRowStart($offset)
* Find the end position of the nearest table row after $offset.
*
* @param integer $offset
*
* @return integer
*/
protected function findRowEnd($offset)
Expand All @@ -470,6 +495,7 @@ protected function findRowEnd($offset)
*
* @param integer $startPosition
* @param integer $endPosition
*
* @return string
*/
protected function getSlice($startPosition, $endPosition = 0)
Expand Down
2 changes: 1 addition & 1 deletion tests/PhpWord/Tests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function testCloneRow()
* @covers ::saveAs
* @test
*/
public function testVariablesCanBeReplacedInHeaderAndFooter()
public function testMacrosCanBeReplacedInHeaderAndFooter()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');

Expand Down

0 comments on commit 3d78ee7

Please sign in to comment.