Skip to content

Commit

Permalink
Merge 4bbbcc9 into 8b891bb
Browse files Browse the repository at this point in the history
  • Loading branch information
sbuerk committed Apr 2, 2024
2 parents 8b891bb + 4bbbcc9 commit 904970c
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/PhpWord/Reader/Word2007/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,33 @@ public function read(PhpWord $phpWord): void
$this->phpWord = $phpWord;
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$readMethods = ['w:p' => 'readWPNode', 'w:tbl' => 'readTable', 'w:sectPr' => 'readWSectPrNode'];

$nodes = $xmlReader->getElements('w:body/*');
if ($nodes->length > 0) {
$section = $this->phpWord->addSection();
foreach ($nodes as $node) {
if (isset($readMethods[$node->nodeName])) {
$readMethod = $readMethods[$node->nodeName];
$this->$readMethod($xmlReader, $node, $section);
$this->readNode($phpWord, $xmlReader, $node, $section);
}
}
}

private function readNode(PhpWord $phpWord, XMLReader $xmlReader, \DOMNode $node, Section $section): void
{
$readMethods = ['w:p' => 'readWPNode', 'w:tbl' => 'readTable', 'w:sectPr' => 'readWSectPrNode'];
if (isset($readMethods[$node->nodeName])) {
$readMethod = $readMethods[$node->nodeName];
$this->$readMethod($xmlReader, $node, $section);
} elseif ($node->nodeName === 'w:sdt') {
$nodes = $xmlReader->getElements('w:sdtContent/*', $node);
if ($nodes->length > 0) {
foreach ($nodes as $subNode) {
$this->readNode($phpWord, $xmlReader, $subNode, $section);
}
}
} elseif ($node->nodeName === 'w:sdtContent') {
$nodes = $xmlReader->getElements('*', $node);
if ($nodes->length > 0) {
foreach ($nodes as $subNode) {
$this->readNode($phpWord, $xmlReader, $subNode, $section);
}
}
}
Expand Down

0 comments on commit 904970c

Please sign in to comment.