Skip to content

Commit 1bcef04

Browse files
committed
Merge remote-tracking branch 'JPBetley/PHPWord/clone-block-variables' into rebase_pull_request
2 parents 1717bd4 + b011e6f commit 1bcef04

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,9 +619,7 @@ public function cloneRow($search, $numberOfClones)
619619
}
620620

621621
$result = $this->getSlice(0, $rowStart);
622-
for ($i = 1; $i <= $numberOfClones; $i++) {
623-
$result .= preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlRow);
624-
}
622+
$result .= implode($this->indexClonedVariables($numberOfClones, $xmlRow));
625623
$result .= $this->getSlice($rowEnd);
626624

627625
$this->tempDocumentMainPart = $result;
@@ -647,10 +645,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
647645

648646
if (isset($matches[3])) {
649647
$xmlBlock = $matches[3];
650-
$cloned = array();
651-
for ($i = 1; $i <= $clones; $i++) {
652-
$cloned[] = $xmlBlock;
653-
}
648+
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
654649

655650
if ($replace) {
656651
$this->tempDocumentMainPart = str_replace(
@@ -938,4 +933,22 @@ protected function getSlice($startPosition, $endPosition = 0)
938933

939934
return substr($this->tempDocumentMainPart, $startPosition, ($endPosition - $startPosition));
940935
}
936+
937+
/**
938+
* Replaces variable names in cloned
939+
* rows/blocks with indexed names
940+
*
941+
* @param integer $count
942+
* @param string $xmlBlock
943+
*
944+
* @return string
945+
*/
946+
protected function indexClonedVariables($count, $xmlBlock)
947+
{
948+
$results = array();
949+
for ($i = 1; $i <= $count; $i++) {
950+
$results[] = preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlBlock);
951+
}
952+
return $results;
953+
}
941954
}

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,14 @@ public function testCloneDeleteBlock()
288288
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');
289289

290290
$this->assertEquals(
291-
array('DELETEME', '/DELETEME', 'CLONEME', '/CLONEME'),
291+
array('DELETEME', '/DELETEME', 'CLONEME', 'blockVariable', '/CLONEME'),
292292
$templateProcessor->getVariables()
293293
);
294294

295295
$docName = 'clone-delete-block-result.docx';
296296
$templateProcessor->cloneBlock('CLONEME', 3);
297297
$templateProcessor->deleteBlock('DELETEME');
298+
$templateProcessor->setValue('blockVariable#3', 'Test');
298299
$templateProcessor->saveAs($docName);
299300
$docFound = file_exists($docName);
300301
unlink($docName);
Binary file not shown.

0 commit comments

Comments
 (0)