diff --git a/CHANGELOG.md b/CHANGELOG.md index 28ffef7298..220d2eeb20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). v0.16.0 (xx dec 2018) ---------------------- ### Added +- Add getVariableCount method in TemplateProcessor. @nicoder #1272 - Add setting Chart Title and Legend visibility @Tom-Magill #1433 - Add ability to pass a Style object in Section constructor @ndench #1416 - Add support for hidden text @Alexmg86 #1527 diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 86d0c07de3..7996b7f8f1 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -234,23 +234,39 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_ } /** - * Returns array of all variables in template. + * Returns count of all variables in template. * - * @return string[] + * @return array */ - public function getVariables() + public function getVariableCount() { $variables = $this->getVariablesForPart($this->tempDocumentMainPart); foreach ($this->tempDocumentHeaders as $headerXML) { - $variables = array_merge($variables, $this->getVariablesForPart($headerXML)); + $variables = array_merge( + $variables, + $this->getVariablesForPart($headerXML) + ); } foreach ($this->tempDocumentFooters as $footerXML) { - $variables = array_merge($variables, $this->getVariablesForPart($footerXML)); + $variables = array_merge( + $variables, + $this->getVariablesForPart($footerXML) + ); } - return array_unique($variables); + return array_count_values($variables); + } + + /** + * Returns array of all variables in template. + * + * @return string[] + */ + public function getVariables() + { + return array_keys($this->getVariableCount()); } /** diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 8839200d35..70cf2b74e2 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -224,6 +224,44 @@ public function testCloneDeleteBlock() $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); + + $templateProcessor = new TemplateProcessor($templatePath); + $variableCount = $templateProcessor->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 + ); + } + /** * @covers ::cloneBlock * @test @@ -242,6 +280,7 @@ public function cloneBlockCanCloneABlockTwice() foreach ($documentElements as $documentElement) { $section->addText($documentElement); } + $objWriter = IOFactory::createWriter($phpWord); $templatePath = 'test.docx'; $objWriter->save($templatePath);