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()
{
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Footnote) {
return;
}
$noteId = count($this->parentWriter->getNotes()) + 1;
$noteMark = $this->noteType . '-' . $this->element->getRelationId();
$this->parentWriter->addNote($noteId, $noteMark);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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