ODText Reader: Basic ODText reader
This commit is contained in:
parent
27a2307d39
commit
a676af30dd
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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 not shown.
|
|
@ -43,7 +43,7 @@ abstract class IOFactory
|
|||
*/
|
||||
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.");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -203,7 +203,7 @@ class Font extends AbstractStyle
|
|||
/**
|
||||
* Get font name
|
||||
*
|
||||
* @return bool
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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 not shown.
Loading…
Reference in New Issue