add parameter to keep backward compatibility + add test
This commit is contained in:
parent
1bcef04ddc
commit
75620caf51
|
|
@ -12,6 +12,7 @@ v0.16.0 (xx dec 2018)
|
|||
- Add support for hidden text @Alexmg86 #1527
|
||||
- Add support for setting images in TemplateProcessor @SailorMax #1170
|
||||
- Add "Plain Text" type to SDT (Structured Document Tags) @morrisdj #1541
|
||||
- Added possibility to index variables inside cloned block in TemplateProcessor @JPBetley #817
|
||||
|
||||
### Fixed
|
||||
- Fix regex in `cloneBlock` function @nicoder #1269
|
||||
|
|
|
|||
|
|
@ -629,12 +629,13 @@ class TemplateProcessor
|
|||
* Clone a block.
|
||||
*
|
||||
* @param string $blockname
|
||||
* @param int $clones
|
||||
* @param int $clones How many time the block should be cloned
|
||||
* @param bool $replace
|
||||
* @param bool $indexVariables If true, any variables inside the block will be indexed (postfixed with #1, #2, ...)
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function cloneBlock($blockname, $clones = 1, $replace = true)
|
||||
public function cloneBlock($blockname, $clones = 1, $replace = true, $indexVariables = false)
|
||||
{
|
||||
$xmlBlock = null;
|
||||
preg_match(
|
||||
|
|
@ -645,7 +646,14 @@ class TemplateProcessor
|
|||
|
||||
if (isset($matches[3])) {
|
||||
$xmlBlock = $matches[3];
|
||||
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
|
||||
if ($indexVariables) {
|
||||
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
|
||||
} else {
|
||||
$cloned = array();
|
||||
for ($i = 1; $i <= $clones; $i++) {
|
||||
$cloned[] = $xmlBlock;
|
||||
}
|
||||
}
|
||||
|
||||
if ($replace) {
|
||||
$this->tempDocumentMainPart = str_replace(
|
||||
|
|
@ -935,10 +943,10 @@ class TemplateProcessor
|
|||
}
|
||||
|
||||
/**
|
||||
* Replaces variable names in cloned
|
||||
* Replaces variable names in cloned
|
||||
* rows/blocks with indexed names
|
||||
*
|
||||
* @param integer $count
|
||||
* @param int $count
|
||||
* @param string $xmlBlock
|
||||
*
|
||||
* @return string
|
||||
|
|
@ -949,6 +957,7 @@ class TemplateProcessor
|
|||
for ($i = 1; $i <= $count; $i++) {
|
||||
$results[] = preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlBlock);
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -409,7 +409,7 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
|||
</w:p>
|
||||
<w:p>
|
||||
<w:r>
|
||||
<w:t xml:space="preserve">This block will be cloned</w:t>
|
||||
<w:t xml:space="preserve">This block will be cloned with ${variable}</w:t>
|
||||
</w:r>
|
||||
</w:p>
|
||||
<w:p>
|
||||
|
|
@ -421,7 +421,39 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
|||
$templateProcessor = new TestableTemplateProcesor($mainPart);
|
||||
$templateProcessor->cloneBlock('CLONEME', 3);
|
||||
|
||||
$this->assertEquals(3, substr_count($templateProcessor->getMainPart(), 'This block will be cloned'));
|
||||
$this->assertEquals(3, substr_count($templateProcessor->getMainPart(), 'This block will be cloned with ${variable}'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::cloneBlock
|
||||
* @test
|
||||
*/
|
||||
public function testCloneBlockWithVariables()
|
||||
{
|
||||
$mainPart = '<?xml version="1.0" encoding="UTF-8"?>
|
||||
<w:p>
|
||||
<w:r>
|
||||
<w:rPr></w:rPr>
|
||||
<w:t>${CLONEME}</w:t>
|
||||
</w:r>
|
||||
</w:p>
|
||||
<w:p>
|
||||
<w:r>
|
||||
<w:t xml:space="preserve">Address ${address}, Street ${street}</w:t>
|
||||
</w:r>
|
||||
</w:p>
|
||||
<w:p>
|
||||
<w:r w:rsidRPr="00204FED">
|
||||
<w:t>${/CLONEME}</w:t>
|
||||
</w:r>
|
||||
</w:p>';
|
||||
|
||||
$templateProcessor = new TestableTemplateProcesor($mainPart);
|
||||
$templateProcessor->cloneBlock('CLONEME', 3, true, true);
|
||||
|
||||
$this->assertContains('Address ${address#1}, Street ${street#1}', $templateProcessor->getMainPart());
|
||||
$this->assertContains('Address ${address#2}, Street ${street#2}', $templateProcessor->getMainPart());
|
||||
$this->assertContains('Address ${address#3}, Street ${street#3}', $templateProcessor->getMainPart());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue