Merge pull request #1269 from nicoder/improve-cloneBlock-regex
Fix regex in `cloneBlock` function
This commit is contained in:
commit
f376f207b8
|
|
@ -3,6 +3,13 @@ Change Log
|
|||
All notable changes to this project will be documented in this file.
|
||||
This project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
v0.16.0 (xx xxx 2018)
|
||||
----------------------
|
||||
### Added
|
||||
|
||||
### Fixed
|
||||
- Fix regex in `cloneBlock` function @nicoder #1269
|
||||
|
||||
v0.15.0 (14 Jul 2018)
|
||||
----------------------
|
||||
### Added
|
||||
|
|
|
|||
|
|
@ -323,7 +323,7 @@ class TemplateProcessor
|
|||
{
|
||||
$xmlBlock = null;
|
||||
preg_match(
|
||||
'/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
|
||||
'/(<\?xml.*)(<w:p\b.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p\b.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
|
||||
$this->tempDocumentMainPart,
|
||||
$matches
|
||||
);
|
||||
|
|
|
|||
|
|
@ -223,4 +223,57 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
|
|||
unlink($docName);
|
||||
$this->assertTrue($docFound);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::cloneBlock
|
||||
* @test
|
||||
*/
|
||||
public function cloneBlockCanCloneABlockTwice()
|
||||
{
|
||||
// create template with placeholders and block
|
||||
$phpWord = new PhpWord();
|
||||
$section = $phpWord->addSection();
|
||||
$documentElements = array(
|
||||
'Title: ${title}',
|
||||
'${subreport}',
|
||||
'${subreport.id}: ${subreport.text}. ',
|
||||
'${/subreport}',
|
||||
);
|
||||
foreach ($documentElements as $documentElement) {
|
||||
$section->addText($documentElement);
|
||||
}
|
||||
$objWriter = IOFactory::createWriter($phpWord);
|
||||
$templatePath = 'test.docx';
|
||||
$objWriter->save($templatePath);
|
||||
|
||||
// replace placeholders and save the file
|
||||
$templateProcessor = new TemplateProcessor($templatePath);
|
||||
$templateProcessor->setValue('title', 'Some title');
|
||||
$templateProcessor->cloneBlock('subreport', 2);
|
||||
$templateProcessor->setValue('subreport.id', '123', 1);
|
||||
$templateProcessor->setValue('subreport.text', 'Some text', 1);
|
||||
$templateProcessor->setValue('subreport.id', '456', 1);
|
||||
$templateProcessor->setValue('subreport.text', 'Some other text', 1);
|
||||
$templateProcessor->saveAs($templatePath);
|
||||
|
||||
// assert the block has been cloned twice
|
||||
// and the placeholders have been replaced correctly
|
||||
$phpWord = IOFactory::load($templatePath);
|
||||
$sections = $phpWord->getSections();
|
||||
/** @var \PhpOffice\PhpWord\Element\TextRun[] $actualElements */
|
||||
$actualElements = $sections[0]->getElements();
|
||||
unlink($templatePath);
|
||||
$expectedElements = array(
|
||||
'Title: Some title',
|
||||
'123: Some text. ',
|
||||
'456: Some other text. ',
|
||||
);
|
||||
$this->assertCount(count($expectedElements), $actualElements);
|
||||
foreach ($expectedElements as $i => $expectedElement) {
|
||||
$this->assertEquals(
|
||||
$expectedElement,
|
||||
$actualElements[$i]->getElement(0)->getText()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue