From 07e97c38cd8843392f83c20b9b109cc6e3cd57b7 Mon Sep 17 00:00:00 2001 From: Nicolas Dermine Date: Mon, 5 Feb 2018 17:45:24 +0100 Subject: [PATCH 1/4] add `getVariableCount` method to `TemplateProcessor` returns how many times each placeholder is present in the document almost the same code as `getVariables` useful when cloning a block a number of times and want to replace placeholders that are present more than once in the block (using the `$limit` parameter of `setValue`) --- src/PhpWord/TemplateProcessor.php | 26 +++++++++++++++++ tests/PhpWord/TemplateProcessorTest.php | 37 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 5dd7b0bf..f5df06b1 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -232,6 +232,32 @@ class TemplateProcessor $this->tempDocumentFooters = $this->setValueForPart($search, $replace, $this->tempDocumentFooters, $limit); } + /** + * Returns count of all variables in template. + * + * @return array + */ + public function getVariableCount() + { + $variables = $this->getVariablesForPart($this->tempDocumentMainPart); + + foreach ($this->tempDocumentHeaders as $headerXML) { + $variables = array_merge( + $variables, + $this->getVariablesForPart($headerXML) + ); + } + + foreach ($this->tempDocumentFooters as $footerXML) { + $variables = array_merge( + $variables, + $this->getVariablesForPart($footerXML) + ); + } + + return array_count_values($variables); + } + /** * Returns array of all variables in template. * diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 7b064ef7..187e5686 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -223,4 +223,41 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase unlink($docName); $this->assertTrue($docFound); } + + /** + * @covers ::getVariableCount + * @test + */ + public function getVariableCountCountsHowManyTimesEachPlaceholderIsPresent() + { + // create template with placeholders + $phpWord = new PhpWord(); + $section = $phpWord->addSection(); + $header = $section->addHeader(); + $header->addText('${a_field_that_is_present_three_times}'); + $footer = $section->addFooter(); + $footer->addText('${a_field_that_is_present_twice}'); + $section2 = $phpWord->addSection(); + $section2->addText(' + ${a_field_that_is_present_one_time} + ${a_field_that_is_present_three_times} + ${a_field_that_is_present_twice} + ${a_field_that_is_present_three_times} + '); + $objWriter = IOFactory::createWriter($phpWord); + $templatePath = 'test.docx'; + $objWriter->save($templatePath); + + $variableCount = (new TemplateProcessor($templatePath))->getVariableCount(); + unlink($templatePath); + + $this->assertEquals( + array( + 'a_field_that_is_present_three_times' => 3, + 'a_field_that_is_present_twice' => 2, + 'a_field_that_is_present_one_time' => 1, + ), + $variableCount + ); + } } From 623bd993d8bebeae4d4d4da21b19b7f7ffbfe8e2 Mon Sep 17 00:00:00 2001 From: Nicolas Dermine Date: Mon, 5 Feb 2018 17:49:23 +0100 Subject: [PATCH 2/4] refactor: use extracted method in original method --- src/PhpWord/TemplateProcessor.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index f5df06b1..2cc56728 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -265,17 +265,7 @@ class TemplateProcessor */ public function getVariables() { - $variables = $this->getVariablesForPart($this->tempDocumentMainPart); - - foreach ($this->tempDocumentHeaders as $headerXML) { - $variables = array_merge($variables, $this->getVariablesForPart($headerXML)); - } - - foreach ($this->tempDocumentFooters as $footerXML) { - $variables = array_merge($variables, $this->getVariablesForPart($footerXML)); - } - - return array_unique($variables); + return array_keys($this->getVariableCount()); } /** From 12ad6fa865cee81bd3b8f84c5f0c970a568b0973 Mon Sep 17 00:00:00 2001 From: Nicolas Dermine Date: Tue, 6 Feb 2018 09:30:24 +0100 Subject: [PATCH 3/4] adapt code for php 5.3 --- tests/PhpWord/TemplateProcessorTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 187e5686..28ade1a3 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -248,7 +248,8 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase $templatePath = 'test.docx'; $objWriter->save($templatePath); - $variableCount = (new TemplateProcessor($templatePath))->getVariableCount(); + $templateProcessor = new TemplateProcessor($templatePath); + $variableCount = $templateProcessor->getVariableCount(); unlink($templatePath); $this->assertEquals( From 8f85424fb7efc2dd43f391234fbe7fdaa3fcb35b Mon Sep 17 00:00:00 2001 From: troosan Date: Tue, 11 Dec 2018 21:30:18 +0100 Subject: [PATCH 4/4] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index abf34834..646f4e97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). v0.16.0 (xx xxx 2018) ---------------------- ### Added +- Add getVariableCount method in TemplateProcessor. @nicoder #1272 ### Fixed - Fix regex in `cloneBlock` function @nicoder #1269