Skip to content

Commit 66f0a2c

Browse files
Progi1984cavasinf
andauthored
Template Processor: Fix 0 considered as empty string (#2748)
* Fix `0` considered as empty string * Update changelog * Updated changelog & Added unit test --------- Co-authored-by: Florian CAVASIN <[email protected]>
1 parent 1be7a80 commit 66f0a2c

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

Diff for: docs/changes/1.x/1.4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- Reader Word2007: Support Header elements within Title elements by [@SpraxDev](https://github.com/SpraxDev) fixing [#2616](https://github.com/PHPOffice/PHPWord/issues/2616), [#2426](https://github.com/PHPOffice/PHPWord/issues/2426) in [#2674](https://github.com/PHPOffice/PHPWord/pull/2674)
2828
- Reader HTML: Support for inherit value for property line-height by [@Progi1984](https://github.com/Progi1984) fixing [#2683](https://github.com/PHPOffice/PHPWord/issues/2683) in [#2733](https://github.com/PHPOffice/PHPWord/pull/2733)
2929
- Writer HTML: Fixed null string for Text Elements by [@armagedon007](https://github.com/armagedon007) and [@Progi1984](https://github.com/Progi1984) in [#2738](https://github.com/PHPOffice/PHPWord/pull/2738)
30+
- Template Processor: Fix 0 considered as empty string by [@cavasinf](https://github.com/cavasinf), [@SnipsMine](https://github.com/SnipsMine) and [@Progi1984](https://github.com/Progi1984) fixing [#2572](https://github.com/PHPOffice/PHPWord/issues/2572), [#2703](https://github.com/PHPOffice/PHPWord/issues/2703) in [#2748](https://github.com/PHPOffice/PHPWord/pull/2748)
3031

3132
### Miscellaneous
3233

Diff for: src/PhpWord/TemplateProcessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro)
269269
*/
270270
protected static function ensureUtf8Encoded($subject)
271271
{
272-
return $subject ? Text::toUTF8($subject) : '';
272+
return (null !== $subject) ? Text::toUTF8($subject) : '';
273273
}
274274

275275
/**

Diff for: tests/PhpWordTests/TemplateProcessorTest.php

+97
Original file line numberDiff line numberDiff line change
@@ -1641,4 +1641,101 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
16411641
$templateProcessor->setUpdateFields(false);
16421642
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
16431643
}
1644+
1645+
public function testEnsureUtf8Encoded(): void
1646+
{
1647+
$mainPart = '<w:tbl>
1648+
<w:tr>
1649+
<w:tc>
1650+
<w:tcPr>
1651+
<w:vMerge w:val="restart"/>
1652+
</w:tcPr>
1653+
<w:p>
1654+
<w:r>
1655+
<w:t t=1>${stringZero}</w:t>
1656+
</w:r>
1657+
</w:p>
1658+
</w:tc>
1659+
<w:tc>
1660+
<w:p>
1661+
<w:r>
1662+
<w:t t=2>${intZero}</w:t>
1663+
</w:r>
1664+
</w:p>
1665+
</w:tc>
1666+
<w:tc>
1667+
<w:p>
1668+
<w:r>
1669+
<w:t t=3>${stringTest}</w:t>
1670+
</w:r>
1671+
</w:p>
1672+
</w:tc>
1673+
<w:tc>
1674+
<w:p>
1675+
<w:r>
1676+
<w:t t=4>${null}</w:t>
1677+
</w:r>
1678+
</w:p>
1679+
</w:tc>
1680+
<w:tc>
1681+
<w:p>
1682+
<w:r>
1683+
<w:t t=5>${floatZero}</w:t>
1684+
</w:r>
1685+
</w:p>
1686+
</w:tc>
1687+
<w:tc>
1688+
<w:p>
1689+
<w:r>
1690+
<w:t t=6>${intTen}</w:t>
1691+
</w:r>
1692+
</w:p>
1693+
</w:tc>
1694+
<w:tc>
1695+
<w:p>
1696+
<w:r>
1697+
<w:t t=7>${boolFalse}</w:t>
1698+
</w:r>
1699+
</w:p>
1700+
</w:tc>
1701+
<w:tc>
1702+
<w:p>
1703+
<w:r>
1704+
<w:t t=8>${boolTrue}</w:t>
1705+
</w:r>
1706+
</w:p>
1707+
</w:tc>
1708+
</w:tr>
1709+
</w:tbl>';
1710+
$templateProcessor = new TestableTemplateProcesor($mainPart);
1711+
1712+
self::assertEquals(
1713+
['stringZero', 'intZero', 'stringTest', 'null', 'floatZero', 'intTen', 'boolFalse', 'boolTrue'],
1714+
$templateProcessor->getVariables()
1715+
);
1716+
1717+
$templateProcessor->setValue('stringZero', '0');
1718+
self::assertStringContainsString('<w:t t=1>0</w:t>', $templateProcessor->getMainPart());
1719+
1720+
$templateProcessor->setValue('intZero', 0);
1721+
self::assertStringContainsString('<w:t t=2>0</w:t>', $templateProcessor->getMainPart());
1722+
1723+
$templateProcessor->setValue('stringTest', 'test');
1724+
self::assertStringContainsString('<w:t t=3>test</w:t>', $templateProcessor->getMainPart());
1725+
1726+
$templateProcessor->setValue('null', null);
1727+
self::assertStringContainsString('<w:t t=4></w:t>', $templateProcessor->getMainPart());
1728+
1729+
$templateProcessor->setValue('floatZero', 0.00);
1730+
self::assertStringContainsString('<w:t t=5>0</w:t>', $templateProcessor->getMainPart());
1731+
1732+
$templateProcessor->setValue('intTen', 10);
1733+
self::assertStringContainsString('<w:t t=6>10</w:t>', $templateProcessor->getMainPart());
1734+
1735+
$templateProcessor->setValue('boolFalse', false);
1736+
self::assertStringContainsString('<w:t t=7></w:t>', $templateProcessor->getMainPart());
1737+
1738+
$templateProcessor->setValue('boolTrue', true);
1739+
self::assertStringContainsString('<w:t t=8>1</w:t>', $templateProcessor->getMainPart());
1740+
}
16441741
}

0 commit comments

Comments
 (0)