Skip to content

Commit 15e5ad2

Browse files
committed
fix format
1 parent 7e90a79 commit 15e5ad2

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PhpOffice\PhpWord\Shared\XMLWriter;
3030
use PhpOffice\PhpWord\Shared\ZipArchive;
3131
use PhpOffice\PhpWord\Writer\Word2007;
32+
use ReflectionClass;
3233
use Throwable;
3334
use XSLTProcessor;
3435

@@ -321,60 +322,60 @@ public function setComplexBlock($search, Element\AbstractElement $complexType):
321322
* @param string $search
322323
* @param string $htmlContent
323324
*/
324-
public function setHtmlBlock($search,$htmlContent,$fullHtml=false): void
325+
public function setHtmlBlock($search, $htmlContent, $fullHtml = false): void
325326
{
326327
$phpWord = new PhpWord();
327328
$section = $phpWord->addSection();
328-
Html::addHtml($section,$htmlContent,$fullHtml);
329+
Html::addHtml($section, $htmlContent, $fullHtml);
329330
$zip = $this->zip();
330331
$obj = new Word2007($phpWord);
331-
$refClass = new \ReflectionClass(Word2007::class);
332+
$refClass = new ReflectionClass(Word2007::class);
332333
$addFilesToPackage = $refClass->getMethod('addFilesToPackage');
333334
$addFilesToPackage->setAccessible(true);
334335
$sectionMedia = Media::getElements('section');
335336
//add image to zip
336337
if (!empty($sectionMedia)) {
337338
//insert image to zip
338-
$res = $addFilesToPackage->invoke($obj,$zip, $sectionMedia);
339+
$res = $addFilesToPackage->invoke($obj, $zip, $sectionMedia);
339340
$registerContentTypes = $refClass->getMethod('registerContentTypes');
340341
$registerContentTypes->setAccessible(true);
341-
$registerContentTypes->invoke($obj,$sectionMedia);
342+
$registerContentTypes->invoke($obj, $sectionMedia);
342343

343344
$relationships = $refClass->getProperty('relationships');
344345
$relationships->setAccessible(true);
345346
$tmpRelationships = [];
346347
foreach ($sectionMedia as $element) {
347348
$tmpRelationships[] = $element;
348349
}
349-
$relationships->setValue($obj,$tmpRelationships);
350+
$relationships->setValue($obj, $tmpRelationships);
350351
}
351-
$documentWriterPart = $obj->getWriterPart("Document");
352-
$relsDocumentWriterPart = $obj->getWriterPart("RelsDocument");
352+
$documentWriterPart = $obj->getWriterPart('Document');
353+
$relsDocumentWriterPart = $obj->getWriterPart('RelsDocument');
353354
$documentXml = $documentWriterPart->write();
354355
$relsDocumentXml = $relsDocumentWriterPart->write();
355356
// Load the XML string into a SimpleXMLElement
356357
$xml = simplexml_load_string($documentXml);
357358
// Extract content between <w:body> tags
358359
$bodyContent = $xml->xpath('//w:body/*');
359360
// Output the extracted content
360-
$documentBodyStr = "";
361+
$documentBodyStr = '';
361362
foreach ($bodyContent as $element) {
362363
$documentBodyStr .= $element->asXML();
363364
}
364365
//replace html content r:id vaule avoid rid conflict
365-
$rIdsElement = $xml->xpath('//*[@r:id]');
366+
$rIdsElement = $xml->xpath('//*[@r:id]');
366367
$rIdValuesMap = [];
367-
if ($rIdsElement){
368-
foreach ($rIdsElement as $idEle){
369-
$rid = (string)$idEle->attributes('r', true)->id;
368+
if ($rIdsElement) {
369+
foreach ($rIdsElement as $idEle) {
370+
$rid = (string) $idEle->attributes('r', true)->id;
370371
$rIdValuesMap[$rid] = $rid;
371372
}
372373
}
373-
if (!empty($rIdValuesMap )){
374-
foreach ($rIdValuesMap as $rid => $value){
375-
$replactVulue = $rid."-1";
374+
if (!empty($rIdValuesMap)) {
375+
foreach ($rIdValuesMap as $rid => $value) {
376+
$replactVulue = $rid . '-1';
376377
$rIdValuesMap[$rid] = $replactVulue;
377-
$documentBodyStr = str_replace($rid,$replactVulue,$documentBodyStr);
378+
$documentBodyStr = str_replace($rid, $replactVulue, $documentBodyStr);
378379
}
379380
}
380381
//replace document.xml
@@ -385,28 +386,26 @@ public function setHtmlBlock($search,$htmlContent,$fullHtml=false): void
385386
$xml->registerXPathNamespace('ns', 'http://schemas.openxmlformats.org/package/2006/relationships');
386387
// Use XPath to find all Relationship nodes
387388
$RelationshipXmls = $xml->xpath('//ns:Relationship');
388-
$RelationshipStr = "";
389-
foreach ($RelationshipXmls as $relationshipXml){
390-
$rid = (string)$relationshipXml->attributes();
391-
if (isset($rIdValuesMap[$rid])){
389+
$RelationshipStr = '';
390+
foreach ($RelationshipXmls as $relationshipXml) {
391+
$rid = (string) $relationshipXml->attributes();
392+
if (isset($rIdValuesMap[$rid])) {
392393
$tmpStr = $relationshipXml->asXML();
393-
$tmpStr = str_replace($rid,$rIdValuesMap[$rid],$tmpStr);
394+
$tmpStr = str_replace($rid, $rIdValuesMap[$rid], $tmpStr);
394395
$RelationshipStr .= $tmpStr;
395396
}
396397
}
397398
//add relation to document.xml.rels
398-
if ($RelationshipStr){
399+
if ($RelationshipStr) {
399400
$relsFileName = $this->getRelationsName($this->getMainPartName());
400401
$content = $this->tempDocumentRelations[$this->getMainPartName()];
401-
$endStr = "</Relationships>";
402-
$replaceValue = $RelationshipStr.$endStr;
403-
$content = str_replace($endStr,$replaceValue,$content);
404-
$this->tempDocumentRelations[$this->getMainPartName()] = $content ;
402+
$endStr = '</Relationships>';
403+
$replaceValue = $RelationshipStr . $endStr;
404+
$content = str_replace($endStr, $replaceValue, $content);
405+
$this->tempDocumentRelations[$this->getMainPartName()] = $content;
405406
}
406-
407407
}
408408

409-
410409
/**
411410
* @param mixed $search
412411
* @param mixed $replace

tests/PhpWordTests/TemplateProcessorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,4 +1630,19 @@ public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
16301630
$templateProcessor->setUpdateFields(false);
16311631
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
16321632
}
1633+
1634+
public function testSetHtml(): void
1635+
{
1636+
Settings::setOutputEscapingEnabled(true);
1637+
$content = '<p><img src="https://t7.baidu.com/it/u=4198287529,2774471735&fm=193&f=GIF" /></p>
1638+
<p><img src="https://t7.baidu.com/it/u=4069854689,43753836&fm=193&f=GIF" /></p>
1639+
<p>HPJ LDAP(Lightweight Directory Access Protocol),轻量级目录访问协议,是一种在线目录访问协议,主要用于目录中资源的搜索和查询。如果在用户可控制的输入中没有对 LDAP 语法进行除去或引用,那么生成的 LDAP 查询可能会导致</p>';
1640+
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/template_to_html.docx');
1641+
$templateProcessor->setHtmlBlock('html_content', $content);
1642+
$docName = 'html-to-template-test.docx';
1643+
$templateProcessor->saveAs($docName);
1644+
$docFound = file_exists($docName);
1645+
unlink($docName);
1646+
self::assertTrue($docFound);
1647+
}
16331648
}
Binary file not shown.

0 commit comments

Comments
 (0)