PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Feature: add limit option to setvalues

Open moghwan opened this issue 1 year ago • 4 comments

Description

This PR adds a limit parameter to the setValues function, which itself is uses within with setValue. This will help to avoid to minimize written if the limit is needed for a bulk search&replace operation.

example:

$templateProcessor = new TemplateProcessor('sample.docx');

$firstname = 'John';
$middlename = 'Mc';
$lastname = 'Doe';

// current
$templateProcessor->setValues([
    "firstname" => $firstname,
    "middlename" => $middlename,
    "lastname" => $lastname,
]);

// assuming we want to change the only the "first" name variables:
// workaround
$templateProcessor->setValue("firstname", $firstname, 1);
$templateProcessor->setValue("middlename", $middlename, 1);
$templateProcessor->setValue("lastname", $lastname, 1);

// improvement
$templateProcessor->setValues([
    "firstname" => $firstname,
    "middlename" => $middlename,
    "lastname" => $lastname,
], 1);

Checklist:

  • [x] My CI is :green_circle:
  • [x] I have covered by unit tests my new code (check build/coverage for coverage report)
  • [ ] I have updated the documentation to describe the changes
  • [x] I have updated the changelog

moghwan avatar Aug 04 '24 15:08 moghwan

Coverage Status

coverage: 96.757%. remained the same when pulling b7d4a11aefea9adb641de17ec2661c4017486779 on moghwan:feat/add-limit-to-setvalues into 2a524e2445e27b293c9b3e0f3945f312cedac3a1 on PHPOffice:master.

coveralls avatar Aug 04 '24 15:08 coveralls

@moghwan Hi, could you add Unit Tests, please ?

https://github.com/PHPOffice/PHPWord/blob/master/tests/PhpWordTests/TemplateProcessorTest.php#L597

Progi1984 avatar Aug 06 '24 20:08 Progi1984

sure thing!

moghwan avatar Aug 26 '24 07:08 moghwan

done

moghwan avatar Aug 27 '24 12:08 moghwan

branch & doc change updated

moghwan avatar Feb 13 '25 13:02 moghwan

@moghwan Thank you for your contribution 🥳

Progi1984 avatar Feb 13 '25 19:02 Progi1984

with pleasure 😁

moghwan avatar Feb 13 '25 22:02 moghwan