diff --git a/samples/Sample_11_ReadWord2007.php b/samples/Sample_11_ReadWord2007.php index 09d9cab0..c0b54c7a 100644 --- a/samples/Sample_11_ReadWord2007.php +++ b/samples/Sample_11_ReadWord2007.php @@ -3,7 +3,8 @@ include_once 'Sample_Header.php'; // Read contents $name = basename(__FILE__, '.php'); -$source = "resources/{$name}.docx"; +$source = __DIR__ . "/resources/{$name}.docx"; + echo date('H:i:s'), " Reading contents from `{$source}`", EOL; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source); diff --git a/samples/Sample_24_ReadODText.php b/samples/Sample_24_ReadODText.php index bb5332e6..42df23ae 100644 --- a/samples/Sample_24_ReadODText.php +++ b/samples/Sample_24_ReadODText.php @@ -3,7 +3,8 @@ include_once 'Sample_Header.php'; // Read contents $name = basename(__FILE__, '.php'); -$source = "resources/{$name}.odt"; +$source = __DIR__ . "/resources/{$name}.odt"; + echo date('H:i:s'), " Reading contents from `{$source}`", EOL; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'ODText'); diff --git a/samples/Sample_27_ReadRTF.php b/samples/Sample_27_ReadRTF.php index a28ba40b..76ac3d48 100644 --- a/samples/Sample_27_ReadRTF.php +++ b/samples/Sample_27_ReadRTF.php @@ -3,9 +3,7 @@ include_once 'Sample_Header.php'; // Read contents $name = basename(__FILE__, '.php'); -$source = "results/Sample_01_SimpleText.rtf"; -$source = "resources/rtf.rtf"; -$source = "results/Sample_11_ReadWord2007.rtf"; +$source = __DIR__ . "/resources/{$name}.rtf"; echo date('H:i:s'), " Reading contents from `{$source}`", EOL; $phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF'); diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php index f39bda16..7af23c94 100644 --- a/samples/Sample_Header.php +++ b/samples/Sample_Header.php @@ -63,8 +63,8 @@ function write($phpWord, $filename, $writers) $result .= date('H:i:s') . " Write to {$writer} format"; if (!is_null($extension)) { $xmlWriter = IOFactory::createWriter($phpWord, $writer); - $xmlWriter->save("{$filename}.{$extension}"); - rename("{$filename}.{$extension}", "results/{$filename}.{$extension}"); + $xmlWriter->save(__DIR__ . "/{$filename}.{$extension}"); + rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}"); } else { $result .= ' ... NOT DONE!'; } diff --git a/samples/resources/rtf.rtf b/samples/resources/Sample_27_ReadRTF.rtf similarity index 100% rename from samples/resources/rtf.rtf rename to samples/resources/Sample_27_ReadRTF.rtf diff --git a/src/PhpWord/Reader/RTF.php b/src/PhpWord/Reader/RTF.php index 1856116c..9d5d813b 100644 --- a/src/PhpWord/Reader/RTF.php +++ b/src/PhpWord/Reader/RTF.php @@ -31,6 +31,7 @@ class RTF extends AbstractReader implements ReaderInterface * Loads PhpWord from file * * @param string $docFile + * @throws \Exception * @return \PhpOffice\PhpWord\PhpWord */ public function load($docFile) diff --git a/src/PhpWord/Reader/RTF/Document.php b/src/PhpWord/Reader/RTF/Document.php index ccb9b595..84e9c1ed 100644 --- a/src/PhpWord/Reader/RTF/Document.php +++ b/src/PhpWord/Reader/RTF/Document.php @@ -18,8 +18,6 @@ namespace PhpOffice\PhpWord\Reader\RTF; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\Element\Section; -use PhpOffice\PhpWord\Element\TextRun; /** * RTF document reader @@ -35,9 +33,9 @@ use PhpOffice\PhpWord\Element\TextRun; class Document { /** @const int */ - const PARA = 0; - const STYL = 1; - const SKIP = 2; + const PARA = 'readParagraph'; + const STYL = 'readStyle'; + const SKIP = 'readSkip'; /** * PhpWord object @@ -247,8 +245,8 @@ class Document private function flushControl($isControl = false) { if (preg_match("/^([A-Za-z]+)(-?[0-9]*) ?$/", $this->control, $match) === 1) { - list(, $control, $parameter) = $match; - $this->parseControl($control, $parameter); + list(, $control) = $match; + $this->parseControl($control); } if ($isControl === true) { @@ -256,23 +254,25 @@ class Document } } - /* + /** * Flush text in queue */ private function flushText() { if ($this->text != '') { - if (isset($this->flags['property'])) { + if (isset($this->flags['property'])) { // Set property $this->flags['value'] = $this->text; - var_dump($this->flags); - } else { + } else { // Set text if ($this->flags['paragraph'] === true) { $this->flags['paragraph'] = false; $this->flags['text'] = $this->text; } } + + // Add text if it's not flagged as skipped if (!isset($this->flags['skipped'])) { - $this->textrun->addText($this->text); + $textrun = $this->textrun->addText($this->text); + $this->flags['element'] = &$textrun; } $this->text = ''; @@ -282,7 +282,7 @@ class Document /** * Reset control word and first char state * - * @param bool $state + * @param bool $value */ private function setControl($value) { @@ -312,7 +312,7 @@ class Document * @param string $control * @param string $parameter */ - private function parseControl($control, $parameter) + private function parseControl($control) { $controls = array( 'par' => array(self::PARA, 'paragraph', true), @@ -333,19 +333,49 @@ class Document ); if (array_key_exists($control, $controls)) { - list($mode, $property, $value) = $controls[$control]; - switch ($mode) { - case self::PARA: // Paragraph - $this->textrun = $this->section->addTextRun(); - $this->flags[$property] = $value; - break; - case self::STYL: // Style - $this->flags[$property] = $value; - break; - case self::SKIP: // Destination - $this->flags['property'] = $property; - $this->flags['skipped'] = true; + list($function) = $controls[$control]; + if (method_exists($this, $function)) { + $this->$function($controls[$control]); } } } + + /** + * Read paragraph + * + * @param array $directives + */ + private function readParagraph($directives) + { + list(, $property, $value) = $directives; + $this->textrun = $this->section->addTextRun(); + $this->flags[$property] = $value; + } + + /** + * Read style + * + * @param array $directives + */ + private function readStyle($directives) + { + list(, $property, $value) = $directives; + $this->flags[$property] = $value; + if (isset($this->flags['element'])) { + $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['skipped'] = true; + } }