Merge pull request #1272 from nicoder/add-getVariableCount-method
Add getVariableCount method in TemplateProcessor
This commit is contained in:
commit
0db21ae115
@ -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
|
||||
|
||||
@ -233,6 +233,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.
|
||||
*
|
||||
@ -240,17 +266,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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -224,6 +224,44 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
||||
$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 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
||||
foreach ($documentElements as $documentElement) {
|
||||
$section->addText($documentElement);
|
||||
}
|
||||
|
||||
$objWriter = IOFactory::createWriter($phpWord);
|
||||
$templatePath = 'test.docx';
|
||||
$objWriter->save($templatePath);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user