Refactor the new textbox element

This commit is contained in:
Ivan Lanin 2014-05-08 12:58:44 +07:00
parent e12daacc0b
commit ea41b08a9a
11 changed files with 104 additions and 136 deletions

View File

@ -78,7 +78,7 @@ abstract class AbstractContainer extends AbstractElement
public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text') public function addText($text, $fontStyle = null, $paragraphStyle = null, $elementName = 'Text')
{ {
$this->checkValidity($elementName); $this->checkValidity($elementName);
$elementClass = dirname(get_class($this)) . '\\' . $elementName; $elementClass = __NAMESPACE__ . '\\' . $elementName;
// Reset paragraph style for footnote and textrun. They have their own // Reset paragraph style for footnote and textrun. They have their own
if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) { if (in_array($this->container, array('textrun', 'footnote', 'endnote'))) {
@ -248,7 +248,7 @@ abstract class AbstractContainer extends AbstractElement
public function addFootnote($paragraphStyle = null, $elementName = 'Footnote') public function addFootnote($paragraphStyle = null, $elementName = 'Footnote')
{ {
$this->checkValidity($elementName); $this->checkValidity($elementName);
$elementClass = dirname(get_class($this)) . '\\' . $elementName; $elementClass = __NAMESPACE__ . '\\' . $elementName;
$docPart = strtolower($elementName); $docPart = strtolower($elementName);
$addMethod = "add{$elementName}"; $addMethod = "add{$elementName}";

View File

@ -223,7 +223,7 @@ class Section extends AbstractContainer
private function addHeaderFooter($type = Header::AUTO, $header = true) private function addHeaderFooter($type = Header::AUTO, $header = true)
{ {
$collectionArray = $header ? 'headers' : 'footers'; $collectionArray = $header ? 'headers' : 'footers';
$containerClass = dirname(get_class($this)) . '\\' . ($header ? 'Header' : 'Footer'); $containerClass = __NAMESPACE__ . '\\' . ($header ? 'Header' : 'Footer');
$collection = &$this->$collectionArray; $collection = &$this->$collectionArray;
if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) { if (in_array($type, array(Header::AUTO, Header::FIRST, Header::EVEN))) {

View File

@ -20,7 +20,9 @@ namespace PhpOffice\PhpWord\Element;
use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle; use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
/** /**
* Table element * TextBox element
*
* @since 0.11.0
*/ */
class TextBox extends AbstractContainer class TextBox extends AbstractContainer
{ {

View File

@ -249,7 +249,7 @@ abstract class AbstractStyle
*/ */
protected function setObjectVal($value, $styleName, &$style) protected function setObjectVal($value, $styleName, &$style)
{ {
$styleClass = dirname(get_class($this)) . '\\' . $styleName; $styleClass = __NAMESPACE__ . '\\' . $styleName;
if (is_array($value)) { if (is_array($value)) {
if (!$style instanceof $styleClass) { if (!$style instanceof $styleClass) {
$style = new $styleClass(); $style = new $styleClass();

View File

@ -19,6 +19,8 @@ namespace PhpOffice\PhpWord\Style;
/** /**
* TextBox style * TextBox style
*
* @since 0.11.0
*/ */
class TextBox extends Image class TextBox extends Image
{ {

View File

@ -73,7 +73,7 @@ class Element
public function write() public function write()
{ {
$content = ''; $content = '';
$writerClass = dirname(get_class($this)) . '\\' . basename(get_class($this->element)); $writerClass = __NAMESPACE__ . '\\' . basename(get_class($this->element));
if (class_exists($writerClass)) { if (class_exists($writerClass)) {
$writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
$content = $writer->write(); $content = $writer->write();

View File

@ -68,7 +68,7 @@ class Element
public function write() public function write()
{ {
$content = ''; $content = '';
$writerClass = dirname(get_class($this)) . '\\' . basename(get_class($this->element)); $writerClass = __NAMESPACE__ . '\\' . basename(get_class($this->element));
if (class_exists($writerClass)) { if (class_exists($writerClass)) {
$writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP); $writer = new $writerClass($this->parentWriter, $this->element, $this->withoutP);
$content = $writer->write(); $content = $writer->write();

View File

@ -37,10 +37,10 @@ class Container extends AbstractElement
// Loop through subelements // Loop through subelements
$containerClass = basename(get_class($element)); $containerClass = basename(get_class($element));
$subelements = $element->getElements(); $subelements = $element->getElements();
$withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false;
if (count($subelements) > 0) { if (count($subelements) > 0) {
foreach ($subelements as $subelement) { foreach ($subelements as $subelement) {
$writerClass = dirname(get_class($this)) . '\\' . basename(get_class($subelement)); $writerClass = __NAMESPACE__ . '\\' . basename(get_class($subelement));
if (class_exists($writerClass)) { if (class_exists($writerClass)) {
$writer = new $writerClass($xmlWriter, $subelement, $withoutP); $writer = new $writerClass($xmlWriter, $subelement, $withoutP);
$writer->write(); $writer->write();
@ -49,7 +49,7 @@ class Container extends AbstractElement
} else { } else {
// Special case for Cell: They have to contain a TextBreak at least // Special case for Cell: They have to contain a TextBreak at least
if ($containerClass == 'Cell') { if ($containerClass == 'Cell') {
$writerClass = dirname(get_class($this)) . '\\TextBreak'; $writerClass = __NAMESPACE__ . '\\TextBreak';
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP); $writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
$writer->write(); $writer->write();
} }

View File

@ -23,52 +23,61 @@ use PhpOffice\PhpWord\Writer\Word2007\Style\TextBox as TextBoxStyleWriter;
/** /**
* TextBox element writer * TextBox element writer
* *
* @since 0.11.0
*/ */
class TextBox extends Element class TextBox extends AbstractElement
{ {
/** /**
* Write element * Write element
*/ */
public function write() public function write()
{ {
$tbxStyle = $this->element->getStyle(); $xmlWriter = $this->getXmlWriter();
if ($tbxStyle instanceof TextBoxStyle) { $element = $this->getElement();
$styleWriter = new TextBoxStyleWriter($this->xmlWriter, $tbxStyle); $style = $element->getStyle();
if ($style instanceof TextBoxStyle) {
$styleWriter = new TextBoxStyleWriter($xmlWriter, $style);
$styleWriter->write(); $styleWriter->write();
} }
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->startElement('w:p'); $xmlWriter->startElement('w:p');
if (!is_null($tbxStyle->getAlign())) { if (!is_null($style->getAlign())) {
$this->xmlWriter->startElement('w:pPr'); $xmlWriter->startElement('w:pPr');
$this->xmlWriter->startElement('w:jc'); $xmlWriter->startElement('w:jc');
$this->xmlWriter->writeAttribute('w:val', $tbxStyle->getAlign()); $xmlWriter->writeAttribute('w:val', $style->getAlign());
$this->xmlWriter->endElement(); // w:jc $xmlWriter->endElement(); // w:jc
$this->xmlWriter->endElement(); // w:pPr $xmlWriter->endElement(); // w:pPr
} }
} }
$this->xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');
$this->xmlWriter->startElement('w:pict'); $xmlWriter->startElement('w:pict');
$this->xmlWriter->startElement('v:shape'); $xmlWriter->startElement('v:shape');
$this->xmlWriter->writeAttribute('type', '#_x0000_t0202'); $xmlWriter->writeAttribute('type', '#_x0000_t0202');
$styleWriter->write(); $styleWriter->write();
$this->xmlWriter->startElement('v:textbox');
$margins = implode(', ', $tbxStyle->getInnerMargin()); $xmlWriter->startElement('v:textbox');
$this->xmlWriter->writeAttribute('inset', $margins); $margins = implode(', ', $style->getInnerMargin());
$this->xmlWriter->startElement('w:txbxContent'); $xmlWriter->writeAttribute('inset', $margins);
$this->xmlWriter->startElement('w:p');
$this->parentWriter->writeContainerElements($this->xmlWriter, $this->element); $xmlWriter->startElement('w:txbxContent');
$this->xmlWriter->endElement(); // w:p $xmlWriter->startElement('w:p');
$this->xmlWriter->endElement(); // w:txbxContent $containerWriter = new Container($xmlWriter, $element);
$this->xmlWriter->endElement(); // v: textbox $containerWriter->write();
$xmlWriter->endElement(); // w:p
$xmlWriter->endElement(); // w:txbxContent
$xmlWriter->endElement(); // v: textbox
$styleWriter->writeW10Wrap(); $styleWriter->writeW10Wrap();
$this->xmlWriter->endElement(); // v:shape $xmlWriter->endElement(); // v:shape
$this->xmlWriter->endElement(); // w:pict $xmlWriter->endElement(); // w:pict
$this->xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
if (!$this->withoutP) { if (!$this->withoutP) {
$this->xmlWriter->endElement(); // w:p $xmlWriter->endElement(); // w:p
} }
} }
} }

View File

@ -31,7 +31,7 @@ class Image extends AbstractStyle
* *
* @var string * @var string
*/ */
private $w10wrap; protected $w10wrap;
/** /**
* Write style * Write style
@ -52,7 +52,7 @@ class Image extends AbstractStyle
'mso-width-relative' => 'margin', 'mso-width-relative' => 'margin',
'mso-height-relative' => 'margin', 'mso-height-relative' => 'margin',
); );
$styleArray = array_merge($styleArray, $this->getElementStyle($this->style)); $styleArray = array_merge($styleArray, $this->getElementStyle($style));
// Absolute/relative positioning // Absolute/relative positioning
$styleArray['position'] = $positioning; $styleArray['position'] = $positioning;
@ -112,7 +112,7 @@ class Image extends AbstractStyle
* *
* @return array * @return array
*/ */
private function getElementStyle(ImageStyle $style) protected function getElementStyle(ImageStyle $style)
{ {
$styles = array(); $styles = array();
$styleValues = array( $styleValues = array(
@ -136,7 +136,7 @@ class Image extends AbstractStyle
* @param array $styles * @param array $styles
* @return string * @return string
*/ */
private function assembleStyle($styles = array()) protected function assembleStyle($styles = array())
{ {
$style = ''; $style = '';
foreach ($styles as $key => $value) { foreach ($styles as $key => $value) {

View File

@ -1,19 +1,19 @@
<?php <?php
/** /**
* This file is part of PHPWord - A pure PHP library for reading and writing * This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents. * word processing documents.
* *
* PHPWord is free software distributed under the terms of the GNU Lesser * PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation. * General Public License version 3 as published by the Free Software Foundation.
* *
* For the full copyright and license information, please read the LICENSE * For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of * file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
* *
* @link https://github.com/PHPOffice/PHPWord * @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors * @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/ */
namespace PhpOffice\PhpWord\Writer\Word2007\Style; namespace PhpOffice\PhpWord\Writer\Word2007\Style;
@ -22,27 +22,21 @@ use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
/** /**
* TextBox style writer * TextBox style writer
* *
* @since 0.11.0
*/ */
class TextBox extends AbstractStyle class TextBox extends Image
{ {
/**
* w10 namespace wrapping type
*
* @var string
*/
private $w10wrap;
/** /**
* Write style * Write style
*/ */
public function write() public function write()
{ {
if (!($this->style instanceof \PhpOffice\PhpWord\Style\TextBox)) { if (is_null($style = $this->getStyle())) {
return; return;
} }
$xmlWriter = $this->getXmlWriter();
$wrapping = $this->style->getWrappingStyle(); $wrapping = $style->getWrappingStyle();
$positioning = $this->style->getPositioning(); $positioning = $style->getPositioning();
// Default style array // Default style array
$styleArray = array( $styleArray = array(
@ -51,7 +45,7 @@ class TextBox extends AbstractStyle
'mso-width-relative' => 'margin', 'mso-width-relative' => 'margin',
'mso-height-relative' => 'margin', 'mso-height-relative' => 'margin',
); );
$styleArray = array_merge($styleArray, $this->getElementStyle()); $styleArray = array_merge($styleArray, $this->getElementStyle($style));
// Absolute/relative positioning // Absolute/relative positioning
$styleArray['position'] = $positioning; $styleArray['position'] = $positioning;
@ -59,10 +53,10 @@ class TextBox extends AbstractStyle
$styleArray['mso-position-horizontal-relative'] = 'page'; $styleArray['mso-position-horizontal-relative'] = 'page';
$styleArray['mso-position-vertical-relative'] = 'page'; $styleArray['mso-position-vertical-relative'] = 'page';
} elseif ($positioning == TextBoxStyle::POSITION_RELATIVE) { } elseif ($positioning == TextBoxStyle::POSITION_RELATIVE) {
$styleArray['mso-position-horizontal'] = $this->style->getPosHorizontal(); $styleArray['mso-position-horizontal'] = $style->getPosHorizontal();
$styleArray['mso-position-vertical'] = $this->style->getPosVertical(); $styleArray['mso-position-vertical'] = $style->getPosVertical();
$styleArray['mso-position-horizontal-relative'] = $this->style->getPosHorizontalRel(); $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel();
$styleArray['mso-position-vertical-relative'] = $this->style->getPosVerticalRel(); $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel();
$styleArray['margin-left'] = 0; $styleArray['margin-left'] = 0;
$styleArray['margin-top'] = 0; $styleArray['margin-top'] = 0;
} }
@ -87,18 +81,18 @@ class TextBox extends AbstractStyle
$textboxStyle = $this->assembleStyle($styleArray); $textboxStyle = $this->assembleStyle($styleArray);
$this->xmlWriter->writeAttribute('style', $textboxStyle); $xmlWriter->writeAttribute('style', $textboxStyle);
$borderSize = $this->style->getBorderSize(); $borderSize = $style->getBorderSize();
if ($borderSize !== null) { if ($borderSize !== null) {
$this->xmlWriter->writeAttribute('strokeweight', $this->style->getBorderSize().'pt'); $xmlWriter->writeAttribute('strokeweight', $style->getBorderSize().'pt');
} }
$borderColor = $this->style->getBorderColor(); $borderColor = $style->getBorderColor();
if (empty($borderColor)) { if (empty($borderColor)) {
$this->xmlWriter->writeAttribute('stroked', 'f'); $xmlWriter->writeAttribute('stroked', 'f');
} else { } else {
$this->xmlWriter->writeAttribute('strokecolor', $borderColor); $xmlWriter->writeAttribute('strokecolor', $borderColor);
} }
//@todo <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/> //@todo <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/>
@ -111,88 +105,49 @@ class TextBox extends AbstractStyle
*/ */
public function writeW10Wrap() public function writeW10Wrap()
{ {
if (!is_null($this->w10wrap)) { $xmlWriter = $this->getXmlWriter();
$this->xmlWriter->startElement('w10:wrap');
$this->xmlWriter->writeAttribute('type', $this->w10wrap);
switch ($this->style->getPositioning()) { if (!is_null($this->w10wrap)) {
$xmlWriter->startElement('w10:wrap');
$xmlWriter->writeAttribute('type', $this->w10wrap);
switch ($style->getPositioning()) {
case TextBoxStyle::POSITION_ABSOLUTE: case TextBoxStyle::POSITION_ABSOLUTE:
$this->xmlWriter->writeAttribute('anchorx', "page"); $xmlWriter->writeAttribute('anchorx', "page");
$this->xmlWriter->writeAttribute('anchory', "page"); $xmlWriter->writeAttribute('anchory', "page");
break; break;
case TextBoxStyle::POSITION_RELATIVE: case TextBoxStyle::POSITION_RELATIVE:
switch ($this->style->getPosVerticalRel()) { switch ($style->getPosVerticalRel()) {
case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN: case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN:
$this->xmlWriter->writeAttribute('anchory', "margin"); $xmlWriter->writeAttribute('anchory', "margin");
break; break;
case TextBoxStyle::POSITION_RELATIVE_TO_PAGE: case TextBoxStyle::POSITION_RELATIVE_TO_PAGE:
$this->xmlWriter->writeAttribute('anchory', "page"); $xmlWriter->writeAttribute('anchory', "page");
break; break;
case TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN: case TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN:
$this->xmlWriter->writeAttribute('anchory', "margin"); $xmlWriter->writeAttribute('anchory', "margin");
break; break;
case TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN: case TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN:
$this->xmlWriter->writeAttribute('anchory', "page"); $xmlWriter->writeAttribute('anchory', "page");
break; break;
} }
switch ($this->style->getPosHorizontalRel()) { switch ($style->getPosHorizontalRel()) {
case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN: case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN:
$this->xmlWriter->writeAttribute('anchorx', "margin"); $xmlWriter->writeAttribute('anchorx', "margin");
break; break;
case TextBoxStyle::POSITION_RELATIVE_TO_PAGE: case TextBoxStyle::POSITION_RELATIVE_TO_PAGE:
$this->xmlWriter->writeAttribute('anchorx', "page"); $xmlWriter->writeAttribute('anchorx', "page");
break; break;
case TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN: case TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN:
$this->xmlWriter->writeAttribute('anchorx', "margin"); $xmlWriter->writeAttribute('anchorx', "margin");
break; break;
case TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN: case TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN:
$this->xmlWriter->writeAttribute('anchorx', "page"); $xmlWriter->writeAttribute('anchorx', "page");
break; break;
} }
} }
$this->xmlWriter->endElement(); // w10:wrap $xmlWriter->endElement(); // w10:wrap
} }
} }
/**
* Get element style
*
* @return array
*/
private function getElementStyle()
{
$styles = array();
$styleValues = array(
'width' => $this->style->getWidth(),
'height' => $this->style->getHeight(),
'margin-top' => $this->style->getMarginTop(),
'margin-left' => $this->style->getMarginLeft()
);
foreach ($styleValues as $key => $value) {
if (!is_null($value) && $value != '') {
$styles[$key] = $value . 'px';
}
}
return $styles;
}
/**
* Assemble style array into style string
*
* @param array $styles
* @return string
*/
private function assembleStyle($styles = array())
{
$style = '';
foreach ($styles as $key => $value) {
if (!is_null($value) && $value != '') {
$style .= "{$key}:{$value}; ";
}
}
return trim($style);
}
} }