From 50c44cc0a731b0536989c939d4569dc9898cd06a Mon Sep 17 00:00:00 2001 From: SnipsMine <46484375+SnipsMine@users.noreply.github.com> Date: Tue, 6 Feb 2024 20:02:36 +0100 Subject: [PATCH 1/4] Changed ensureUtf8Encoded to work with 0 I changed the function ensureUtf8Encoded so that it only returns an empty string when it is null. --- src/PhpWord/TemplateProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 8aee40c546..4784640166 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro) */ protected static function ensureUtf8Encoded($subject) { - return $subject ? Text::toUTF8($subject) : ''; + return !is_null($subject) ? Text::toUTF8($subject) : ''; } /** From 383ba5760a96ac9974fcdcb0b4554154b6f49c73 Mon Sep 17 00:00:00 2001 From: SnipsMine Date: Tue, 6 Feb 2024 20:59:33 +0100 Subject: [PATCH 2/4] Created a test for the function ensureUtf8Encoded via setValue --- tests/PhpWordTests/TemplateProcessorTest.php | 97 ++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/tests/PhpWordTests/TemplateProcessorTest.php b/tests/PhpWordTests/TemplateProcessorTest.php index 49e88d1b5b..807bdda418 100644 --- a/tests/PhpWordTests/TemplateProcessorTest.php +++ b/tests/PhpWordTests/TemplateProcessorTest.php @@ -1630,4 +1630,101 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void $templateProcessor->setUpdateFields(false); self::assertStringContainsString('', $templateProcessor->getSettingsPart()); } + + public function testEnsureUtf8Encoded(): void + { + $mainPart = ' + + + + + + + + ${stringZero} + + + + + + + ${intZero} + + + + + + + ${stringTest} + + + + + + + ${null} + + + + + + + ${floatZero} + + + + + + + ${intTen} + + + + + + + ${boolFalse} + + + + + + + ${boolTrue} + + + + + '; + $templateProcessor = new TestableTemplateProcesor($mainPart); + + self::assertEquals( + ['stringZero', 'intZero', 'stringTest', 'null', 'floatZero', 'intTen', 'boolFalse', 'boolTrue'], + $templateProcessor->getVariables() + ); + + $templateProcessor->setValue('stringZero', '0'); + self::assertStringContainsString('0', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('intZero', 0); + self::assertStringContainsString('0', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('stringTest', 'test'); + self::assertStringContainsString('test', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('null', null); + self::assertStringContainsString('', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('floatZero', 0.00); + self::assertStringContainsString('0', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('intTen', 10); + self::assertStringContainsString('10', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('boolFalse', false); + self::assertStringContainsString('', $templateProcessor->getMainPart()); + + $templateProcessor->setValue('boolTrue', true); + self::assertStringContainsString('1', $templateProcessor->getMainPart()); + } } From 33aa056edd9c880bd6e0b385c480a27b11f2f981 Mon Sep 17 00:00:00 2001 From: SnipsMine <46484375+SnipsMine@users.noreply.github.com> Date: Tue, 6 Feb 2024 23:05:06 +0100 Subject: [PATCH 3/4] Made a small alteration to be complient with php-cs-fixer --- src/PhpWord/TemplateProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 4784640166..baf88b3018 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro) */ protected static function ensureUtf8Encoded($subject) { - return !is_null($subject) ? Text::toUTF8($subject) : ''; + return $subject !== null ? Text::toUTF8($subject) : ''; } /** From cb1ba73c909932d1de5314c0bfdd7fd00db6d31a Mon Sep 17 00:00:00 2001 From: SnipsMine <46484375+SnipsMine@users.noreply.github.com> Date: Tue, 6 Feb 2024 23:51:36 +0100 Subject: [PATCH 4/4] Removed ambiguity --- src/PhpWord/TemplateProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index baf88b3018..c3297eac76 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -269,7 +269,7 @@ protected static function ensureMacroCompleted($macro) */ protected static function ensureUtf8Encoded($subject) { - return $subject !== null ? Text::toUTF8($subject) : ''; + return ($subject !== null) ? Text::toUTF8($subject) : ''; } /**