Skip to content

Commit 1d6365c

Browse files
committed
TemplateProcessor SetComplexBlock/Value and Sections
Fix PHPOffice#2561. Allow the TemplateProcessor methods `setComplexBlock` and `setComplexValue` to work with Sections.
1 parent 2f4da6e commit 1d6365c

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

docs/changes/2.x/2.0.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Bug fixes
1010

1111
- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)
12+
- TemplateProcessor setComplexBlock/Value and Section by [@oleibman](https://github.com/oleibman) fixing [#2561](https://github.com/PHPOffice/PHPWord/issues/2561) in [#2562](https://github.com/PHPOffice/PHPWord/pull/2562)
1213
- TemplateProcessor Persist File After Destruct [@oleibman](https://github.com/oleibman) fixing [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) in [#2545](https://github.com/PHPOffice/PHPWord/pull/2545)
1314
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)
1415

src/PhpWord/TemplateProcessor.php

+6
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ protected static function ensureUtf8Encoded($subject)
278278
public function setComplexValue($search, Element\AbstractElement $complexType): void
279279
{
280280
$elementName = substr(get_class($complexType), strrpos(get_class($complexType), '\\') + 1);
281+
if ($elementName === 'Section') {
282+
$elementName = 'Container';
283+
}
281284
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;
282285

283286
$xmlWriter = new XMLWriter();
@@ -305,6 +308,9 @@ public function setComplexValue($search, Element\AbstractElement $complexType):
305308
public function setComplexBlock($search, Element\AbstractElement $complexType): void
306309
{
307310
$elementName = substr(get_class($complexType), strrpos(get_class($complexType), '\\') + 1);
311+
if ($elementName === 'Section') {
312+
$elementName = 'Container';
313+
}
308314
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;
309315

310316
$xmlWriter = new XMLWriter();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @see https://github.com/PHPOffice/PHPWord
14+
*
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWordTests;
19+
20+
use PhpOffice\PhpWord\Element\Section;
21+
use PhpOffice\PhpWord\Element\TextRun;
22+
use PhpOffice\PhpWord\Shared\Html;
23+
use PhpOffice\PhpWord\TemplateProcessor;
24+
25+
/**
26+
* @covers \PhpOffice\PhpWord\TemplateProcessor
27+
*
28+
* @coversDefaultClass \PhpOffice\PhpWord\TemplateProcessor
29+
*
30+
* @runTestsInSeparateProcesses
31+
*/
32+
final class TemplateProcessorSectionTest extends \PHPUnit\Framework\TestCase
33+
{
34+
/** @var ?TemplateProcessor */
35+
private $templateProcessor;
36+
37+
private function getTemplateProcessor(string $filename): TemplateProcessor
38+
{
39+
$this->templateProcessor = new TemplateProcessor($filename);
40+
41+
return $this->templateProcessor;
42+
}
43+
44+
protected function tearDown(): void
45+
{
46+
if ($this->templateProcessor !== null) {
47+
$filename = $this->templateProcessor->getTempDocumentFilename();
48+
$this->templateProcessor = null;
49+
if (file_exists($filename)) {
50+
@unlink($filename);
51+
}
52+
}
53+
}
54+
55+
public function testSetComplexSection(): void
56+
{
57+
$templateProcessor = $this->getTemplateProcessor('C:/git/sectiontemplate/tests/PhpWordTests/_files/templates/document22-xml.docx');
58+
$html = '
59+
<p>&nbsp;Bug Report:</p>
60+
<p><span style="background-color: #ff0000;">BugTracker X</span> is ${facing1} an issue.</p>
61+
<p><span style="background-color: #00ff00;">BugTracker X</span> is ${facing2} an issue.</p>
62+
';
63+
$section = new Section(0);
64+
Html::addHtml($section, $html, false, false);
65+
$templateProcessor->setComplexBlock('test', $section);
66+
$facing1 = new TextRun();
67+
$facing1->addText('facing', ['bold' => true]);
68+
$facing2 = new TextRun();
69+
$facing2->addText('facing', ['italic' => true]);
70+
71+
$templateProcessor->setComplexBlock('test', $section);
72+
$templateProcessor->setComplexValue('facing1', $facing1);
73+
$templateProcessor->setComplexValue('facing2', $facing2);
74+
75+
$docName = $templateProcessor->save();
76+
$docFound = file_exists($docName);
77+
self::assertTrue($docFound);
78+
$contents = file_get_contents("zip://$docName#word/document2.xml");
79+
unlink($docName);
80+
self::assertNotFalse($contents);
81+
$contents = preg_replace('/>\s+</', '><', $contents) ?? '';
82+
self::assertStringContainsString('<w:t>Test</w:t>', $contents);
83+
self::assertStringContainsString('<w:r><w:rPr><w:b w:val="1"/><w:bCs w:val="1"/></w:rPr><w:t xml:space="preserve">facing</w:t></w:r>', $contents, 'bold string found');
84+
self::assertStringContainsString('<w:r><w:rPr><w:i w:val="1"/><w:iCs w:val="1"/></w:rPr><w:t xml:space="preserve">facing</w:t></w:r>', $contents, 'italic string found');
85+
self::assertStringNotContainsString('$', $contents, 'no leftover macros');
86+
self::assertStringNotContainsString('facing1', $contents, 'no leftover replaced string1');
87+
self::assertStringNotContainsString('facing2', $contents, 'no leftover replaced string2');
88+
}
89+
}

0 commit comments

Comments
 (0)