Type check in element writers

This commit is contained in:
Ivan Lanin 2014-05-11 19:41:48 +07:00
parent c243a11e57
commit c7e4ed0c18
32 changed files with 281 additions and 25 deletions

View File

@ -32,6 +32,9 @@ class Container extends AbstractElement
public function write()
{
$container = $this->element;
if (!$this->element instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
return;
}
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
$content = '';

View File

@ -38,6 +38,10 @@ class Footnote extends AbstractElement
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) {
return;
}
$noteId = count($this->parentWriter->getNotes()) + 1;
$noteMark = $this->noteType . '-' . $this->element->getRelationId();
$content = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";

View File

@ -34,6 +34,10 @@ class Image extends Text
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
return;
}
$content = '';
if (!$this->parentWriter->isPdf()) {
$imageData = $this->getBase64ImageData($this->element);

View File

@ -31,6 +31,10 @@ class Link extends Text
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
return;
}
$content = '';
$content .= $this->writeOpening();
$content .= "<a href=\"{$this->element->getTarget()}\">{$this->element->getText()}</a>";

View File

@ -31,6 +31,10 @@ class ListItem extends AbstractElement
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) {
return;
}
$text = htmlspecialchars($this->element->getTextObject()->getText());
$content = '<p>' . $text . '</p>' . PHP_EOL;

View File

@ -31,6 +31,10 @@ class Table extends AbstractElement
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Table) {
return;
}
$content = '';
$rows = $this->element->getRows();
$rowCount = count($rows);

View File

@ -31,6 +31,10 @@ class Title extends AbstractElement
*/
public function write()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) {
return;
}
$tag = 'h' . $this->element->getDepth();
$text = htmlspecialchars($this->element->getText());
$content = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;

View File

@ -33,6 +33,9 @@ class Image extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Image) {
return;
}
$mediaIndex = $element->getMediaIndex();
$target = 'Pictures/' . $element->getTarget();

View File

@ -31,6 +31,9 @@ class Link extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Link) {
return;
}
if (!$this->withoutP) {
$xmlWriter->startElement('text:p'); // text:p

View File

@ -31,6 +31,9 @@ class Table extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Table) {
return;
}
$rows = $element->getRows();
$rowCount = count($rows);
$colCount = $element->countColumns();

View File

@ -31,6 +31,9 @@ class Text extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$fontStyle = $element->getFontStyle();
$paragraphStyle = $element->getParagraphStyle();

View File

@ -32,6 +32,9 @@ class Container extends \PhpOffice\PhpWord\Writer\HTML\Element\Container
public function write()
{
$container = $this->element;
if (!$this->element instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
return;
}
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false;
$content = '';

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer\RTF\Element;
use PhpOffice\PhpWord\Element\Text as TextElement;
use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font as FontStyle;
use PhpOffice\PhpWord\Writer\RTF\Style\Font as FontStyleWriter;
@ -34,10 +35,14 @@ class Text extends AbstractElement
*/
public function write()
{
$fontStyle = $this->getFontStyle();
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$fontStyle = $this->getFontStyle($this->element);
$content = '';
$content .= $this->writeParagraphStyle();
$content .= $this->writeParagraphStyle($this->element);
$content .= $this->writeFontStyleBegin($fontStyle);
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
$content .= ' ';
@ -57,22 +62,22 @@ class Text extends AbstractElement
*
* @return string
*/
private function writeParagraphStyle()
private function writeParagraphStyle(TextElement $element)
{
$content = '';
// Get paragraph style
$paragraphStyle = $this->element->getParagraphStyle();
$paragraphStyle = $element->getParagraphStyle();
if (is_string($paragraphStyle)) {
$paragraphStyle = Style::getStyle($paragraphStyle);
}
// Write style when applicable
if ($paragraphStyle && !$this->withoutP) {
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
if ($this->parentWriter->getLastParagraphStyle() != $element->getParagraphStyle()) {
$styleWriter = new ParagraphStyleWriter($paragraphStyle);
$content = $styleWriter->write();
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
$this->parentWriter->setLastParagraphStyle($element->getParagraphStyle());
} else {
$this->parentWriter->setLastParagraphStyle();
}
@ -139,9 +144,9 @@ class Text extends AbstractElement
*
* @return \PhpOffice\PhpWord\Style\Font
*/
private function getFontStyle()
private function getFontStyle(TextElement $element)
{
$fontStyle = $this->element->getFontStyle();
$fontStyle = $element->getFontStyle();
if (is_string($fontStyle)) {
$fontStyle = Style::getStyle($fontStyle);
}

View File

@ -33,6 +33,9 @@ class CheckBox extends Text
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\CheckBox) {
return;
}
$name = htmlspecialchars($element->getName());
$name = String::controlCharacterPHP2OOXML($name);

View File

@ -40,6 +40,9 @@ class Container extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$container = $this->getElement();
if (!$container instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
return;
}
$containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1);
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'ListItemRun')) ? true : false;

View File

@ -38,6 +38,9 @@ class Footnote extends Text
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Footnote) {
return;
}
$this->writeOpeningWP();

View File

@ -17,6 +17,8 @@
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\Image as ImageElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Writer\Word2007\Style\Image as ImageStyleWriter;
/**
@ -31,22 +33,24 @@ class Image extends AbstractElement
*/
public function write()
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Image) {
return;
}
if ($element->isWatermark()) {
$this->writeWatermark();
$this->writeWatermark($xmlWriter, $element);
} else {
$this->writeImage();
$this->writeImage($xmlWriter, $element);
}
}
/**
* Write image element
*/
private function writeImage()
private function writeImage(XMLWriter $xmlWriter, ImageElement $element)
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
$rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
$style = $element->getStyle();
@ -84,11 +88,8 @@ class Image extends AbstractElement
/**
* Write watermark element
*/
private function writeWatermark()
private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element)
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
$rId = $element->getRelationId();
$style = $element->getStyle();
$style->setPositioning('absolute');

View File

@ -31,6 +31,9 @@ class Link extends Text
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Link) {
return;
}
$rId = $element->getRelationId() + ($element->isInSection() ? 6 : 0);

View File

@ -33,6 +33,9 @@ class ListItem extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItem) {
return;
}
$textObject = $element->getTextObject();

View File

@ -33,6 +33,9 @@ class ListItemRun extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\ListItemRun) {
return;
}
$xmlWriter->startElement('w:p');

View File

@ -31,6 +31,9 @@ class Object extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Object) {
return;
}
$rIdObject = $element->getRelationId() + ($element->isInSection() ? 6 : 0);
$rIdImage = $element->getImageRelationId() + ($element->isInSection() ? 6 : 0);

View File

@ -33,6 +33,9 @@ class PreserveText extends Text
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\PreserveText) {
return;
}
$texts = $element->getText();
if (!is_array($texts)) {

View File

@ -36,6 +36,9 @@ class TOC extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\TOC) {
return;
}
$titles = $element->getTitles();
$writeFieldMark = true;

View File

@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element;
use PhpOffice\PhpWord\Element\Cell as CellElement;
use PhpOffice\PhpWord\Element\Row as RowElement;
use PhpOffice\PhpWord\Shared\XMLWriter;
use PhpOffice\PhpWord\Style\Cell as CellStyle;
use PhpOffice\PhpWord\Style\Table as TableStyle;
use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
@ -38,6 +39,9 @@ class Table extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Table) {
return;
}
$rows = $element->getRows();
$rowCount = count($rows);
@ -94,7 +98,7 @@ class Table extends AbstractElement
// Table rows
for ($i = 0; $i < $rowCount; $i++) {
$this->writeRow($rows[$i]);
$this->writeRow($xmlWriter, $rows[$i]);
}
$xmlWriter->endElement();
}
@ -103,10 +107,8 @@ class Table extends AbstractElement
/**
* Write row
*/
private function writeRow(RowElement $row)
private function writeRow(XMLWriter $xmlWriter, RowElement $row)
{
$xmlWriter = $this->getXmlWriter();
$height = $row->getHeight();
$rowStyle = $row->getStyle();
@ -132,7 +134,7 @@ class Table extends AbstractElement
$xmlWriter->endElement();
}
foreach ($row->getCells() as $cell) {
$this->writeCell($cell);
$this->writeCell($xmlWriter, $cell);
}
$xmlWriter->endElement(); // w:tr
}
@ -140,10 +142,8 @@ class Table extends AbstractElement
/**
* Write cell
*/
private function writeCell(CellElement $cell)
private function writeCell(XMLWriter $xmlWriter, CellElement $cell)
{
$xmlWriter = $this->getXmlWriter();
$cellStyle = $cell->getStyle();
$xmlWriter->startElement('w:tc');

View File

@ -35,6 +35,9 @@ class Text extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$text = htmlspecialchars($element->getText());
$text = String::controlCharacterPHP2OOXML($text);

View File

@ -33,6 +33,9 @@ class TextBox extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\TextBox) {
return;
}
$style = $element->getStyle();
$styleWriter = new TextBoxStyleWriter($xmlWriter, $style);

View File

@ -31,6 +31,9 @@ class TextBreak extends Text
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\TextBreak) {
return;
}
if (!$this->withoutP) {
$hasStyle = $element->hasStyle();

View File

@ -33,6 +33,9 @@ class Title extends AbstractElement
{
$xmlWriter = $this->getXmlWriter();
$element = $this->getElement();
if (!$element instanceof \PhpOffice\PhpWord\Element\Title) {
return;
}
$bookmarkId = $element->getBookmarkId();
$anchor = '_Toc' . ($bookmarkId + 252634154);

View File

@ -0,0 +1,41 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Writer\HTML;
use PhpOffice\PhpWord\Writer\HTML;
/**
* Test class for PhpOffice\PhpWord\Writer\HTML\Element subnamespace
*/
class ElementTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unmatched elements
*/
public function testUnmatchedElements()
{
$styles = array('Container', 'Footnote', 'Image', 'Link', 'ListItem', 'Table', 'Title');
foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $style;
$parentWriter = new HTML();
$element = new \PhpOffice\PhpWord\Element\PageBreak();
$object = new $objectClass($parentWriter, $element);
$this->assertEquals('', $object->write());
}
}
}

View File

@ -0,0 +1,42 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Writer\ODText;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Test class for PhpOffice\PhpWord\Writer\ODText\Element subnamespace
*/
class ElementTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unmatched elements
*/
public function testUnmatchedElements()
{
$styles = array('Image', 'Link', 'Table', 'Text');
foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\ODText\\Element\\' . $style;
$xmlWriter = new XMLWriter();
$element = new \PhpOffice\PhpWord\Element\PageBreak();
$object = new $objectClass($xmlWriter, $element);
$object->write();
$this->assertEquals('', $xmlWriter->getData());
}
}
}

View File

@ -0,0 +1,41 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Writer\RTF;
use PhpOffice\PhpWord\Writer\RTF;
/**
* Test class for PhpOffice\PhpWord\Writer\RTF\Element subnamespace
*/
class ElementTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unmatched elements
*/
public function testUnmatchedElements()
{
$styles = array('Container', 'Text', 'Title');
foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style;
$parentWriter = new RTF();
$element = new \PhpOffice\PhpWord\Element\PageBreak();
$object = new $objectClass($parentWriter, $element);
$this->assertEquals('', $object->write());
}
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord\Tests\Writer\Word2007;
use PhpOffice\PhpWord\Shared\XMLWriter;
/**
* Test class for PhpOffice\PhpWord\Writer\Word2007\Element subnamespace
*/
class ElementTest extends \PHPUnit_Framework_TestCase
{
/**
* Test unmatched element
*/
public function testUnmatchedElements()
{
$styles = array(
'CheckBox', 'Container', 'Footnote', 'Image', 'Link', 'ListItem', 'ListItemRun',
'Object', 'PreserveText', 'Table', 'Text', 'TextBox', 'TextBreak', 'Title', 'TOC'
);
foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\Word2007\\Element\\' . $style;
$xmlWriter = new XMLWriter();
$element = new \PhpOffice\PhpWord\Element\PageBreak();
$object = new $objectClass($xmlWriter, $element);
$object->write();
$this->assertEquals('', $xmlWriter->getData());
}
}
}