Skip to content

Commit

Permalink
TemplateProcessor SetComplexBlock/Value and Sections
Browse files Browse the repository at this point in the history
Fix PHPOffice#2561. Allow the TemplateProcessor methods `setComplexBlock` and `setComplexValue` to work with Sections.
  • Loading branch information
oleibman committed Jan 31, 2024
1 parent 2f4da6e commit 1d6365c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Bug fixes

- 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)
- 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)
- 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)
- 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)

Expand Down
6 changes: 6 additions & 0 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ protected static function ensureUtf8Encoded($subject)
public function setComplexValue($search, Element\AbstractElement $complexType): void
{
$elementName = substr(get_class($complexType), strrpos(get_class($complexType), '\\') + 1);
if ($elementName === 'Section') {
$elementName = 'Container';
}
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;

$xmlWriter = new XMLWriter();
Expand Down Expand Up @@ -305,6 +308,9 @@ public function setComplexValue($search, Element\AbstractElement $complexType):
public function setComplexBlock($search, Element\AbstractElement $complexType): void
{
$elementName = substr(get_class($complexType), strrpos(get_class($complexType), '\\') + 1);
if ($elementName === 'Section') {
$elementName = 'Container';
}
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $elementName;

$xmlWriter = new XMLWriter();
Expand Down
89 changes: 89 additions & 0 deletions tests/PhpWordTests/TemplateProcessorSectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @see https://github.com/PHPOffice/PHPWord
*
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWordTests;

use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\TemplateProcessor;

/**
* @covers \PhpOffice\PhpWord\TemplateProcessor
*
* @coversDefaultClass \PhpOffice\PhpWord\TemplateProcessor
*
* @runTestsInSeparateProcesses
*/
final class TemplateProcessorSectionTest extends \PHPUnit\Framework\TestCase
{
/** @var ?TemplateProcessor */
private $templateProcessor;

private function getTemplateProcessor(string $filename): TemplateProcessor
{
$this->templateProcessor = new TemplateProcessor($filename);

return $this->templateProcessor;
}

protected function tearDown(): void
{
if ($this->templateProcessor !== null) {
$filename = $this->templateProcessor->getTempDocumentFilename();
$this->templateProcessor = null;
if (file_exists($filename)) {
@unlink($filename);
}
}
}

public function testSetComplexSection(): void
{
$templateProcessor = $this->getTemplateProcessor('C:/git/sectiontemplate/tests/PhpWordTests/_files/templates/document22-xml.docx');
$html = '
<p>&nbsp;Bug Report:</p>
<p><span style="background-color: #ff0000;">BugTracker X</span> is ${facing1} an issue.</p>
<p><span style="background-color: #00ff00;">BugTracker X</span> is ${facing2} an issue.</p>
';
$section = new Section(0);
Html::addHtml($section, $html, false, false);
$templateProcessor->setComplexBlock('test', $section);
$facing1 = new TextRun();
$facing1->addText('facing', ['bold' => true]);
$facing2 = new TextRun();
$facing2->addText('facing', ['italic' => true]);

$templateProcessor->setComplexBlock('test', $section);
$templateProcessor->setComplexValue('facing1', $facing1);
$templateProcessor->setComplexValue('facing2', $facing2);

$docName = $templateProcessor->save();
$docFound = file_exists($docName);
self::assertTrue($docFound);
$contents = file_get_contents("zip://$docName#word/document2.xml");
unlink($docName);
self::assertNotFalse($contents);
$contents = preg_replace('/>\s+</', '><', $contents) ?? '';
self::assertStringContainsString('<w:t>Test</w:t>', $contents);
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');
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');
self::assertStringNotContainsString('$', $contents, 'no leftover macros');
self::assertStringNotContainsString('facing1', $contents, 'no leftover replaced string1');
self::assertStringNotContainsString('facing2', $contents, 'no leftover replaced string2');
}
}

0 comments on commit 1d6365c

Please sign in to comment.