diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d44ce1d..9f4f07dd 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ This release marked heavy refactorings on internal code structure with the creat - DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin - ODT Writer: Basic image writing - @ivanlanin - ODT Writer: Link writing - @ivanlanin +- ODT Reader: Basic ODText Reader - @ivanlanin ### Bugfixes diff --git a/samples/Sample_24_ReadODText.php b/samples/Sample_24_ReadODText.php new file mode 100644 index 00000000..bb5332e6 --- /dev/null +++ b/samples/Sample_24_ReadODText.php @@ -0,0 +1,14 @@ +readRelationships($docFile); + + $readerParts = array( + 'content.xml' => 'Content', + ); + + foreach ($readerParts as $xmlFile => $partName) { + $this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile); + } + + return $phpWord; + } + + /** + * Read document part + * + * @param \PhpOffice\PhpWord\PhpWord $phpWord + * @param array $relationships + * @param string $partName + * @param string $docFile + * @param string $xmlFile + */ + private function readPart(PhpWord &$phpWord, $relationships, $partName, $docFile, $xmlFile) + { + $partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}"; + if (class_exists($partClass)) { + $part = new $partClass($docFile, $xmlFile); + $part->setRels($relationships); + $part->read($phpWord); + } + + } + + /** + * Read all relationship files + * + * @param string $docFile + * @return array + */ + private function readRelationships($docFile) + { + $rels = array(); + $xmlFile = 'META-INF/manifest.xml'; + $xmlReader = new XMLReader(); + $xmlReader->getDomFromZip($docFile, $xmlFile); + $nodes = $xmlReader->getElements('manifest:file-entry'); + foreach ($nodes as $node) { + $type = $xmlReader->getAttribute('manifest:media-type', $node); + $target = $xmlReader->getAttribute('manifest:full-path', $node); + $rels[] = array('type' => $type, 'target' => $target); + } + + return $rels; + } +} diff --git a/src/PhpWord/Reader/ODText/AbstractPart.php b/src/PhpWord/Reader/ODText/AbstractPart.php new file mode 100644 index 00000000..a0cc75de --- /dev/null +++ b/src/PhpWord/Reader/ODText/AbstractPart.php @@ -0,0 +1,67 @@ +docFile = $docFile; + $this->xmlFile = $xmlFile; + } + + /** + * Set relationships + * + * @param array $value + */ + public function setRels($value) + { + $this->rels = $value; + } +} diff --git a/src/PhpWord/Reader/ODText/Content.php b/src/PhpWord/Reader/ODText/Content.php new file mode 100644 index 00000000..c0bb49c6 --- /dev/null +++ b/src/PhpWord/Reader/ODText/Content.php @@ -0,0 +1,57 @@ +getDomFromZip($this->docFile, $this->xmlFile); + + $nodes = $xmlReader->getElements('office:body/office:text/*'); + if ($nodes->length > 0) { + $section = $phpWord->addSection(); + foreach ($nodes as $node) { + // $styleName = $xmlReader->getAttribute('text:style-name', $node); + switch ($node->nodeName) { + + case 'text:h': // Heading + $depth = $xmlReader->getAttribute('text:outline-level', $node); + $section->addTitle($node->nodeValue, $depth); + break; + + case 'text:p': // Paragraph + $section->addText($node->nodeValue); + break; + + case 'text:list': // List + $listItems = $xmlReader->getElements('text:list-item/text:p', $node); + foreach ($listItems as $listItem) { + // $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem); + $section->addListItem($listItem->nodeValue); + } + break; + } + } + } + } +} diff --git a/src/PhpWord/Style/Font.php b/src/PhpWord/Style/Font.php index 32332ace..b4edeee3 100644 --- a/src/PhpWord/Style/Font.php +++ b/src/PhpWord/Style/Font.php @@ -203,7 +203,7 @@ class Font extends AbstractStyle /** * Get font name * - * @return bool + * @return string */ public function getName() { diff --git a/tests/PhpWord/Tests/Reader/ODTextTest.php b/tests/PhpWord/Tests/Reader/ODTextTest.php new file mode 100644 index 00000000..6136b0c4 --- /dev/null +++ b/tests/PhpWord/Tests/Reader/ODTextTest.php @@ -0,0 +1,31 @@ +assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object); + } +} diff --git a/tests/PhpWord/Tests/_files/documents/reader.odt b/tests/PhpWord/Tests/_files/documents/reader.odt new file mode 100644 index 00000000..9e18e619 Binary files /dev/null and b/tests/PhpWord/Tests/_files/documents/reader.odt differ