77 * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
88 */
99
10- namespace PhpOffice \PhpWord \Container ;
10+ namespace PhpOffice \PhpWord \Element ;
1111
1212use PhpOffice \PhpWord \Exception \InvalidImageException ;
1313use PhpOffice \PhpWord \Exception \InvalidObjectException ;
3535 *
3636 * @since 0.9.2
3737 */
38- abstract class Container extends Element
38+ abstract class AbstractElement
3939{
4040 /**
4141 * Container type section|header|footer|cell|textrun|footnote
@@ -51,6 +51,28 @@ abstract class Container extends Element
5151 */
5252 protected $ sectionId ;
5353
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+
5476 /**
5577 * Elements collection
5678 *
@@ -340,6 +362,38 @@ public function getSectionId()
340362 return $ this ->sectionId ;
341363 }
342364
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+
343397 /**
344398 * Get all elements
345399 *
@@ -373,40 +427,37 @@ public function setRelationId($rId)
373427 }
374428
375429 /**
376- * Add memory image element
430+ * Check if element is located in section doc part (as opposed to header/footer)
377431 *
378- * @param string $src
379- * @param mixed $style
380- * @deprecated 0.9.0
381- * @codeCoverageIgnore
432+ * @return boolean
382433 */
383- public function addMemoryImage ( $ src , $ style = null )
434+ public function isInSection ( )
384435 {
385- return $ this ->addImage ( $ src , $ style );
436+ return ( $ this ->docPart == ' section ' );
386437 }
387438
388439 /**
389- * Create textrun element
440+ * Set style value
390441 *
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
394445 */
395- public function createTextRun ( $ paragraphStyle = null )
446+ protected function setStyle ( $ styleObject , $ styleValue = null , $ returnObject = false )
396447 {
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+ }
399459
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 ;
410461 }
411462
412463 /**
@@ -417,13 +468,14 @@ public function createFootnote($paragraphStyle = null)
417468 */
418469 private function checkValidity ($ method )
419470 {
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 ' );
421473 $ 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 ,
427479 'textrun ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
428480 'listitem ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
429481 'checkbox ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
@@ -442,10 +494,8 @@ private function checkValidity($method)
442494
443495 // Check if a method is valid for current container
444496 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 ();
449499 }
450500 }
451501 // Check if a method is valid for current container, located in other container
@@ -475,4 +525,41 @@ private function checkElementDocPart()
475525
476526 return $ inHeaderFooter ? $ docPart . $ docPartId : $ docPart ;
477527 }
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+ }
478565}
0 commit comments