Skip to content

Commit c82f0ac

Browse files
committed
Merge branch 'PHPOffice#231-basjan' into develop
2 parents 530f8ee + 62ed725 commit c82f0ac

File tree

10 files changed

+321
-47
lines changed

10 files changed

+321
-47
lines changed

.scrutinizer.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ before_commands:
55
- "composer install --prefer-source --dev"
66

77
tools:
8+
php_code_sniffer:
9+
enabled: true
10+
config:
11+
standard: PSR2
12+
php_cpd: true
13+
php_mess_detector:
14+
enabled: true
15+
config:
16+
ruleset: phpmd.xml.dist
817
external_code_coverage:
918
enabled: true
1019
timeout: 900

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ script:
5252
## PHP Copy/Paste Detector
5353
- php phpcpd.phar src/ tests/ --verbose
5454
## PHP Mess Detector
55-
- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php
55+
- phpmd src/,tests/ text ./phpmd.xml.dist --exclude pclzip.lib.php
5656
## PHPLOC
5757
#- php phploc.phar src/
5858
## PHPUnit

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
1111
- Image: Ability to define relative and absolute positioning - @basjan GH-217
1212
- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219
1313
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
14+
- TextBox: Ability to add textbox in table - @basjan GH-231
15+
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
1416

1517
### Bugfixes
1618

phpmd.xml.dist

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<ruleset name="PHPWord PHP Mess Detector Rule Set"
3+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
7+
<rule ref="rulesets/naming.xml"/>
8+
<rule ref="rulesets/design.xml/ExitExpression" />
9+
<rule ref="rulesets/design.xml/EvalExpression" />
10+
<rule ref="rulesets/design.xml/GotoStatement" />
11+
<rule ref="rulesets/design.xml/NumberOfChildren" />
12+
<rule ref="rulesets/design.xml/DepthOfInheritance" />
13+
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
14+
<properties>
15+
<property name="minimum" value="15" />
16+
</properties>
17+
</rule>
18+
<rule ref="rulesets/unusedcode.xml" />
19+
<rule ref="rulesets/controversial.xml" />
20+
</ruleset>

samples/Sample_26_Html.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// New Word Document
5+
echo date('H:i:s') , ' Create new PhpWord object' , EOL;
6+
$phpWord = new \PhpOffice\PhpWord\PhpWord();
7+
8+
$section = $phpWord->addSection();
9+
$html = '<h1>Adding element via HTML</h1>';
10+
$html .= '<p>Some well formed HTML snippet needs to be used</p>';
11+
$html .= '<p>With for example <strong>some <em>inline</em> formatting</strong></p>';
12+
13+
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
14+
15+
// Save file
16+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
17+
if (!CLI) {
18+
include_once 'Sample_Footer.php';
19+
}

src/PhpWord/Element/AbstractContainer.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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'),

src/PhpWord/Element/Footer.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,4 @@ public function evenPage()
114114
{
115115
return $this->type = self::EVEN;
116116
}
117-
118-
/**
119-
* Add table element
120-
*
121-
* @param mixed $style
122-
* @return \PhpOffice\PhpWord\Element\Table
123-
* @todo Merge with the same function on Section
124-
*/
125-
public function addTable($style = null)
126-
{
127-
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
128-
$this->addElement($table);
129-
130-
return $table;
131-
}
132117
}

src/PhpWord/Element/Section.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,6 @@ public function addPageBreak()
117117
$this->addElement(new PageBreak());
118118
}
119119

120-
/**
121-
* Add table element
122-
*
123-
* @param mixed $style
124-
* @return \PhpOffice\PhpWord\Element\Table
125-
* @todo Merge with the same function on Footer
126-
*/
127-
public function addTable($style = null)
128-
{
129-
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
130-
$this->addElement($table);
131-
132-
return $table;
133-
}
134-
135120
/**
136121
* Add a Table-of-Contents Element
137122
*

src/PhpWord/Element/Table.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ class Table extends AbstractElement
5353
* @param integer $docPartId
5454
* @param mixed $style
5555
*/
56-
public function __construct($docPart, $docPartId, $style = null)
56+
public function __construct($style = null)
5757
{
58-
$this->setDocPart($docPart, $docPartId);
5958
$this->style = $this->setStyle(new TableStyle(), $style);
6059
}
6160

0 commit comments

Comments
 (0)