Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1.x/1.4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

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

### Deprecations
- Deprecate `PhpOffice\PhpWord\Style\Paragraph::getIndent()` : Use `PhpOffice\PhpWord\Style\Paragraph::getIndentLeft()`
Expand Down
4 changes: 2 additions & 2 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,10 @@ public function setValue($search, $replace, $limit = self::MAXIMUM_REPLACEMENTS_
/**
* Set values from a one-dimensional array of "variable => value"-pairs.
*/
public function setValues(array $values): void
public function setValues(array $values, int $limit = self::MAXIMUM_REPLACEMENTS_DEFAULT): void
{
foreach ($values as $macro => $replace) {
$this->setValue($macro, $replace);
$this->setValue($macro, $replace, $limit);
}
}

Expand Down
45 changes: 43 additions & 2 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,12 +615,53 @@ public function testSetValues(): void
<w:r>
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
</w:r>
</w:p>';
</w:p>
<w:p>
<w:r>
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:t xml:space="preserve">Hello ${firstname} ${lastname}</w:t>
</w:r>
</w:p>
';

$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->setValues(['firstname' => 'John', 'lastname' => 'Doe']);

self::assertStringContainsString('Hello John Doe', $templateProcessor->getMainPart());
self::assertStringNotContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());

// test with a specific limit that is lower than the number of replacements
$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->setValues(['firstname' => 'Jane', 'lastname' => 'Smith'], 2);
$variablesCounts = $templateProcessor->getVariableCount();

self::assertStringContainsString('Hello Jane Smith', $templateProcessor->getMainPart());
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
self::assertEquals(1, $variablesCounts['firstname']);
self::assertEquals(1, $variablesCounts['lastname']);

// test with a limit for only one replacement
$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->setValues(['firstname' => 'Alice', 'lastname' => 'Wonderland'], 1);
$variablesCounts = $templateProcessor->getVariableCount();

self::assertStringContainsString('Hello Alice Wonderland', $templateProcessor->getMainPart());
self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
self::assertEquals(2, $variablesCounts['firstname']);
self::assertEquals(2, $variablesCounts['lastname']);

// Test with a limit of 0 for a result with no replacements
$templateProcessor = new TestableTemplateProcesor($mainPart);
$templateProcessor->setValues(['firstname' => 'Test', 'lastname' => 'User'], 0);
$variablesCounts = $templateProcessor->getVariableCount();

self::assertStringContainsString('Hello ${firstname} ${lastname}', $templateProcessor->getMainPart());
self::assertStringNotContainsString('Hello Test User', $templateProcessor->getMainPart());
self::assertEquals(3, $variablesCounts['firstname']);
self::assertEquals(3, $variablesCounts['lastname']);
}

/**
Expand Down