Type checking

This commit is contained in:
Ivan Lanin 2014-05-06 23:37:14 +07:00
parent c4e8fdac84
commit 0c1e47d7a7
29 changed files with 124 additions and 7 deletions

View File

@ -38,6 +38,10 @@ class Footnote extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) {
return;
}
$noteId = count($this->parentWriter->getNotes()) + 1; $noteId = count($this->parentWriter->getNotes()) + 1;
$noteMark = $this->noteType . '-' . $this->element->getRelationId(); $noteMark = $this->noteType . '-' . $this->element->getRelationId();
$this->parentWriter->addNote($noteId, $noteMark); $this->parentWriter->addNote($noteId, $noteMark);

View File

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

View File

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

View File

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

View File

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

View File

@ -36,6 +36,10 @@ class Text extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$html = ''; $html = '';
// Paragraph style // Paragraph style
$paragraphStyle = $this->element->getParagraphStyle(); $paragraphStyle = $this->element->getParagraphStyle();
@ -44,6 +48,8 @@ class Text extends Element
$styleWriter = new ParagraphStyleWriter($paragraphStyle); $styleWriter = new ParagraphStyleWriter($paragraphStyle);
$paragraphStyle = $styleWriter->write(); $paragraphStyle = $styleWriter->write();
} }
$hasParagraphStyle = $paragraphStyle && !$this->withoutP;
// Font style // Font style
$fontStyle = $this->element->getFontStyle(); $fontStyle = $this->element->getFontStyle();
$fontStyleIsObject = ($fontStyle instanceof Font); $fontStyleIsObject = ($fontStyle instanceof Font);
@ -52,7 +58,7 @@ class Text extends Element
$fontStyle = $styleWriter->write(); $fontStyle = $styleWriter->write();
} }
if ($paragraphStyle && !$this->withoutP) { if ($hasParagraphStyle) {
$attribute = $pStyleIsObject ? 'style' : 'class'; $attribute = $pStyleIsObject ? 'style' : 'class';
$html .= "<p {$attribute}=\"{$paragraphStyle}\">"; $html .= "<p {$attribute}=\"{$paragraphStyle}\">";
} }
@ -64,7 +70,7 @@ class Text extends Element
if ($fontStyle) { if ($fontStyle) {
$html .= '</span>'; $html .= '</span>';
} }
if ($paragraphStyle && !$this->withoutP) { if ($hasParagraphStyle) {
$html .= '</p>' . PHP_EOL; $html .= '</p>' . PHP_EOL;
} }

View File

@ -34,6 +34,10 @@ class TextRun extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
return;
}
$html = ''; $html = '';
$elements = $this->element->getElements(); $elements = $this->element->getElements();
if (count($elements) > 0) { if (count($elements) > 0) {

View File

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

View File

@ -31,6 +31,10 @@ class Image extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
return;
}
$mediaIndex = $this->element->getMediaIndex(); $mediaIndex = $this->element->getMediaIndex();
$target = 'Pictures/' . $this->element->getTarget(); $target = 'Pictures/' . $this->element->getTarget();
$style = $this->element->getStyle(); $style = $this->element->getStyle();

View File

@ -29,6 +29,10 @@ class Link extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
return;
}
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('text:p'); // text:p $this->xmlWriter->startElement('text:p'); // text:p
} }

View File

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

View File

@ -29,6 +29,10 @@ class Text extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$fontStyle = $this->element->getFontStyle(); $fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle(); $paragraphStyle = $this->element->getParagraphStyle();

View File

@ -17,6 +17,7 @@
namespace PhpOffice\PhpWord\Writer\ODText\Element; namespace PhpOffice\PhpWord\Writer\ODText\Element;
use PhpOffice\PhpWord\Element\Link as LinkElement;
use PhpOffice\PhpWord\Element\Text as TextElement; use PhpOffice\PhpWord\Element\Text as TextElement;
/** /**
@ -31,6 +32,10 @@ class TextRun extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
return;
}
$elements = $this->element->getElements(); $elements = $this->element->getElements();
$this->xmlWriter->startElement('text:p'); $this->xmlWriter->startElement('text:p');
if (count($elements) > 0) { if (count($elements) > 0) {
@ -38,7 +43,7 @@ class TextRun extends Element
if ($element instanceof TextElement) { if ($element instanceof TextElement) {
$elementWriter = new Text($this->xmlWriter, $this->parentWriter, $element, true); $elementWriter = new Text($this->xmlWriter, $this->parentWriter, $element, true);
$elementWriter->write(); $elementWriter->write();
} elseif ($element instanceof Link) { } elseif ($element instanceof LinkElement) {
$elementWriter = new Link($this->xmlWriter, $this->parentWriter, $element, true); $elementWriter = new Link($this->xmlWriter, $this->parentWriter, $element, true);
$elementWriter->write(); $elementWriter->write();
} }

View File

@ -33,6 +33,10 @@ class Text extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$rtfText = ''; $rtfText = '';
$fontStyle = $this->element->getFontStyle(); $fontStyle = $this->element->getFontStyle();

View File

@ -33,7 +33,12 @@ class TextRun extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
return;
}
$rtfText = ''; $rtfText = '';
$elements = $this->element->getElements(); $elements = $this->element->getElements();
if (count($elements) > 0) { if (count($elements) > 0) {
$rtfText .= '\pard\nowidctlpar' . PHP_EOL; $rtfText .= '\pard\nowidctlpar' . PHP_EOL;

View File

@ -31,7 +31,12 @@ class Title extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) {
return;
}
$rtfText = ''; $rtfText = '';
$rtfText .= '\pard\nowidctlpar' . PHP_EOL; $rtfText .= '\pard\nowidctlpar' . PHP_EOL;
$rtfText .= $this->element->getText(); $rtfText .= $this->element->getText();
$rtfText .= '\par' . PHP_EOL; $rtfText .= '\par' . PHP_EOL;

View File

@ -33,6 +33,10 @@ class CheckBox extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\CheckBox) {
return;
}
$name = htmlspecialchars($this->element->getName()); $name = htmlspecialchars($this->element->getName());
$name = String::controlCharacterPHP2OOXML($name); $name = String::controlCharacterPHP2OOXML($name);
$text = htmlspecialchars($this->element->getText()); $text = htmlspecialchars($this->element->getText());

View File

@ -36,6 +36,10 @@ class Footnote extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) {
return;
}
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p'); $this->xmlWriter->startElement('w:p');
} }

View File

@ -43,6 +43,10 @@ class Image extends Element
*/ */
private function writeImage() private function writeImage()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
return;
}
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0);
$style = $this->element->getStyle(); $style = $this->element->getStyle();
$styleWriter = new ImageStyleWriter($this->xmlWriter, $style); $styleWriter = new ImageStyleWriter($this->xmlWriter, $style);

View File

@ -32,6 +32,10 @@ class Link extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Link) {
return;
}
$rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $rId = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0);
$fontStyle = $this->element->getFontStyle(); $fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle(); $paragraphStyle = $this->element->getParagraphStyle();

View File

@ -32,6 +32,10 @@ class ListItem extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\ListItem) {
return;
}
$textObject = $this->element->getTextObject(); $textObject = $this->element->getTextObject();
$depth = $this->element->getDepth(); $depth = $this->element->getDepth();
$numId = $this->element->getStyle()->getNumId(); $numId = $this->element->getStyle()->getNumId();

View File

@ -29,6 +29,10 @@ class Object extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Object) {
return;
}
$rIdObject = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0); $rIdObject = $this->element->getRelationId() + ($this->element->isInSection() ? 6 : 0);
$rIdImage = $this->element->getImageRelationId() + ($this->element->isInSection() ? 6 : 0); $rIdImage = $this->element->getImageRelationId() + ($this->element->isInSection() ? 6 : 0);
$shapeId = md5($rIdObject . '_' . $rIdImage); $shapeId = md5($rIdObject . '_' . $rIdImage);

View File

@ -33,6 +33,10 @@ class PreserveText extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\PreserveText) {
return;
}
$fontStyle = $this->element->getFontStyle(); $fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle(); $paragraphStyle = $this->element->getParagraphStyle();
$texts = $this->element->getText(); $texts = $this->element->getText();

View File

@ -34,6 +34,10 @@ class TOC extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TOC) {
return;
}
$titles = $this->element->getTitles(); $titles = $this->element->getTitles();
$writeFieldMark = true; $writeFieldMark = true;
@ -55,6 +59,9 @@ class TOC extends Element
/** /**
* Write title * Write title
*
* @param \PhpOffice\PhpWord\Element\Title $title
* @param bool $writeFieldMark
*/ */
private function writeTitle($title, $writeFieldMark) private function writeTitle($title, $writeFieldMark)
{ {

View File

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

View File

@ -33,6 +33,10 @@ class Text extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
return;
}
$fontStyle = $this->element->getFontStyle(); $fontStyle = $this->element->getFontStyle();
$paragraphStyle = $this->element->getParagraphStyle(); $paragraphStyle = $this->element->getParagraphStyle();
$text = htmlspecialchars($this->element->getText()); $text = htmlspecialchars($this->element->getText());

View File

@ -31,6 +31,10 @@ class TextRun extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
return;
}
$paragraphStyle = $this->element->getParagraphStyle(); $paragraphStyle = $this->element->getParagraphStyle();
$styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle); $styleWriter = new ParagraphStyleWriter($this->xmlWriter, $paragraphStyle);
$styleWriter->setIsInline(true); $styleWriter->setIsInline(true);

View File

@ -31,6 +31,10 @@ class Title extends Element
*/ */
public function write() public function write()
{ {
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Title) {
return;
}
$bookmarkId = $this->element->getBookmarkId(); $bookmarkId = $this->element->getBookmarkId();
$anchor = '_Toc' . ($bookmarkId + 252634154); $anchor = '_Toc' . ($bookmarkId + 252634154);
$style = $this->element->getStyle(); $style = $this->element->getStyle();

View File

@ -72,7 +72,7 @@ class Rels extends AbstractPart
} }
// Media relationships // Media relationships
if (!is_null($mediaRels) && is_array($mediaRels)) { if (is_array($mediaRels)) {
$mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink'); $mapping = array('image' => 'image', 'object' => 'oleObject', 'link' => 'hyperlink');
$targetPaths = array('image' => 'media/', 'object' => 'embeddings/'); $targetPaths = array('image' => 'media/', 'object' => 'embeddings/');
foreach ($mediaRels as $mediaRel) { foreach ($mediaRels as $mediaRel) {