Skip to content

Commit e801c49

Browse files
moghwanMichaelPFrey
authored andcommitted
Feature: add limit option to setvalues (#2640)
* adding limit option to setValues for multiple values * add parameter type * Update 2.0.0.md * add more replacement for testSetValues * testSetValues with different limit values * testSetValues with a zero replacement * Update 1.3.0.md * improve tests with variables count check to ensure number of occurrences replaced * Update 1.4.0.md * Update 1.3.0.md
1 parent b5ccad2 commit e801c49

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

docs/changes/1.x/1.4.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
- Bump dompdf/dompdf from 2.0.4 to 3.0.0 by [@dependabot](https://github.com/dependabot) fixing [#2621](https://github.com/PHPOffice/PHPWord/issues/2621) in [#2666](https://github.com/PHPOffice/PHPWord/pull/2666)
3636
- Add test case to make sure vMerge defaults to 'continue' by [@SpraxDev](https://github.com/SpraxDev) in [#2677](https://github.com/PHPOffice/PHPWord/pull/2677)
37+
- Adding the possibility to use iterate search and replace with setValues by [@moghwan](https://github.com/moghwan) in [#2632](https://github.com/PHPOffice/PHPWord/pull/2640)
3738

3839
### Deprecations
3940
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`

src/PhpWord/TemplateProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
362362
/**
363363
* Set values from a one-dimensional array of "variable => value"-pairs.
364364
*/
365-
public function setValues(array $values): void
365+
public function setValues(array $values, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT): void
366366
{
367367
foreach ($values as $macro => $replace) {
368-
$this->setValue($macro, $replace);
368+
$this->setValue($macro, $replace, $limit);
369369
}
370370
}
371371

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,12 +615,53 @@ public function testSetValues(): void
615615
<w:r>
616616
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
617617
</w:r>
618-
</w:p>';
618+
</w:p>
619+
<w:p>
620+
<w:r>
621+
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
622+
</w:r>
623+
</w:p>
624+
<w:p>
625+
<w:r>
626+
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
627+
</w:r>
628+
</w:p>
629+
';
619630

620631
$templateProcessor = new TestableTemplateProcesor($mainPart);
621632
$templateProcessor->setValues(['firstname' => 'John', 'lastname' => 'Doe']);
622-
623633
self::assertStringContainsString('Hello John Doe', $templateProcessor->getMainPart());
634+
self::assertStringNotContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
635+
636+
// test with a specific limit that is lower than the number of replacements
637+
$templateProcessor = new TestableTemplateProcesor($mainPart);
638+
$templateProcessor->setValues(['firstname' => 'Jane', 'lastname' => 'Smith'], 2);
639+
$variablesCounts = $templateProcessor->getVariableCount();
640+
641+
self::assertStringContainsString('Hello Jane Smith', $templateProcessor->getMainPart());
642+
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
643+
self::assertEquals(1, $variablesCounts['firstname']);
644+
self::assertEquals(1, $variablesCounts['lastname']);
645+
646+
// test with a limit for only one replacement
647+
$templateProcessor = new TestableTemplateProcesor($mainPart);
648+
$templateProcessor->setValues(['firstname' => 'Alice', 'lastname' => 'Wonderland'], 1);
649+
$variablesCounts = $templateProcessor->getVariableCount();
650+
651+
self::assertStringContainsString('Hello Alice Wonderland', $templateProcessor->getMainPart());
652+
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
653+
self::assertEquals(2, $variablesCounts['firstname']);
654+
self::assertEquals(2, $variablesCounts['lastname']);
655+
656+
// Test with a limit of 0 for a result with no replacements
657+
$templateProcessor = new TestableTemplateProcesor($mainPart);
658+
$templateProcessor->setValues(['firstname' => 'Test', 'lastname' => 'User'], 0);
659+
$variablesCounts = $templateProcessor->getVariableCount();
660+
661+
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
662+
self::assertStringNotContainsString('Hello Test User', $templateProcessor->getMainPart());
663+
self::assertEquals(3, $variablesCounts['firstname']);
664+
self::assertEquals(3, $variablesCounts['lastname']);
624665
}
625666

626667
/**

0 commit comments

Comments
 (0)