Skip to content

Commit 07e97c3

Browse files
committed
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`)
1 parent cf6319d commit 07e97c3

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,32 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
232232
$this->tempDocumentFooters = $this->setValueForPart($search, $replace, $this->tempDocumentFooters, $limit);
233233
}
234234

235+
/**
236+
* Returns count of all variables in template.
237+
*
238+
* @return array
239+
*/
240+
public function getVariableCount()
241+
{
242+
$variables = $this->getVariablesForPart($this->tempDocumentMainPart);
243+
244+
foreach ($this->tempDocumentHeaders as $headerXML) {
245+
$variables = array_merge(
246+
$variables,
247+
$this->getVariablesForPart($headerXML)
248+
);
249+
}
250+
251+
foreach ($this->tempDocumentFooters as $footerXML) {
252+
$variables = array_merge(
253+
$variables,
254+
$this->getVariablesForPart($footerXML)
255+
);
256+
}
257+
258+
return array_count_values($variables);
259+
}
260+
235261
/**
236262
* Returns array of all variables in template.
237263
*

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,41 @@ public function testCloneDeleteBlock()
223223
unlink($docName);
224224
$this->assertTrue($docFound);
225225
}
226+
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+
$variableCount = (new TemplateProcessor($templatePath))->getVariableCount();
252+
unlink($templatePath);
253+
254+
$this->assertEquals(
255+
array(
256+
'a_field_that_is_present_three_times' => 3,
257+
'a_field_that_is_present_twice' => 2,
258+
'a_field_that_is_present_one_time' => 1,
259+
),
260+
$variableCount
261+
);
262+
}
226263
}

0 commit comments

Comments
 (0)