From 75620caf5149aa79aa347dda81272b74116adfb2 Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 27 Dec 2018 01:44:37 +0100 Subject: [PATCH] add parameter to keep backward compatibility + add test --- CHANGELOG.md | 1 + src/PhpWord/TemplateProcessor.php | 19 +++++++++---- tests/PhpWord/TemplateProcessorTest.php | 36 +++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a140bf4..d440989c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ v0.16.0 (xx dec 2018) - Add support for hidden text @Alexmg86 #1527 - Add support for setting images in TemplateProcessor @SailorMax #1170 - Add "Plain Text" type to SDT (Structured Document Tags) @morrisdj #1541 +- Added possibility to index variables inside cloned block in TemplateProcessor @JPBetley #817 ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269 diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 6d3a1348..727dd74c 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -629,12 +629,13 @@ class TemplateProcessor * Clone a block. * * @param string $blockname - * @param int $clones + * @param int $clones How many time the block should be cloned * @param bool $replace + * @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...) * * @return string|null */ - public function cloneBlock($blockname, $clones = 1, $replace = true) + public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false) { $xmlBlock = null; preg_match( @@ -645,7 +646,14 @@ class TemplateProcessor if (isset($matches[3])) { $xmlBlock = $matches[3]; - $cloned = $this->indexClonedVariables($clones, $xmlBlock); + if ($indexVariables) { + $cloned = $this->indexClonedVariables($clones, $xmlBlock); + } else { + $cloned = array(); + for ($i = 1; $i <= $clones; $i++) { + $cloned[] = $xmlBlock; + } + } if ($replace) { $this->tempDocumentMainPart = str_replace( @@ -935,10 +943,10 @@ class TemplateProcessor } /** - * Replaces variable names in cloned + * Replaces variable names in cloned * rows/blocks with indexed names * - * @param integer $count + * @param int $count * @param string $xmlBlock * * @return string @@ -949,6 +957,7 @@ class TemplateProcessor for ($i = 1; $i <= $count; $i++) { $results[] = preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlBlock); } + return $results; } } diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 36432dc5..2c388299 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -409,7 +409,7 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase - This block will be cloned + This block will be cloned with ${variable} @@ -421,7 +421,39 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase $templateProcessor = new TestableTemplateProcesor($mainPart); $templateProcessor->cloneBlock('CLONEME', 3); - $this->assertEquals(3, substr_count($templateProcessor->getMainPart(), 'This block will be cloned')); + $this->assertEquals(3, substr_count($templateProcessor->getMainPart(), 'This block will be cloned with ${variable}')); + } + + /** + * @covers ::cloneBlock + * @test + */ + public function testCloneBlockWithVariables() + { + $mainPart = ' + + + + ${CLONEME} + + + + + Address ${address}, Street ${street} + + + + + ${/CLONEME} + + '; + + $templateProcessor = new TestableTemplateProcesor($mainPart); + $templateProcessor->cloneBlock('CLONEME', 3, true, true); + + $this->assertContains('Address ${address#1}, Street ${street#1}', $templateProcessor->getMainPart()); + $this->assertContains('Address ${address#2}, Street ${street#2}', $templateProcessor->getMainPart()); + $this->assertContains('Address ${address#3}, Street ${street#3}', $templateProcessor->getMainPart()); } /**