RTF Reader: Split control parsers into functions

This commit is contained in:
Ivan Lanin 2014-05-29 15:09:02 +07:00
parent f9a1f34dcb
commit 2d88ea22b4
7 changed files with 64 additions and 33 deletions

View File

@ -3,7 +3,8 @@ include_once 'Sample_Header.php';
// Read contents // Read contents
$name = basename(__FILE__, '.php'); $name = basename(__FILE__, '.php');
$source = "resources/{$name}.docx"; $source = __DIR__ . "/resources/{$name}.docx";
echo date('H:i:s'), " Reading contents from `{$source}`", EOL; echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source); $phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

View File

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

View File

@ -3,9 +3,7 @@ include_once 'Sample_Header.php';
// Read contents // Read contents
$name = basename(__FILE__, '.php'); $name = basename(__FILE__, '.php');
$source = "results/Sample_01_SimpleText.rtf"; $source = __DIR__ . "/resources/{$name}.rtf";
$source = "resources/rtf.rtf";
$source = "results/Sample_11_ReadWord2007.rtf";
echo date('H:i:s'), " Reading contents from `{$source}`", EOL; echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF'); $phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF');

View File

@ -63,8 +63,8 @@ function write($phpWord, $filename, $writers)
$result .= date('H:i:s') . " Write to {$writer} format"; $result .= date('H:i:s') . " Write to {$writer} format";
if (!is_null($extension)) { if (!is_null($extension)) {
$xmlWriter = IOFactory::createWriter($phpWord, $writer); $xmlWriter = IOFactory::createWriter($phpWord, $writer);
$xmlWriter->save("{$filename}.{$extension}"); $xmlWriter->save(__DIR__ . "/{$filename}.{$extension}");
rename("{$filename}.{$extension}", "results/{$filename}.{$extension}"); rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}");
} else { } else {
$result .= ' ... NOT DONE!'; $result .= ' ... NOT DONE!';
} }

View File

@ -31,6 +31,7 @@ class RTF extends AbstractReader implements ReaderInterface
* Loads PhpWord from file * Loads PhpWord from file
* *
* @param string $docFile * @param string $docFile
* @throws \Exception
* @return \PhpOffice\PhpWord\PhpWord * @return \PhpOffice\PhpWord\PhpWord
*/ */
public function load($docFile) public function load($docFile)

View File

@ -18,8 +18,6 @@
namespace PhpOffice\PhpWord\Reader\RTF; namespace PhpOffice\PhpWord\Reader\RTF;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Element\Section;
use PhpOffice\PhpWord\Element\TextRun;
/** /**
* RTF document reader * RTF document reader
@ -35,9 +33,9 @@ use PhpOffice\PhpWord\Element\TextRun;
class Document class Document
{ {
/** @const int */ /** @const int */
const PARA = 0; const PARA = 'readParagraph';
const STYL = 1; const STYL = 'readStyle';
const SKIP = 2; const SKIP = 'readSkip';
/** /**
* PhpWord object * PhpWord object
@ -247,8 +245,8 @@ class Document
private function flushControl($isControl = false) private function flushControl($isControl = false)
{ {
if (preg_match("/^([A-Za-z]+)(-?[0-9]*) ?$/", $this->control, $match) === 1) { if (preg_match("/^([A-Za-z]+)(-?[0-9]*) ?$/", $this->control, $match) === 1) {
list(, $control, $parameter) = $match; list(, $control) = $match;
$this->parseControl($control, $parameter); $this->parseControl($control);
} }
if ($isControl === true) { if ($isControl === true) {
@ -256,23 +254,25 @@ class Document
} }
} }
/* /**
* Flush text in queue * Flush text in queue
*/ */
private function flushText() private function flushText()
{ {
if ($this->text != '') { if ($this->text != '') {
if (isset($this->flags['property'])) { if (isset($this->flags['property'])) { // Set property
$this->flags['value'] = $this->text; $this->flags['value'] = $this->text;
var_dump($this->flags); } else { // Set text
} else {
if ($this->flags['paragraph'] === true) { if ($this->flags['paragraph'] === true) {
$this->flags['paragraph'] = false; $this->flags['paragraph'] = false;
$this->flags['text'] = $this->text; $this->flags['text'] = $this->text;
} }
} }
// Add text if it's not flagged as skipped
if (!isset($this->flags['skipped'])) { if (!isset($this->flags['skipped'])) {
$this->textrun->addText($this->text); $textrun = $this->textrun->addText($this->text);
$this->flags['element'] = &$textrun;
} }
$this->text = ''; $this->text = '';
@ -282,7 +282,7 @@ class Document
/** /**
* Reset control word and first char state * Reset control word and first char state
* *
* @param bool $state * @param bool $value
*/ */
private function setControl($value) private function setControl($value)
{ {
@ -312,7 +312,7 @@ class Document
* @param string $control * @param string $control
* @param string $parameter * @param string $parameter
*/ */
private function parseControl($control, $parameter) private function parseControl($control)
{ {
$controls = array( $controls = array(
'par' => array(self::PARA, 'paragraph', true), 'par' => array(self::PARA, 'paragraph', true),
@ -333,19 +333,49 @@ class Document
); );
if (array_key_exists($control, $controls)) { if (array_key_exists($control, $controls)) {
list($mode, $property, $value) = $controls[$control]; list($function) = $controls[$control];
switch ($mode) { if (method_exists($this, $function)) {
case self::PARA: // Paragraph $this->$function($controls[$control]);
}
}
}
/**
* Read paragraph
*
* @param array $directives
*/
private function readParagraph($directives)
{
list(, $property, $value) = $directives;
$this->textrun = $this->section->addTextRun(); $this->textrun = $this->section->addTextRun();
$this->flags[$property] = $value; $this->flags[$property] = $value;
break; }
case self::STYL: // Style
/**
* Read style
*
* @param array $directives
*/
private function readStyle($directives)
{
list(, $property, $value) = $directives;
$this->flags[$property] = $value; $this->flags[$property] = $value;
break; if (isset($this->flags['element'])) {
case self::SKIP: // Destination $element = &$this->flags['element'];
$element->getFontStyle()->setStyleValue($property, $value);
}
}
/**
* Read skip
*
* @param array $directives
*/
private function readSkip($directives)
{
list(, $property) = $directives;
$this->flags['property'] = $property; $this->flags['property'] = $property;
$this->flags['skipped'] = true; $this->flags['skipped'] = true;
} }
} }
}
}