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')
{
$this->checkValidity($elementName);
$elementClass = dirname(get_class($this)) . '\\' . $elementName;
$elementClass = __NAMESPACE__ . '\\' . $elementName;
// Reset paragraph style for footnote and textrun. They have their own
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')
{
$this->checkValidity($elementName);
$elementClass = dirname(get_class($this)) . '\\' . $elementName;
$elementClass = __NAMESPACE__ . '\\' . $elementName;
$docPart = strtolower($elementName);
$addMethod = "add{$elementName}";

View File

@ -223,7 +223,7 @@ class Section extends AbstractContainer
private function addHeaderFooter($type = Header::AUTO, $header = true)
{
$collectionArray = $header ? 'headers' : 'footers';
$containerClass = dirname(get_class($this)) . '\\' . ($header ? 'Header' : 'Footer');
$containerClass = __NAMESPACE__ . '\\' . ($header ? 'Header' : 'Footer');
$collection = &$this->$collectionArray;
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;
/**
* Table element
* TextBox element
*
* @since 0.11.0
*/
class TextBox extends AbstractContainer
{

View File

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

View File

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

View File

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

View File

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

View File

@ -37,10 +37,10 @@ class Container extends AbstractElement
// Loop through subelements
$containerClass = basename(get_class($element));
$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) {
foreach ($subelements as $subelement) {
$writerClass = dirname(get_class($this)) . '\\' . basename(get_class($subelement));
$writerClass = __NAMESPACE__ . '\\' . basename(get_class($subelement));
if (class_exists($writerClass)) {
$writer = new $writerClass($xmlWriter, $subelement, $withoutP);
$writer->write();
@ -49,7 +49,7 @@ class Container extends AbstractElement
} else {
// Special case for Cell: They have to contain a TextBreak at least
if ($containerClass == 'Cell') {
$writerClass = dirname(get_class($this)) . '\\TextBreak';
$writerClass = __NAMESPACE__ . '\\TextBreak';
$writer = new $writerClass($xmlWriter, new TextBreakElement(), $withoutP);
$writer->write();
}

View File

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

View File

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

View File

@ -1,19 +1,19 @@
<?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
*/
* 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\Writer\Word2007\Style;
@ -22,27 +22,21 @@ use PhpOffice\PhpWord\Style\TextBox as TextBoxStyle;
/**
* 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
*/
public function write()
{
if (!($this->style instanceof \PhpOffice\PhpWord\Style\TextBox)) {
if (is_null($style = $this->getStyle())) {
return;
}
$wrapping = $this->style->getWrappingStyle();
$positioning = $this->style->getPositioning();
$xmlWriter = $this->getXmlWriter();
$wrapping = $style->getWrappingStyle();
$positioning = $style->getPositioning();
// Default style array
$styleArray = array(
@ -51,7 +45,7 @@ class TextBox extends AbstractStyle
'mso-width-relative' => 'margin',
'mso-height-relative' => 'margin',
);
$styleArray = array_merge($styleArray, $this->getElementStyle());
$styleArray = array_merge($styleArray, $this->getElementStyle($style));
// Absolute/relative positioning
$styleArray['position'] = $positioning;
@ -59,10 +53,10 @@ class TextBox extends AbstractStyle
$styleArray['mso-position-horizontal-relative'] = 'page';
$styleArray['mso-position-vertical-relative'] = 'page';
} elseif ($positioning == TextBoxStyle::POSITION_RELATIVE) {
$styleArray['mso-position-horizontal'] = $this->style->getPosHorizontal();
$styleArray['mso-position-vertical'] = $this->style->getPosVertical();
$styleArray['mso-position-horizontal-relative'] = $this->style->getPosHorizontalRel();
$styleArray['mso-position-vertical-relative'] = $this->style->getPosVerticalRel();
$styleArray['mso-position-horizontal'] = $style->getPosHorizontal();
$styleArray['mso-position-vertical'] = $style->getPosVertical();
$styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel();
$styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel();
$styleArray['margin-left'] = 0;
$styleArray['margin-top'] = 0;
}
@ -87,18 +81,18 @@ class TextBox extends AbstractStyle
$textboxStyle = $this->assembleStyle($styleArray);
$this->xmlWriter->writeAttribute('style', $textboxStyle);
$xmlWriter->writeAttribute('style', $textboxStyle);
$borderSize = $this->style->getBorderSize();
$borderSize = $style->getBorderSize();
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)) {
$this->xmlWriter->writeAttribute('stroked', 'f');
$xmlWriter->writeAttribute('stroked', 'f');
} else {
$this->xmlWriter->writeAttribute('strokecolor', $borderColor);
$xmlWriter->writeAttribute('strokecolor', $borderColor);
}
//@todo <v:stroke dashstyle="dashDot" linestyle="thickBetweenThin"/>
@ -111,88 +105,49 @@ class TextBox extends AbstractStyle
*/
public function writeW10Wrap()
{
if (!is_null($this->w10wrap)) {
$this->xmlWriter->startElement('w10:wrap');
$this->xmlWriter->writeAttribute('type', $this->w10wrap);
$xmlWriter = $this->getXmlWriter();
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:
$this->xmlWriter->writeAttribute('anchorx', "page");
$this->xmlWriter->writeAttribute('anchory', "page");
$xmlWriter->writeAttribute('anchorx', "page");
$xmlWriter->writeAttribute('anchory', "page");
break;
case TextBoxStyle::POSITION_RELATIVE:
switch ($this->style->getPosVerticalRel()) {
switch ($style->getPosVerticalRel()) {
case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN:
$this->xmlWriter->writeAttribute('anchory', "margin");
$xmlWriter->writeAttribute('anchory', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_PAGE:
$this->xmlWriter->writeAttribute('anchory', "page");
$xmlWriter->writeAttribute('anchory', "page");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_TMARGIN:
$this->xmlWriter->writeAttribute('anchory', "margin");
$xmlWriter->writeAttribute('anchory', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_BMARGIN:
$this->xmlWriter->writeAttribute('anchory', "page");
$xmlWriter->writeAttribute('anchory', "page");
break;
}
switch ($this->style->getPosHorizontalRel()) {
switch ($style->getPosHorizontalRel()) {
case TextBoxStyle::POSITION_RELATIVE_TO_MARGIN:
$this->xmlWriter->writeAttribute('anchorx', "margin");
$xmlWriter->writeAttribute('anchorx', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_PAGE:
$this->xmlWriter->writeAttribute('anchorx', "page");
$xmlWriter->writeAttribute('anchorx', "page");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_LMARGIN:
$this->xmlWriter->writeAttribute('anchorx', "margin");
$xmlWriter->writeAttribute('anchorx', "margin");
break;
case TextBoxStyle::POSITION_RELATIVE_TO_RMARGIN:
$this->xmlWriter->writeAttribute('anchorx', "page");
$xmlWriter->writeAttribute('anchorx', "page");
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);
}
}