Merge pull request #1272 from nicoder/add-getVariableCount-method

Add getVariableCount method in TemplateProcessor
This commit is contained in:
troosan 2018-12-11 21:56:14 +01:00 committed by GitHub
commit 0db21ae115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 11 deletions

View File

@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
v0.16.0 (xx dec 2018) v0.16.0 (xx dec 2018)
---------------------- ----------------------
### Added ### Added
- Add getVariableCount method in TemplateProcessor. @nicoder #1272
- Add setting Chart Title and Legend visibility @Tom-Magill #1433 - Add setting Chart Title and Legend visibility @Tom-Magill #1433
- Add ability to pass a Style object in Section constructor @ndench #1416 - Add ability to pass a Style object in Section constructor @ndench #1416
- Add support for hidden text @Alexmg86 #1527 - Add support for hidden text @Alexmg86 #1527

View File

@ -233,6 +233,32 @@ class TemplateProcessor
$this->tempDocumentFooters = $this->setValueForPart($search, $replace, $this->tempDocumentFooters, $limit); $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. * Returns array of all variables in template.
* *
@ -240,17 +266,7 @@ class TemplateProcessor
*/ */
public function getVariables() public function getVariables()
{ {
$variables = $this->getVariablesForPart($this->tempDocumentMainPart); return array_keys($this->getVariableCount());
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);
} }
/** /**

View File

@ -224,6 +224,44 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
$this->assertTrue($docFound); $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 * @covers ::cloneBlock
* @test * @test
@ -242,6 +280,7 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
foreach ($documentElements as $documentElement) { foreach ($documentElements as $documentElement) {
$section->addText($documentElement); $section->addText($documentElement);
} }
$objWriter = IOFactory::createWriter($phpWord); $objWriter = IOFactory::createWriter($phpWord);
$templatePath = 'test.docx'; $templatePath = 'test.docx';
$objWriter->save($templatePath); $objWriter->save($templatePath);