add variable indexing for block cloning
Use the same functionality from cloneRow to index variables inside cloned block sections
This commit is contained in:
parent
c6fff318f3
commit
f51422ad8d
|
|
@ -274,9 +274,7 @@ class TemplateProcessor
|
|||
}
|
||||
|
||||
$result = $this->getSlice(0, $rowStart);
|
||||
for ($i = 1; $i <= $numberOfClones; $i++) {
|
||||
$result .= preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlRow);
|
||||
}
|
||||
$result .= implode($this->indexClonedVariables($numberOfClones, $xmlRow));
|
||||
$result .= $this->getSlice($rowEnd);
|
||||
|
||||
$this->tempDocumentMainPart = $result;
|
||||
|
|
@ -302,10 +300,7 @@ class TemplateProcessor
|
|||
|
||||
if (isset($matches[3])) {
|
||||
$xmlBlock = $matches[3];
|
||||
$cloned = array();
|
||||
for ($i = 1; $i <= $clones; $i++) {
|
||||
$cloned[] = $xmlBlock;
|
||||
}
|
||||
$cloned = $this->indexClonedVariables($clones, $xmlBlock);
|
||||
|
||||
if ($replace) {
|
||||
$this->tempDocumentMainPart = str_replace(
|
||||
|
|
@ -545,4 +540,22 @@ class TemplateProcessor
|
|||
|
||||
return substr($this->tempDocumentMainPart, $startPosition, ($endPosition - $startPosition));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces variable names in cloned
|
||||
* rows/blocks with indexed names
|
||||
*
|
||||
* @param integer $count
|
||||
* @param string $xmlBlock
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function indexClonedVariables($count, $xmlBlock)
|
||||
{
|
||||
$results = [];
|
||||
for ($i = 1; $i <= $count; $i++) {
|
||||
$results[] = preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlBlock);
|
||||
}
|
||||
return $results;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -199,13 +199,14 @@ final class TemplateProcessorTest extends \PHPUnit_Framework_TestCase
|
|||
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');
|
||||
|
||||
$this->assertEquals(
|
||||
array('DELETEME', '/DELETEME', 'CLONEME', '/CLONEME'),
|
||||
array('DELETEME', '/DELETEME', 'CLONEME', 'blockVariable', '/CLONEME'),
|
||||
$templateProcessor->getVariables()
|
||||
);
|
||||
|
||||
$docName = 'clone-delete-block-result.docx';
|
||||
$templateProcessor->cloneBlock('CLONEME', 3);
|
||||
$templateProcessor->deleteBlock('DELETEME');
|
||||
$templateProcessor->setValue('blockVariable#3', 'Test');
|
||||
$templateProcessor->saveAs($docName);
|
||||
$docFound = file_exists($docName);
|
||||
unlink($docName);
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue