Some modifications for the new `Html::addHtml` feature

This commit is contained in:
Ivan Lanin 2014-05-10 21:38:44 +07:00
parent a595e6d78c
commit 62ed725032
11 changed files with 98 additions and 86 deletions

View File

@ -5,6 +5,15 @@ before_commands:
- "composer install --prefer-source --dev" - "composer install --prefer-source --dev"
tools: tools:
php_code_sniffer:
enabled: true
config:
standard: PSR2
php_cpd: true
php_mess_detector:
enabled: true
config:
ruleset: phpmd.xml.dist
external_code_coverage: external_code_coverage:
enabled: true enabled: true
timeout: 900 timeout: 900

View File

@ -52,7 +52,7 @@ script:
## PHP Copy/Paste Detector ## PHP Copy/Paste Detector
- php phpcpd.phar src/ tests/ --verbose - php phpcpd.phar src/ tests/ --verbose
## PHP Mess Detector ## PHP Mess Detector
- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php - phpmd src/,tests/ text ./phpmd.xml.dist --exclude pclzip.lib.php
## PHPLOC ## PHPLOC
#- php phploc.phar src/ #- php phploc.phar src/
## PHPUnit ## PHPUnit

View File

@ -11,6 +11,8 @@ This release changed PHPWord license from LGPL 2.1 to LGPL 3.
- Image: Ability to define relative and absolute positioning - @basjan GH-217 - Image: Ability to define relative and absolute positioning - @basjan GH-217
- Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219 - Footer: Conform footer with header by adding firstPage, evenPage and by inheritance - @basjan @ivanlanin GH-219
- TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228 - TextBox: Ability to add textbox in section, header, and footer - @basjan @ivanlanin GH-228
- TextBox: Ability to add textbox in table - @basjan GH-231
- HTML: Ability to add elements to PHPWord object via html - @basjan GH-231
### Bugfixes ### Bugfixes

20
phpmd.xml.dist Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ruleset name="PHPWord PHP Mess Detector Rule Set"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
<rule ref="rulesets/naming.xml"/>
<rule ref="rulesets/design.xml/ExitExpression" />
<rule ref="rulesets/design.xml/EvalExpression" />
<rule ref="rulesets/design.xml/GotoStatement" />
<rule ref="rulesets/design.xml/NumberOfChildren" />
<rule ref="rulesets/design.xml/DepthOfInheritance" />
<rule ref="rulesets/design.xml/CouplingBetweenObjects">
<properties>
<property name="minimum" value="15" />
</properties>
</rule>
<rule ref="rulesets/unusedcode.xml" />
<rule ref="rulesets/controversial.xml" />
</ruleset>

View File

@ -6,9 +6,9 @@ echo date('H:i:s') , ' Create new PhpWord object' , EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection(); $section = $phpWord->addSection();
$html='<h1>Adding element via HTML</h1>'; $html = '<h1>Adding element via HTML</h1>';
$html.='<p>Some well formed HTML snippet needs to be used</p>'; $html .= '<p>Some well formed HTML snippet needs to be used</p>';
$html.='<p>With for example <strong>some <em>inline</em> formatting</strong>'; $html .= '<p>With for example <strong>some <em>inline</em> formatting</strong></p>';
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html); \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);

View File

@ -66,6 +66,31 @@ abstract class AbstractContainer extends AbstractElement
return count($this->elements); return count($this->elements);
} }
/**
* Add generic element with style
*
* This is how all elements should be added with dependency injection: with
* just one simple $style. Currently this function supports TextRun, Table,
* and TextBox since all other elements have different arguments
*
* @todo Change the function name into something better?
*
* @param string $elementName
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\AbstractElement
*/
private function addGenericElement($elementName, $style)
{
$elementClass = __NAMESPACE__ . '\\' . $elementName;
$this->checkValidity($elementName);
$element = new $elementClass($style);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);
return $element;
}
/** /**
* Add text/preservetext element * Add text/preservetext element
* *
@ -100,13 +125,7 @@ abstract class AbstractContainer extends AbstractElement
*/ */
public function addTextRun($paragraphStyle = null) public function addTextRun($paragraphStyle = null)
{ {
$this->checkValidity('TextRun'); return $this->addGenericElement('TextRun', $paragraphStyle);
$element = new TextRun($paragraphStyle);
$element->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($element);
return $element;
} }
/** /**
@ -186,6 +205,18 @@ abstract class AbstractContainer extends AbstractElement
return $element; return $element;
} }
/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Footer
*/
public function addTable($style = null)
{
return $this->addGenericElement('Table', $style);
}
/** /**
* Add image element * Add image element
* *
@ -302,13 +333,7 @@ abstract class AbstractContainer extends AbstractElement
*/ */
public function addTextBox($style = null) public function addTextBox($style = null)
{ {
$this->checkValidity('TextBox'); return $this->addGenericElement('TextBox', $style);
$textbox = new TextBox($style);
$textbox->setDocPart($this->getDocPart(), $this->getDocPartId());
$this->addElement($textbox);
return $textbox;
} }
/** /**
@ -329,6 +354,7 @@ abstract class AbstractContainer extends AbstractElement
'Object' => $allContainers, 'Object' => $allContainers,
'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'), 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'),
'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'), 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'),
'Table' => array('section', 'header', 'footer', 'textbox'),
'CheckBox' => array('section', 'header', 'footer', 'cell'), 'CheckBox' => array('section', 'header', 'footer', 'cell'),
'TextBox' => array('section', 'header', 'footer', 'cell'), 'TextBox' => array('section', 'header', 'footer', 'cell'),
'Footnote' => array('section', 'textrun', 'cell'), 'Footnote' => array('section', 'textrun', 'cell'),

View File

@ -114,19 +114,4 @@ class Footer extends AbstractContainer
{ {
return $this->type = self::EVEN; return $this->type = self::EVEN;
} }
/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Section
*/
public function addTable($style = null)
{
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($table);
return $table;
}
} }

View File

@ -117,21 +117,6 @@ class Section extends AbstractContainer
$this->addElement(new PageBreak()); $this->addElement(new PageBreak());
} }
/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Footer
*/
public function addTable($style = null)
{
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($table);
return $table;
}
/** /**
* Add a Table-of-Contents Element * Add a Table-of-Contents Element
* *

View File

@ -53,9 +53,8 @@ class Table extends AbstractElement
* @param integer $docPartId * @param integer $docPartId
* @param mixed $style * @param mixed $style
*/ */
public function __construct($docPart, $docPartId, $style = null) public function __construct($style = null)
{ {
$this->setDocPart($docPart, $docPartId);
$this->style = $this->setStyle(new TableStyle(), $style); $this->style = $this->setStyle(new TableStyle(), $style);
} }

View File

@ -53,19 +53,4 @@ class TextBox extends AbstractContainer
{ {
return $this->style; return $this->style;
} }
/**
* Add table element
*
* @param mixed $style
* @return \PhpOffice\PhpWord\Element\Table
* @todo Merge with the same function on Footer
*/
public function addTable($style = null)
{
$table = new Table($this->getDocPart(), $this->getDocPartId(), $style);
$this->addElement($table);
return $table;
}
} }

View File

@ -22,28 +22,29 @@ namespace PhpOffice\PhpWord\Shared;
*/ */
class Html class Html
{ {
/** /**
* add HTML parts * Add HTML parts
*
* Note: $stylesheet parameter is removed to avoid PHPMD error for unused parameter
*
* @param \PhpOffice\PhpWord\Element\AbstractElement $object Where the parts need to be added
* @param string $html the code to parse
* *
* @param $object where the parts need to be added
* @param $html the code to parse
*
*/ */
public static function addHtml($object, $html, $stylesheet = '') public static function addHtml($object, $html)
{ {
/* /*
* @todo parse $stylesheet for default styles. Should result in an array based on id, class and element, * @todo parse $stylesheet for default styles. Should result in an array based on id, class and element,
* which could be applied when such an element occurs in the parseNode function. * which could be applied when such an element occurs in the parseNode function.
*/ */
$html = str_replace(array("\n","\r"), '', $html); $html = str_replace(array("\n","\r"), '', $html);
$dom = new \DOMDocument(); $dom = new \DOMDocument();
$dom->preserveWhiteSpace = true; $dom->preserveWhiteSpace = true;
$dom->loadXML('<body>' . html_entity_decode($html) . '</body>'); $dom->loadXML('<body>' . html_entity_decode($html) . '</body>');
$node = $dom->getElementsByTagName('body'); $node = $dom->getElementsByTagName('body');
self::parseNode($node->item(0), $object); self::parseNode($node->item(0), $object);
} }
@ -58,7 +59,7 @@ class Html
{ {
if ($node->nodeType == XML_ELEMENT_NODE) { if ($node->nodeType == XML_ELEMENT_NODE) {
$attributes = $node->attributes; // get all the attributes(eg: id, class) $attributes = $node->attributes; // get all the attributes(eg: id, class)
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
switch ($attribute->name) { switch ($attribute->name) {
case 'style': case 'style':
@ -94,7 +95,7 @@ class Html
} }
return $style; return $style;
} }
/** /**
* parse a node and add a corresponding element to the object * parse a node and add a corresponding element to the object
* *
@ -112,8 +113,8 @@ class Html
$styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']); $styles['paragraphStyle'] = self::parseInlineStyle($node, $styles['paragraphStyle']);
$newobject = $object->addTextRun($styles['paragraphStyle']); $newobject = $object->addTextRun($styles['paragraphStyle']);
break; break;
/* /**
* @todo Think of a clever way of defining header styles, now it is only based on the assumption, that * @todo Think of a clever way of defining header styles, now it is only based on the assumption, that
* Heading1 - Heading6 are already defined somewhere * Heading1 - Heading6 are already defined somewhere
*/ */
@ -157,8 +158,8 @@ class Html
case 'sub': case 'sub':
$styles['fontStyle']['subScript'] = true; $styles['fontStyle']['subScript'] = true;
break; break;
/* /**
* @todo As soon as TableItem, RowItem and CellItem support relative width and height * @todo As soon as TableItem, RowItem and CellItem support relative width and height
*/ */
case 'table': case 'table':
@ -193,8 +194,8 @@ class Html
} }
$styles['listStyle']['listType'] = 7; // TYPE_NUMBER = 7; $styles['listStyle']['listType'] = 7; // TYPE_NUMBER = 7;
break; break;
/* /**
* @todo As soon as ListItem inherits from AbstractContainer or TextRun delete parsing part of childNodes * @todo As soon as ListItem inherits from AbstractContainer or TextRun delete parsing part of childNodes
*/ */
case 'li': case 'li':
@ -208,12 +209,12 @@ class Html
$object->addListItem($text, $data['listdepth'], $styles['fontStyle'], $styles['listStyle'], $styles['paragraphStyle']); $object->addListItem($text, $data['listdepth'], $styles['fontStyle'], $styles['listStyle'], $styles['paragraphStyle']);
} }
} }
if ($newobject === null) { if ($newobject === null) {
$newobject = $object; $newobject = $object;
} }
/* /**
* @todo As soon as ListItem inherits from AbstractContainer or TextRun delete condition * @todo As soon as ListItem inherits from AbstractContainer or TextRun delete condition
*/ */
if ($node->nodeName != 'li') { if ($node->nodeName != 'li') {