Skip to content

Commit

Permalink
Fix text extraction from PhpWord TextRun elements
Browse files Browse the repository at this point in the history
Previously, `$title->getText()` was assumed to always return a string, but in some cases, it returns a `PhpOffice\PhpWord\Element\TextRun`. This commit adds a check to determine if the returned value is a `TextRun` and properly extracts text from its child elements.
  • Loading branch information
chalda-pnuzig authored Feb 23, 2025
1 parent 6ca8c9f commit 170cdff
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/PhpWord/Writer/Word2007/Element/TOC.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,17 @@ private function writeTitle(XMLWriter $xmlWriter, TOCElement $element, Title $ti
$xmlWriter->startElement('w:t');

$titleText = $title->getText();
$this->writeText(is_string($titleText) ? $titleText : '');
if (get_class($titleText) === 'PhpOffice\PhpWord\Element\TextRun') {
$textRunElements = $title->getText()->getElements();
$uploadedText = '';
foreach ($textRunElements as $textRunElement) {
$uploadedText .= $textRunElement->getText();
$uploadedText .= ' ';
}
$this->writeText($uploadedText);
} else {
$this->writeText((is_string($titleText) ? $titleText : '');

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Syntax error, unexpected ';', expecting ')' on line 108

Check failure on line 108 in src/PhpWord/Writer/Word2007/Element/TOC.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.4)

Syntax error, unexpected ';', expecting ')' on line 108
}

$xmlWriter->endElement(); // w:t
$xmlWriter->endElement(); // w:r
Expand Down

0 comments on commit 170cdff

Please sign in to comment.