This commit is contained in:
simivar 2022-11-07 22:38:19 +01:00
parent 46e61d4c18
commit f482f2600b
2 changed files with 19 additions and 30 deletions

View File

@ -761,12 +761,6 @@ class TemplateProcessor
/**
* Delete a table row in a template document.
*
* @param string $search
*
* @return void
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function deleteRow(string $search): void
{
@ -776,7 +770,7 @@ class TemplateProcessor
$tagPos = strpos($this->tempDocumentMainPart, $search);
if (!$tagPos) {
throw new Exception(sprintf("Can not delete row %s, template variable not found or variable contains markup.", $search));
throw new Exception(sprintf('Can not delete row %s, template variable not found or variable contains markup.', $search));
}
$tableStart = $this->findTableStart($tagPos);
@ -1151,21 +1145,21 @@ class TemplateProcessor
/**
* Find the start position of the nearest table before $offset.
*
* @param integer $offset
*
* @return integer
*
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
protected function findTableStart(int $offset): int
{
$rowStart = strrpos($this->tempDocumentMainPart, '<w:tbl ',
((strlen($this->tempDocumentMainPart) - $offset) * -1));
$rowStart = strrpos(
$this->tempDocumentMainPart,
'<w:tbl ',
((strlen($this->tempDocumentMainPart) - $offset) * -1)
);
if (!$rowStart) {
$rowStart = strrpos($this->tempDocumentMainPart, '<w:tbl>',
((strlen($this->tempDocumentMainPart) - $offset) * -1));
$rowStart = strrpos(
$this->tempDocumentMainPart,
'<w:tbl>',
((strlen($this->tempDocumentMainPart) - $offset) * -1)
);
}
if (!$rowStart) {
throw new Exception('Can not find the start position of the table.');
@ -1175,12 +1169,8 @@ class TemplateProcessor
}
/**
* Find the end position of the nearest table row after $offset.
*
* @param integer $offset
*
* @return integer
*/
* Find the end position of the nearest table row after $offset.
*/
protected function findTableEnd(int $offset): int
{
return strpos($this->tempDocumentMainPart, '</w:tbl>', $offset) + 7;

View File

@ -183,30 +183,29 @@ final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
}
/**
* @covers ::getVariables
* @covers ::deleteRow
* @covers ::getVariables
* @covers ::saveAs
* @test
*/
public function testDeleteRow(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/delete-row.docx');
$this->assertEquals(
array('deleteMe', 'deleteMeToo'),
self::assertEquals(
['deleteMe', 'deleteMeToo'],
$templateProcessor->getVariables()
);
$docName = 'delete-row-test-result.docx';
$templateProcessor->deleteRow('deleteMe');
$this->assertEquals(
array(),
self::assertEquals(
[],
$templateProcessor->getVariables()
);
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
unlink($docName);
$this->assertTrue($docFound);
self::assertTrue($docFound);
}
/**