Skip to content

Commit 0db21ae

Browse files
authored
Merge pull request #1272 from nicoder/add-getVariableCount-method
Add getVariableCount method in TemplateProcessor
2 parents cf3132a + b188ab9 commit 0db21ae

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
66
v0.16.0 (xx dec 2018)
77
----------------------
88
### Added
9+
- Add getVariableCount method in TemplateProcessor. @nicoder #1272
910
- Add setting Chart Title and Legend visibility @Tom-Magill #1433
1011
- Add ability to pass a Style object in Section constructor @ndench #1416
1112
- Add support for hidden text @Alexmg86 #1527

src/PhpWord/TemplateProcessor.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,23 +234,39 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
234234
}
235235

236236
/**
237-
* Returns array of all variables in template.
237+
* Returns count of all variables in template.
238238
*
239-
* @return string[]
239+
* @return array
240240
*/
241-
public function getVariables()
241+
public function getVariableCount()
242242
{
243243
$variables = $this->getVariablesForPart($this->tempDocumentMainPart);
244244

245245
foreach ($this->tempDocumentHeaders as $headerXML) {
246-
$variables = array_merge($variables, $this->getVariablesForPart($headerXML));
246+
$variables = array_merge(
247+
$variables,
248+
$this->getVariablesForPart($headerXML)
249+
);
247250
}
248251

249252
foreach ($this->tempDocumentFooters as $footerXML) {
250-
$variables = array_merge($variables, $this->getVariablesForPart($footerXML));
253+
$variables = array_merge(
254+
$variables,
255+
$this->getVariablesForPart($footerXML)
256+
);
251257
}
252258

253-
return array_unique($variables);
259+
return array_count_values($variables);
260+
}
261+
262+
/**
263+
* Returns array of all variables in template.
264+
*
265+
* @return string[]
266+
*/
267+
public function getVariables()
268+
{
269+
return array_keys($this->getVariableCount());
254270
}
255271

256272
/**

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,44 @@ public function testCloneDeleteBlock()
224224
$this->assertTrue($docFound);
225225
}
226226

227+
/**
228+
* @covers ::getVariableCount
229+
* @test
230+
*/
231+
public function getVariableCountCountsHowManyTimesEachPlaceholderIsPresent()
232+
{
233+
// create template with placeholders
234+
$phpWord = new PhpWord();
235+
$section = $phpWord->addSection();
236+
$header = $section->addHeader();
237+
$header->addText('${a_field_that_is_present_three_times}');
238+
$footer = $section->addFooter();
239+
$footer->addText('${a_field_that_is_present_twice}');
240+
$section2 = $phpWord->addSection();
241+
$section2->addText('
242+
${a_field_that_is_present_one_time}
243+
${a_field_that_is_present_three_times}
244+
${a_field_that_is_present_twice}
245+
${a_field_that_is_present_three_times}
246+
');
247+
$objWriter = IOFactory::createWriter($phpWord);
248+
$templatePath = 'test.docx';
249+
$objWriter->save($templatePath);
250+
251+
$templateProcessor = new TemplateProcessor($templatePath);
252+
$variableCount = $templateProcessor->getVariableCount();
253+
unlink($templatePath);
254+
255+
$this->assertEquals(
256+
array(
257+
'a_field_that_is_present_three_times' => 3,
258+
'a_field_that_is_present_twice' => 2,
259+
'a_field_that_is_present_one_time' => 1,
260+
),
261+
$variableCount
262+
);
263+
}
264+
227265
/**
228266
* @covers ::cloneBlock
229267
* @test
@@ -242,6 +280,7 @@ public function cloneBlockCanCloneABlockTwice()
242280
foreach ($documentElements as $documentElement) {
243281
$section->addText($documentElement);
244282
}
283+
245284
$objWriter = IOFactory::createWriter($phpWord);
246285
$templatePath = 'test.docx';
247286
$objWriter->save($templatePath);

0 commit comments

Comments
 (0)