@@ -66,6 +66,31 @@ public function countElements()
6666 return count ($ this ->elements );
6767 }
6868
69+ /**
70+ * Add generic element with style
71+ *
72+ * This is how all elements should be added with dependency injection: with
73+ * just one simple $style. Currently this function supports TextRun, Table,
74+ * and TextBox since all other elements have different arguments
75+ *
76+ * @todo Change the function name into something better?
77+ *
78+ * @param string $elementName
79+ * @param mixed $style
80+ * @return \PhpOffice\PhpWord\Element\AbstractElement
81+ */
82+ private function addGenericElement ($ elementName , $ style )
83+ {
84+ $ elementClass = __NAMESPACE__ . '\\' . $ elementName ;
85+
86+ $ this ->checkValidity ($ elementName );
87+ $ element = new $ elementClass ($ style );
88+ $ element ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
89+ $ this ->addElement ($ element );
90+
91+ return $ element ;
92+ }
93+
6994 /**
7095 * Add text/preservetext element
7196 *
@@ -100,13 +125,7 @@ public function addText($text, $fontStyle = null, $paragraphStyle = null, $eleme
100125 */
101126 public function addTextRun ($ paragraphStyle = null )
102127 {
103- $ this ->checkValidity ('TextRun ' );
104-
105- $ element = new TextRun ($ paragraphStyle );
106- $ element ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
107- $ this ->addElement ($ element );
108-
109- return $ element ;
128+ return $ this ->addGenericElement ('TextRun ' , $ paragraphStyle );
110129 }
111130
112131 /**
@@ -186,6 +205,18 @@ public function addListItem($text, $depth = 0, $fontStyle = null, $listStyle = n
186205 return $ element ;
187206 }
188207
208+ /**
209+ * Add table element
210+ *
211+ * @param mixed $style
212+ * @return \PhpOffice\PhpWord\Element\Table
213+ * @todo Merge with the same function on Footer
214+ */
215+ public function addTable ($ style = null )
216+ {
217+ return $ this ->addGenericElement ('Table ' , $ style );
218+ }
219+
189220 /**
190221 * Add image element
191222 *
@@ -302,13 +333,7 @@ public function addCheckBox($name, $text, $fontStyle = null, $paragraphStyle = n
302333 */
303334 public function addTextBox ($ style = null )
304335 {
305- $ this ->checkValidity ('TextBox ' );
306-
307- $ textbox = new TextBox ($ style );
308- $ textbox ->setDocPart ($ this ->getDocPart (), $ this ->getDocPartId ());
309- $ this ->addElement ($ textbox );
310-
311- return $ textbox ;
336+ return $ this ->addGenericElement ('TextBox ' , $ style );
312337 }
313338
314339 /**
@@ -329,6 +354,7 @@ private function checkValidity($method)
329354 'Object ' => $ allContainers ,
330355 'TextRun ' => array ('section ' , 'header ' , 'footer ' , 'cell ' , 'textbox ' ),
331356 'ListItem ' => array ('section ' , 'header ' , 'footer ' , 'cell ' , 'textbox ' ),
357+ 'Table ' => array ('section ' , 'header ' , 'footer ' , 'textbox ' ),
332358 'CheckBox ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
333359 'TextBox ' => array ('section ' , 'header ' , 'footer ' , 'cell ' ),
334360 'Footnote ' => array ('section ' , 'textrun ' , 'cell ' ),
0 commit comments