18
18
namespace PhpOffice \PhpWord \Reader \RTF ;
19
19
20
20
use PhpOffice \PhpWord \PhpWord ;
21
- use PhpOffice \PhpWord \Element \Section ;
22
- use PhpOffice \PhpWord \Element \TextRun ;
23
21
24
22
/**
25
23
* RTF document reader
35
33
class Document
36
34
{
37
35
/** @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 ' ;
41
39
42
40
/**
43
41
* PhpWord object
@@ -247,32 +245,34 @@ private function flush($isControl = false)
247
245
private function flushControl ($ isControl = false )
248
246
{
249
247
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 );
252
250
}
253
251
254
252
if ($ isControl === true ) {
255
253
$ this ->setControl (false );
256
254
}
257
255
}
258
256
259
- /*
257
+ /**
260
258
* Flush text in queue
261
259
*/
262
260
private function flushText ()
263
261
{
264
262
if ($ this ->text != '' ) {
265
- if (isset ($ this ->flags ['property ' ])) {
263
+ if (isset ($ this ->flags ['property ' ])) { // Set property
266
264
$ this ->flags ['value ' ] = $ this ->text ;
267
- var_dump ($ this ->flags );
268
- } else {
265
+ } else { // Set text
269
266
if ($ this ->flags ['paragraph ' ] === true ) {
270
267
$ this ->flags ['paragraph ' ] = false ;
271
268
$ this ->flags ['text ' ] = $ this ->text ;
272
269
}
273
270
}
271
+
272
+ // Add text if it's not flagged as skipped
274
273
if (!isset ($ this ->flags ['skipped ' ])) {
275
- $ this ->textrun ->addText ($ this ->text );
274
+ $ textrun = $ this ->textrun ->addText ($ this ->text );
275
+ $ this ->flags ['element ' ] = &$ textrun ;
276
276
}
277
277
278
278
$ this ->text = '' ;
@@ -282,7 +282,7 @@ private function flushText()
282
282
/**
283
283
* Reset control word and first char state
284
284
*
285
- * @param bool $state
285
+ * @param bool $value
286
286
*/
287
287
private function setControl ($ value )
288
288
{
@@ -312,7 +312,7 @@ private function pushText($char)
312
312
* @param string $control
313
313
* @param string $parameter
314
314
*/
315
- private function parseControl ($ control, $ parameter )
315
+ private function parseControl ($ control )
316
316
{
317
317
$ controls = array (
318
318
'par ' => array (self ::PARA , 'paragraph ' , true ),
@@ -333,19 +333,49 @@ private function parseControl($control, $parameter)
333
333
);
334
334
335
335
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 ]);
348
339
}
349
340
}
350
341
}
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
+ }
351
381
}
0 commit comments