Skip to content

Commit 65c155c

Browse files
committed
analyse code
1 parent 15e5ad2 commit 65c155c

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ public function setComplexBlock($search, Element\AbstractElement $complexType):
321321
/**
322322
* @param string $search
323323
* @param string $htmlContent
324+
* @param bool $fullHtml
324325
*/
325326
public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
326327
{
@@ -356,12 +357,19 @@ public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
356357
// Load the XML string into a SimpleXMLElement
357358
$xml = simplexml_load_string($documentXml);
358359
// Extract content between <w:body> tags
360+
if ($xml === false){
361+
return;
362+
}
359363
$bodyContent = $xml->xpath('//w:body/*');
360364
// Output the extracted content
361365
$documentBodyStr = '';
362-
foreach ($bodyContent as $element) {
363-
$documentBodyStr .= $element->asXML();
366+
if ($bodyContent) {
367+
foreach ($bodyContent as $element) {
368+
$documentBodyStr .= $element->asXML();
369+
}
364370
}
371+
372+
365373
//replace html content r:id vaule avoid rid conflict
366374
$rIdsElement = $xml->xpath('//*[@r:id]');
367375
$rIdValuesMap = [];
@@ -382,19 +390,27 @@ public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
382390
$this->replaceXmlBlock($search, $documentBodyStr, 'w:p');
383391

384392
$xml = simplexml_load_string($relsDocumentXml);
393+
if ($xml === false){
394+
return;
395+
}
385396
// Register the namespace
386397
$xml->registerXPathNamespace('ns', 'http://schemas.openxmlformats.org/package/2006/relationships');
387398
// Use XPath to find all Relationship nodes
388399
$RelationshipXmls = $xml->xpath('//ns:Relationship');
389400
$RelationshipStr = '';
390-
foreach ($RelationshipXmls as $relationshipXml) {
391-
$rid = (string) $relationshipXml->attributes();
392-
if (isset($rIdValuesMap[$rid])) {
393-
$tmpStr = $relationshipXml->asXML();
394-
$tmpStr = str_replace($rid, $rIdValuesMap[$rid], $tmpStr);
395-
$RelationshipStr .= $tmpStr;
401+
if ($RelationshipXmls){
402+
foreach ($RelationshipXmls as $relationshipXml) {
403+
$rid = (string) $relationshipXml->attributes();
404+
if (isset($rIdValuesMap[$rid])) {
405+
$tmpStr = $relationshipXml->asXML();
406+
if ($tmpStr!=false){
407+
$tmpStr = str_replace($rid, $rIdValuesMap[$rid], $tmpStr);
408+
$RelationshipStr .= $tmpStr;
409+
}
410+
}
396411
}
397412
}
413+
398414
//add relation to document.xml.rels
399415
if ($RelationshipStr) {
400416
$relsFileName = $this->getRelationsName($this->getMainPartName());

0 commit comments

Comments
 (0)