Skip to content

Commit 2d88ea2

Browse files
committed
RTF Reader: Split control parsers into functions
1 parent f9a1f34 commit 2d88ea2

File tree

7 files changed

+64
-33
lines changed

7 files changed

+64
-33
lines changed

samples/Sample_11_ReadWord2007.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
// Read contents
55
$name = basename(__FILE__, '.php');
6-
$source = "resources/{$name}.docx";
6+
$source = __DIR__ . "/resources/{$name}.docx";
7+
78
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
89
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);
910

samples/Sample_24_ReadODText.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
// Read contents
55
$name = basename(__FILE__, '.php');
6-
$source = "resources/{$name}.odt";
6+
$source = __DIR__ . "/resources/{$name}.odt";
7+
78
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
89
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'ODText');
910

samples/Sample_27_ReadRTF.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
// Read contents
55
$name = basename(__FILE__, '.php');
6-
$source = "results/Sample_01_SimpleText.rtf";
7-
$source = "resources/rtf.rtf";
8-
$source = "results/Sample_11_ReadWord2007.rtf";
6+
$source = __DIR__ . "/resources/{$name}.rtf";
97

108
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
119
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF');

samples/Sample_Header.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ function write($phpWord, $filename, $writers)
6363
$result .= date('H:i:s') . " Write to {$writer} format";
6464
if (!is_null($extension)) {
6565
$xmlWriter = IOFactory::createWriter($phpWord, $writer);
66-
$xmlWriter->save("{$filename}.{$extension}");
67-
rename("{$filename}.{$extension}", "results/{$filename}.{$extension}");
66+
$xmlWriter->save(__DIR__ . "/{$filename}.{$extension}");
67+
rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}");
6868
} else {
6969
$result .= ' ... NOT DONE!';
7070
}
File renamed without changes.

src/PhpWord/Reader/RTF.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class RTF extends AbstractReader implements ReaderInterface
3131
* Loads PhpWord from file
3232
*
3333
* @param string $docFile
34+
* @throws \Exception
3435
* @return \PhpOffice\PhpWord\PhpWord
3536
*/
3637
public function load($docFile)

src/PhpWord/Reader/RTF/Document.php

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
namespace PhpOffice\PhpWord\Reader\RTF;
1919

2020
use PhpOffice\PhpWord\PhpWord;
21-
use PhpOffice\PhpWord\Element\Section;
22-
use PhpOffice\PhpWord\Element\TextRun;
2321

2422
/**
2523
* RTF document reader
@@ -35,9 +33,9 @@
3533
class Document
3634
{
3735
/** @const int */
38-
const PARA = 0;
39-
const STYL = 1;
40-
const SKIP = 2;
36+
const PARA = 'readParagraph';
37+
const STYL = 'readStyle';
38+
const SKIP = 'readSkip';
4139

4240
/**
4341
* PhpWord object
@@ -247,32 +245,34 @@ private function flush($isControl = false)
247245
private function flushControl($isControl = false)
248246
{
249247
if (preg_match("/^([A-Za-z]+)(-?[0-9]*) ?$/", $this->control, $match) === 1) {
250-
list(, $control, $parameter) = $match;
251-
$this->parseControl($control, $parameter);
248+
list(, $control) = $match;
249+
$this->parseControl($control);
252250
}
253251

254252
if ($isControl === true) {
255253
$this->setControl(false);
256254
}
257255
}
258256

259-
/*
257+
/**
260258
* Flush text in queue
261259
*/
262260
private function flushText()
263261
{
264262
if ($this->text != '') {
265-
if (isset($this->flags['property'])) {
263+
if (isset($this->flags['property'])) { // Set property
266264
$this->flags['value'] = $this->text;
267-
var_dump($this->flags);
268-
} else {
265+
} else { // Set text
269266
if ($this->flags['paragraph'] === true) {
270267
$this->flags['paragraph'] = false;
271268
$this->flags['text'] = $this->text;
272269
}
273270
}
271+
272+
// Add text if it's not flagged as skipped
274273
if (!isset($this->flags['skipped'])) {
275-
$this->textrun->addText($this->text);
274+
$textrun = $this->textrun->addText($this->text);
275+
$this->flags['element'] = &$textrun;
276276
}
277277

278278
$this->text = '';
@@ -282,7 +282,7 @@ private function flushText()
282282
/**
283283
* Reset control word and first char state
284284
*
285-
* @param bool $state
285+
* @param bool $value
286286
*/
287287
private function setControl($value)
288288
{
@@ -312,7 +312,7 @@ private function pushText($char)
312312
* @param string $control
313313
* @param string $parameter
314314
*/
315-
private function parseControl($control, $parameter)
315+
private function parseControl($control)
316316
{
317317
$controls = array(
318318
'par' => array(self::PARA, 'paragraph', true),
@@ -333,19 +333,49 @@ private function parseControl($control, $parameter)
333333
);
334334

335335
if (array_key_exists($control, $controls)) {
336-
list($mode, $property, $value) = $controls[$control];
337-
switch ($mode) {
338-
case self::PARA: // Paragraph
339-
$this->textrun = $this->section->addTextRun();
340-
$this->flags[$property] = $value;
341-
break;
342-
case self::STYL: // Style
343-
$this->flags[$property] = $value;
344-
break;
345-
case self::SKIP: // Destination
346-
$this->flags['property'] = $property;
347-
$this->flags['skipped'] = true;
336+
list($function) = $controls[$control];
337+
if (method_exists($this, $function)) {
338+
$this->$function($controls[$control]);
348339
}
349340
}
350341
}
342+
343+
/**
344+
* Read paragraph
345+
*
346+
* @param array $directives
347+
*/
348+
private function readParagraph($directives)
349+
{
350+
list(, $property, $value) = $directives;
351+
$this->textrun = $this->section->addTextRun();
352+
$this->flags[$property] = $value;
353+
}
354+
355+
/**
356+
* Read style
357+
*
358+
* @param array $directives
359+
*/
360+
private function readStyle($directives)
361+
{
362+
list(, $property, $value) = $directives;
363+
$this->flags[$property] = $value;
364+
if (isset($this->flags['element'])) {
365+
$element = &$this->flags['element'];
366+
$element->getFontStyle()->setStyleValue($property, $value);
367+
}
368+
}
369+
370+
/**
371+
* Read skip
372+
*
373+
* @param array $directives
374+
*/
375+
private function readSkip($directives)
376+
{
377+
list(, $property) = $directives;
378+
$this->flags['property'] = $property;
379+
$this->flags['skipped'] = true;
380+
}
351381
}

0 commit comments

Comments
 (0)