Skip to content

Commit b4ca413

Browse files
authored
Merge pull request #1 from moghwan/feat/add-limit-to-setvalues
Feat/add limit to setvalues
2 parents 89a202e + 8d5d1aa commit b4ca413

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

docs/changes/1.x/1.3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@
4545
- Bump mpdf/mpdf from 8.2.2 to 8.2.4 by [@dependabot](https://github.com/dependabot) in [#2647](https://github.com/PHPOffice/PHPWord/pull/2647)
4646
- Bump phenx/php-svg-lib from 0.5.1 to 0.5.4 by [@dependabot](https://github.com/dependabot) in [#2649](https://github.com/PHPOffice/PHPWord/pull/2649)
4747
- Bump phpstan/phpstan-phpunit from 1.3.15 to 1.4.0 by [@dependabot](https://github.com/dependabot) in [#2648](https://github.com/PHPOffice/PHPWord/pull/2648)
48+
- 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)
4849

4950
### BC Breaks

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
@@ -614,12 +614,53 @@ public function testSetValues(): void
614614
<w:r>
615615
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
616616
</w:r>
617-
</w:p>';
617+
</w:p>
618+
<w:p>
619+
<w:r>
620+
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
621+
</w:r>
622+
</w:p>
623+
<w:p>
624+
<w:r>
625+
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
626+
</w:r>
627+
</w:p>
628+
';
618629

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

625666
/**

0 commit comments

Comments
 (0)