Skip to content

Commit f376f20

Browse files
authored
Merge pull request #1269 from nicoder/improve-cloneBlock-regex
Fix regex in `cloneBlock` function
2 parents 4c9e750 + 1951db5 commit f376f20

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Change Log
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
v0.16.0 (xx xxx 2018)
7+
----------------------
8+
### Added
9+
10+
### Fixed
11+
- Fix regex in `cloneBlock` function @nicoder #1269
12+
613
v0.15.0 (14 Jul 2018)
714
----------------------
815
### Added

src/PhpWord/TemplateProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public function cloneBlock($blockname, $clones = 1, $replace = true)
323323
{
324324
$xmlBlock = null;
325325
preg_match(
326-
'/(<\?xml.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
326+
'/(<\?xml.*)(<w:p\b.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p\b.*\${\/' . $blockname . '}<\/w:.*?p>)/is',
327327
$this->tempDocumentMainPart,
328328
$matches
329329
);

tests/PhpWord/TemplateProcessorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,57 @@ public function testCloneDeleteBlock()
223223
unlink($docName);
224224
$this->assertTrue($docFound);
225225
}
226+
227+
/**
228+
* @covers ::cloneBlock
229+
* @test
230+
*/
231+
public function cloneBlockCanCloneABlockTwice()
232+
{
233+
// create template with placeholders and block
234+
$phpWord = new PhpWord();
235+
$section = $phpWord->addSection();
236+
$documentElements = array(
237+
'Title: ${title}',
238+
'${subreport}',
239+
'${subreport.id}: ${subreport.text}. ',
240+
'${/subreport}',
241+
);
242+
foreach ($documentElements as $documentElement) {
243+
$section->addText($documentElement);
244+
}
245+
$objWriter = IOFactory::createWriter($phpWord);
246+
$templatePath = 'test.docx';
247+
$objWriter->save($templatePath);
248+
249+
// replace placeholders and save the file
250+
$templateProcessor = new TemplateProcessor($templatePath);
251+
$templateProcessor->setValue('title', 'Some title');
252+
$templateProcessor->cloneBlock('subreport', 2);
253+
$templateProcessor->setValue('subreport.id', '123', 1);
254+
$templateProcessor->setValue('subreport.text', 'Some text', 1);
255+
$templateProcessor->setValue('subreport.id', '456', 1);
256+
$templateProcessor->setValue('subreport.text', 'Some other text', 1);
257+
$templateProcessor->saveAs($templatePath);
258+
259+
// assert the block has been cloned twice
260+
// and the placeholders have been replaced correctly
261+
$phpWord = IOFactory::load($templatePath);
262+
$sections = $phpWord->getSections();
263+
/** @var \PhpOffice\PhpWord\Element\TextRun[] $actualElements */
264+
$actualElements = $sections[0]->getElements();
265+
unlink($templatePath);
266+
$expectedElements = array(
267+
'Title: Some title',
268+
'123: Some text. ',
269+
'456: Some other text. ',
270+
);
271+
$this->assertCount(count($expectedElements), $actualElements);
272+
foreach ($expectedElements as $i => $expectedElement) {
273+
$this->assertEquals(
274+
$expectedElement,
275+
$actualElements[$i]->getElement(0)->getText()
276+
);
277+
}
278+
}
226279
}

0 commit comments

Comments
 (0)