-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
258 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.