Skip to content

Commit a676af3

Browse files
committed
ODText Reader: Basic ODText reader
1 parent 27a2307 commit a676af3

File tree

10 files changed

+258
-2
lines changed

10 files changed

+258
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This release marked heavy refactorings on internal code structure with the creat
3838
- DOCX Writer: Create `word/settings.xml` and `word/webSettings.xml` dynamically - @ivanlanin
3939
- ODT Writer: Basic image writing - @ivanlanin
4040
- ODT Writer: Link writing - @ivanlanin
41+
- ODT Reader: Basic ODText Reader - @ivanlanin
4142

4243
### Bugfixes
4344

samples/Sample_24_ReadODText.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// Read contents
5+
$name = basename(__FILE__, '.php');
6+
$source = "resources/{$name}.odt";
7+
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
8+
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'ODText');
9+
10+
// Save file
11+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
12+
if (!CLI) {
13+
include_once 'Sample_Footer.php';
14+
}
11.5 KB
Binary file not shown.

src/PhpWord/IOFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
4343
*/
4444
public static function createReader($name = 'Word2007')
4545
{
46-
if (!in_array($name, array('ReaderInterface', 'Word2007'))) {
46+
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText'))) {
4747
throw new Exception("\"{$name}\" is not a valid reader.");
4848
}
4949

src/PhpWord/Reader/ODText.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Reader;
11+
12+
use PhpOffice\PhpWord\PhpWord;
13+
use PhpOffice\PhpWord\Settings;
14+
use PhpOffice\PhpWord\Shared\XMLReader;
15+
16+
/**
17+
* Reader for ODText
18+
*
19+
* @since 0.10.0
20+
*/
21+
class ODText extends AbstractReader implements ReaderInterface
22+
{
23+
/**
24+
* Loads PhpWord from file
25+
*
26+
* @param string $docFile
27+
* @return \PhpOffice\PhpWord\PhpWord
28+
*/
29+
public function load($docFile)
30+
{
31+
$phpWord = new PhpWord();
32+
$relationships = $this->readRelationships($docFile);
33+
34+
$readerParts = array(
35+
'content.xml' => 'Content',
36+
);
37+
38+
foreach ($readerParts as $xmlFile => $partName) {
39+
$this->readPart($phpWord, $relationships, $partName, $docFile, $xmlFile);
40+
}
41+
42+
return $phpWord;
43+
}
44+
45+
/**
46+
* Read document part
47+
*
48+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
49+
* @param array $relationships
50+
* @param string $partName
51+
* @param string $docFile
52+
* @param string $xmlFile
53+
*/
54+
private function readPart(PhpWord &$phpWord, $relationships, $partName, $docFile, $xmlFile)
55+
{
56+
$partClass = "PhpOffice\\PhpWord\\Reader\\ODText\\{$partName}";
57+
if (class_exists($partClass)) {
58+
$part = new $partClass($docFile, $xmlFile);
59+
$part->setRels($relationships);
60+
$part->read($phpWord);
61+
}
62+
63+
}
64+
65+
/**
66+
* Read all relationship files
67+
*
68+
* @param string $docFile
69+
* @return array
70+
*/
71+
private function readRelationships($docFile)
72+
{
73+
$rels = array();
74+
$xmlFile = 'META-INF/manifest.xml';
75+
$xmlReader = new XMLReader();
76+
$xmlReader->getDomFromZip($docFile, $xmlFile);
77+
$nodes = $xmlReader->getElements('manifest:file-entry');
78+
foreach ($nodes as $node) {
79+
$type = $xmlReader->getAttribute('manifest:media-type', $node);
80+
$target = $xmlReader->getAttribute('manifest:full-path', $node);
81+
$rels[] = array('type' => $type, 'target' => $target);
82+
}
83+
84+
return $rels;
85+
}
86+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Reader\ODText;
11+
12+
use PhpOffice\PhpWord\PhpWord;
13+
use PhpOffice\PhpWord\Shared\XMLReader;
14+
15+
/**
16+
* Abstract part reader
17+
*/
18+
abstract class AbstractPart
19+
{
20+
/**
21+
* Document file
22+
*
23+
* @var string
24+
*/
25+
protected $docFile;
26+
27+
/**
28+
* XML file
29+
*
30+
* @var string
31+
*/
32+
protected $xmlFile;
33+
34+
/**
35+
* Part relationships
36+
*
37+
* @var array
38+
*/
39+
protected $rels = array();
40+
41+
/**
42+
* Read part
43+
*/
44+
abstract public function read(PhpWord &$phpWord);
45+
46+
/**
47+
* Create new instance
48+
*
49+
* @param string $docFile
50+
* @param string $xmlFile
51+
*/
52+
public function __construct($docFile, $xmlFile)
53+
{
54+
$this->docFile = $docFile;
55+
$this->xmlFile = $xmlFile;
56+
}
57+
58+
/**
59+
* Set relationships
60+
*
61+
* @param array $value
62+
*/
63+
public function setRels($value)
64+
{
65+
$this->rels = $value;
66+
}
67+
}

src/PhpWord/Reader/ODText/Content.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Reader\ODText;
11+
12+
use PhpOffice\PhpWord\PhpWord;
13+
use PhpOffice\PhpWord\Shared\XMLReader;
14+
15+
/**
16+
* Content reader
17+
*/
18+
class Content extends AbstractPart
19+
{
20+
/**
21+
* Read content.xml
22+
*
23+
* @param \PhpOffice\PhpWord\PhpWord $phpWord
24+
*/
25+
public function read(PhpWord &$phpWord)
26+
{
27+
$xmlReader = new XMLReader();
28+
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
29+
30+
$nodes = $xmlReader->getElements('office:body/office:text/*');
31+
if ($nodes->length > 0) {
32+
$section = $phpWord->addSection();
33+
foreach ($nodes as $node) {
34+
// $styleName = $xmlReader->getAttribute('text:style-name', $node);
35+
switch ($node->nodeName) {
36+
37+
case 'text:h': // Heading
38+
$depth = $xmlReader->getAttribute('text:outline-level', $node);
39+
$section->addTitle($node->nodeValue, $depth);
40+
break;
41+
42+
case 'text:p': // Paragraph
43+
$section->addText($node->nodeValue);
44+
break;
45+
46+
case 'text:list': // List
47+
$listItems = $xmlReader->getElements('text:list-item/text:p', $node);
48+
foreach ($listItems as $listItem) {
49+
// $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
50+
$section->addListItem($listItem->nodeValue);
51+
}
52+
break;
53+
}
54+
}
55+
}
56+
}
57+
}

src/PhpWord/Style/Font.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function setArrayStyle(array $style = array())
203203
/**
204204
* Get font name
205205
*
206-
* @return bool
206+
* @return string
207207
*/
208208
public function getName()
209209
{
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* @link https://github.com/PHPOffice/PHPWord
6+
* @copyright 2014 PHPWord
7+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8+
*/
9+
10+
namespace PhpOffice\PhpWord\Tests\Reader;
11+
12+
use PhpOffice\PhpWord\IOFactory;
13+
14+
/**
15+
* Test class for PhpOffice\PhpWord\Reader\ODText
16+
*
17+
* @coversDefaultClass \PhpOffice\PhpWord\Reader\ODText
18+
* @runTestsInSeparateProcesses
19+
*/
20+
class ODTextTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* Load
24+
*/
25+
public function testLoad()
26+
{
27+
$filename = __DIR__ . '/../_files/documents/reader.odt';
28+
$object = IOFactory::load($filename, 'ODText');
29+
$this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $object);
30+
}
31+
}
11.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)