Skip to content

Commit

Permalink
ODText Reader: Basic ODText reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlanin committed Apr 28, 2014
1 parent 27a2307 commit a676af3
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions samples/Sample_24_ReadODText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
include_once 'Sample_Header.php';

// Read contents
$name = basename(__FILE__, '.php');
$source = "resources/{$name}.odt";
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'ODText');

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
Binary file added samples/resources/Sample_24_ReadODText.odt
Binary file not shown.
2 changes: 1 addition & 1 deletion src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
*/
public static function createReader($name = 'Word2007')
{
if (!in_array($name, array('ReaderInterface', 'Word2007'))) {
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText'))) {
throw new Exception("\"{$name}\" is not a valid reader.");
}

Expand Down
86 changes: 86 additions & 0 deletions src/PhpWord/Reader/ODText.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Reader;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\Shared\XMLReader;

/**
* Reader for ODText
*
* @since 0.10.0
*/
class ODText extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file
*
* @param string $docFile
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
$relationships = $this->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;
}
}
67 changes: 67 additions & 0 deletions src/PhpWord/Reader/ODText/AbstractPart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Reader\ODText;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;

/**
* Abstract part reader
*/
abstract class AbstractPart
{
/**
* Document file
*
* @var string
*/
protected $docFile;

/**
* XML file
*
* @var string
*/
protected $xmlFile;

/**
* Part relationships
*
* @var array
*/
protected $rels = array();

/**
* Read part
*/
abstract public function read(PhpWord &$phpWord);

/**
* Create new instance
*
* @param string $docFile
* @param string $xmlFile
*/
public function __construct($docFile, $xmlFile)
{
$this->docFile = $docFile;
$this->xmlFile = $xmlFile;
}

/**
* Set relationships
*
* @param array $value
*/
public function setRels($value)
{
$this->rels = $value;
}
}
57 changes: 57 additions & 0 deletions src/PhpWord/Reader/ODText/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Reader\ODText;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\XMLReader;

/**
* Content reader
*/
class Content extends AbstractPart
{
/**
* Read content.xml
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord &$phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->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;
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/PhpWord/Style/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function setArrayStyle(array $style = array())
/**
* Get font name
*
* @return bool
* @return string
*/
public function getName()
{
Expand Down
31 changes: 31 additions & 0 deletions tests/PhpWord/Tests/Reader/ODTextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* PHPWord
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2014 PHPWord
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/

namespace PhpOffice\PhpWord\Tests\Reader;

use PhpOffice\PhpWord\IOFactory;

/**
* Test class for PhpOffice\PhpWord\Reader\ODText
*
* @coversDefaultClass \PhpOffice\PhpWord\Reader\ODText
* @runTestsInSeparateProcesses
*/
class ODTextTest extends \PHPUnit_Framework_TestCase
{
/**
* Load
*/
public function testLoad()
{
$filename = __DIR__ . '/../_files/documents/reader.odt';
$object = IOFactory::load($filename, 'ODText');
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object);
}
}
Binary file added tests/PhpWord/Tests/_files/documents/reader.odt
Binary file not shown.

0 comments on commit a676af3

Please sign in to comment.