7
7
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
8
8
*/
9
9
10
- namespace PhpOffice \PhpWord \Container ;
10
+ namespace PhpOffice \PhpWord \Element ;
11
11
12
12
use PhpOffice \PhpWord \Exception \InvalidImageException ;
13
13
use PhpOffice \PhpWord \Exception \InvalidObjectException ;
35
35
*
36
36
* @since 0.9.2
37
37
*/
38
- abstract class Container extends Element
38
+ abstract class AbstractElement
39
39
{
40
40
/**
41
41
* Container type section|header|footer|cell|textrun|footnote
@@ -51,6 +51,28 @@ abstract class Container extends Element
51
51
*/
52
52
protected $ sectionId ;
53
53
54
+ /**
55
+ * Document part type: section|header|footer
56
+ *
57
+ * Used by textrun and cell container to determine where the element is
58
+ * located because it will affect the availability of other element,
59
+ * e.g. footnote will not be available when $docPart is header or footer.
60
+ *
61
+ * @var string
62
+ */
63
+ private $ docPart = 'section ' ;
64
+
65
+ /**
66
+ * Document part Id
67
+ *
68
+ * For header and footer, this will be = ($sectionId - 1) * 3 + $index
69
+ * because the max number of header/footer in every page is 3, i.e.
70
+ * AUTO, FIRST, and EVEN (AUTO = ODD)
71
+ *
72
+ * @var integer
73
+ */
74
+ private $ docPartId = 1 ;
75
+
54
76
/**
55
77
* Elements collection
56
78
*
@@ -340,6 +362,38 @@ public function getSectionId()
340
362
return $ this ->sectionId ;
341
363
}
342
364
365
+ /**
366
+ * Set doc part
367
+ *
368
+ * @param string $docPart
369
+ * @param integer $docPartId
370
+ */
371
+ public function setDocPart ($ docPart , $ docPartId = 1 )
372
+ {
373
+ $ this ->docPart = $ docPart ;
374
+ $ this ->docPartId = $ docPartId ;
375
+ }
376
+
377
+ /**
378
+ * Get doc part
379
+ *
380
+ * @return string
381
+ */
382
+ public function getDocPart ()
383
+ {
384
+ return $ this ->docPart ;
385
+ }
386
+
387
+ /**
388
+ * Get doc part Id
389
+ *
390
+ * @return integer
391
+ */
392
+ public function getDocPartId ()
393
+ {
394
+ return $ this ->docPartId ;
395
+ }
396
+
343
397
/**
344
398
* Get all elements
345
399
*
@@ -373,40 +427,37 @@ public function setRelationId($rId)
373
427
}
374
428
375
429
/**
376
- * Add memory image element
430
+ * Check if element is located in section doc part (as opposed to header/footer)
377
431
*
378
- * @param string $src
379
- * @param mixed $style
380
- * @deprecated 0.9.0
381
- * @codeCoverageIgnore
432
+ * @return boolean
382
433
*/
383
- public function addMemoryImage ( $ src , $ style = null )
434
+ public function isInSection ( )
384
435
{
385
- return $ this ->addImage ( $ src , $ style );
436
+ return ( $ this ->docPart == ' section ' );
386
437
}
387
438
388
439
/**
389
- * Create textrun element
440
+ * Set style value
390
441
*
391
- * @param mixed $paragraphStyle
392
- * @deprecated 0.9.2
393
- * @codeCoverageIgnore
442
+ * @param mixed $styleObject Style object
443
+ * @param mixed $styleValue Style value
444
+ * @param boolean $returnObject Always return object
394
445
*/
395
- public function createTextRun ( $ paragraphStyle = null )
446
+ protected function setStyle ( $ styleObject , $ styleValue = null , $ returnObject = false )
396
447
{
397
- return $ this ->addTextRun ($ paragraphStyle );
398
- }
448
+ if (!is_null ($ styleValue ) && is_array ($ styleValue )) {
449
+ foreach ($ styleValue as $ key => $ value ) {
450
+ if (substr ($ key , 0 , 1 ) == '_ ' ) {
451
+ $ key = substr ($ key , 1 );
452
+ }
453
+ $ styleObject ->setStyleValue ($ key , $ value );
454
+ }
455
+ $ style = $ styleObject ;
456
+ } else {
457
+ $ style = $ returnObject ? $ styleObject : $ styleValue ;
458
+ }
399
459
400
- /**
401
- * Create footnote element
402
- *
403
- * @param mixed $paragraphStyle
404
- * @deprecated 0.9.2
405
- * @codeCoverageIgnore
406
- */
407
- public function createFootnote ($ paragraphStyle = null )
408
- {
409
- return $ this ->addFootnote ($ paragraphStyle );
460
+ return $ style ;
410
461
}
411
462
412
463
/**
@@ -417,13 +468,14 @@ public function createFootnote($paragraphStyle = null)
417
468
*/
418
469
private function checkValidity ($ method )
419
470
{
420
- // Empty array means the element can be accepted by all containers
471
+ // Valid containers for each element
472
+ $ allContainers = array ('section ' , 'header ' , 'footer ' , 'cell ' , 'textrun ' , 'footnote ' );
421
473
$ validContainers = array (
422
- 'text ' => array () ,
423
- 'link ' => array () ,
424
- 'textbreak ' => array () ,
425
- 'image ' => array () ,
426
- 'object ' => array () ,
474
+ 'text ' => $ allContainers ,
475
+ 'link ' => $ allContainers ,
476
+ 'textbreak ' => $ allContainers ,
477
+ 'image ' => $ allContainers ,
478
+ 'object ' => $ allContainers ,
427
479
'textrun ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
428
480
'listitem ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
429
481
'checkbox ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
@@ -442,10 +494,8 @@ private function checkValidity($method)
442
494
443
495
// Check if a method is valid for current container
444
496
if (array_key_exists ($ method , $ validContainers )) {
445
- if (!empty ($ validContainers [$ method ])) {
446
- if (!in_array ($ this ->container , $ validContainers [$ method ])) {
447
- throw new \BadMethodCallException ();
448
- }
497
+ if (!in_array ($ this ->container , $ validContainers [$ method ])) {
498
+ throw new \BadMethodCallException ();
449
499
}
450
500
}
451
501
// Check if a method is valid for current container, located in other container
@@ -475,4 +525,41 @@ private function checkElementDocPart()
475
525
476
526
return $ inHeaderFooter ? $ docPart . $ docPartId : $ docPart ;
477
527
}
528
+
529
+ /**
530
+ * Add memory image element
531
+ *
532
+ * @param string $src
533
+ * @param mixed $style
534
+ * @deprecated 0.9.0
535
+ * @codeCoverageIgnore
536
+ */
537
+ public function addMemoryImage ($ src , $ style = null )
538
+ {
539
+ return $ this ->addImage ($ src , $ style );
540
+ }
541
+
542
+ /**
543
+ * Create textrun element
544
+ *
545
+ * @param mixed $paragraphStyle
546
+ * @deprecated 0.9.2
547
+ * @codeCoverageIgnore
548
+ */
549
+ public function createTextRun ($ paragraphStyle = null )
550
+ {
551
+ return $ this ->addTextRun ($ paragraphStyle );
552
+ }
553
+
554
+ /**
555
+ * Create footnote element
556
+ *
557
+ * @param mixed $paragraphStyle
558
+ * @deprecated 0.9.2
559
+ * @codeCoverageIgnore
560
+ */
561
+ public function createFootnote ($ paragraphStyle = null )
562
+ {
563
+ return $ this ->addFootnote ($ paragraphStyle );
564
+ }
478
565
}
0 commit comments