From 246557e3be6d7a689723abb61deea489d6f66a39 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Fri, 9 May 2014 02:09:20 +0700 Subject: [PATCH] Reactivate PHPMD and fix some rules for textbox --- .travis.yml | 2 +- samples/Sample_25_TextBox.php | 31 ++++++++++++++----- src/PhpWord/Element/AbstractContainer.php | 19 ++++++------ src/PhpWord/Style/TextBox.php | 2 +- .../Writer/Word2007/Element/Container.php | 4 +-- .../Writer/Word2007/Element/TextBox.php | 5 +-- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 50587e49..7593379c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,7 +52,7 @@ script: ## PHP Copy/Paste Detector - php phpcpd.phar src/ tests/ --verbose ## PHP Mess Detector - #- phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php + - phpmd src/,tests/ text unusedcode,naming,design,controversial --exclude pclzip.lib.php ## PHPLOC #- php phploc.phar src/ ## PHPUnit diff --git a/samples/Sample_25_TextBox.php b/samples/Sample_25_TextBox.php index 575a1df5..1946315f 100644 --- a/samples/Sample_25_TextBox.php +++ b/samples/Sample_25_TextBox.php @@ -6,14 +6,29 @@ echo date('H:i:s') , ' Create new PhpWord object' , EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); -$textbox = $section->addTextBox(array('align' => 'left', 'width' => 300, 'borderSize' => 1, 'borderColor' => '#FF0000')); -$textbox->addText('Text box content '); -$textbox->addText('with bold text', array('bold' => true)); -$textbox->addText(', '); -$textbox->addLink('http://www.google.com', 'link'); -$textbox->addText(', and image '); -$textbox->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); -$textbox->addText('.'); + +// In section +$textbox = $section->addTextBox(array('align' => 'left', 'width' => 400, 'borderSize' => 1, 'borderColor' => '#FF0000')); +$textbox->addText('Text box content in section.'); +$textbox->addText('Another line.'); + +// Inside table +$section->addTextBreak(2); +$cell = $section->addTable()->addRow()->addCell(300); +$textbox = $cell->addTextBox(array('borderSize' => 1, 'borderColor' => '#0000FF', 'innerMargin' => 100)); +$textbox->addText('Inside table'); + +// Inside header with textrun +$header = $section->addHeader(); +$textbox = $header->addTextBox(array('align' => 'center', 'width' => 600, 'borderSize' => 1, 'borderColor' => '#00FF00')); +$textrun = $textbox->addTextRun(); +$textrun->addText('TextBox in header. TextBox can contain a TextRun '); +$textrun->addText('with bold text', array('bold' => true)); +$textrun->addText(', '); +$textrun->addLink('http://www.google.com', 'link'); +$textrun->addText(', and image '); +$textrun->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18)); +$textrun->addText('.'); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 290c54d9..8b72f660 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -100,7 +100,7 @@ abstract class AbstractContainer extends AbstractElement */ public function addTextRun($paragraphStyle = null) { - $this->checkValidity('Textrun'); + $this->checkValidity('TextRun'); $element = new TextRun($paragraphStyle); $element->setDocPart($this->getDocPart(), $this->getDocPartId()); @@ -327,10 +327,10 @@ abstract class AbstractContainer extends AbstractElement 'TextBreak' => $allContainers, 'Image' => $allContainers, 'Object' => $allContainers, - 'TextRun' => array('section', 'header', 'footer', 'cell'), - 'ListItem' => array('section', 'header', 'footer', 'cell'), + 'TextRun' => array('section', 'header', 'footer', 'cell', 'textbox'), + 'ListItem' => array('section', 'header', 'footer', 'cell', 'textbox'), 'CheckBox' => array('section', 'header', 'footer', 'cell'), - 'TextBox' => array('section', 'header', 'footer'), + 'TextBox' => array('section', 'header', 'footer', 'cell'), 'Footnote' => array('section', 'textrun', 'cell'), 'Endnote' => array('section', 'textrun', 'cell'), 'PreserveText' => array('header', 'footer', 'cell'), @@ -346,7 +346,7 @@ abstract class AbstractContainer extends AbstractElement // Check if a method is valid for current container if (array_key_exists($method, $validContainers)) { if (!in_array($this->container, $validContainers[$method])) { - throw new \BadMethodCallException(); + throw new \BadMethodCallException("Cannot put $method in $this->container."); } } // Check if a method is valid for current container, located in other container @@ -356,7 +356,7 @@ abstract class AbstractContainer extends AbstractElement $allowedDocParts = $rules[1]; foreach ($containers as $container) { if ($this->container == $container && !in_array($this->getDocPart(), $allowedDocParts)) { - throw new \BadMethodCallException(); + throw new \BadMethodCallException("Cannot put $method in $this->container."); } } } @@ -369,11 +369,12 @@ abstract class AbstractContainer extends AbstractElement */ private function checkElementDocPart() { - $isCellTextrun = in_array($this->container, array('cell', 'textrun', 'textbox')); - $docPart = $isCellTextrun ? $this->getDocPart() : $this->container; - $docPartId = $isCellTextrun ? $this->getDocPartId() : $this->sectionId; + $inOtherPart = in_array($this->container, array('cell', 'textrun', 'textbox')); + $docPart = $inOtherPart ? $this->getDocPart() : $this->container; + $docPartId = $inOtherPart ? $this->getDocPartId() : $this->sectionId; $inHeaderFooter = ($docPart == 'header' || $docPart == 'footer'); $docPartId = $inHeaderFooter ? $this->getDocPartId() : $docPartId; + return $inHeaderFooter ? $docPart . $docPartId : $docPart; } diff --git a/src/PhpWord/Style/TextBox.php b/src/PhpWord/Style/TextBox.php index 2beeaed1..da19cd10 100644 --- a/src/PhpWord/Style/TextBox.php +++ b/src/PhpWord/Style/TextBox.php @@ -177,7 +177,7 @@ class TextBox extends Image public function hasInnerMargins() { $hasInnerMargins = false; - $margins = $this->getInnerMargins(); + $margins = $this->getInnerMargin(); for ($i = 0; $i < count($margins); $i++) { if (!is_null($margins[$i])) { $hasInnerMargins = true; diff --git a/src/PhpWord/Writer/Word2007/Element/Container.php b/src/PhpWord/Writer/Word2007/Element/Container.php index de2e90d9..0561509d 100644 --- a/src/PhpWord/Writer/Word2007/Element/Container.php +++ b/src/PhpWord/Writer/Word2007/Element/Container.php @@ -42,9 +42,9 @@ class Container extends AbstractElement $container = $this->getElement(); // Loop through subelements - $containerClass = substr(get_class($container), strrpos(get_class($this), '\\') + 1); + $containerClass = substr(get_class($container), strrpos(get_class($container), '\\') + 1); $subelements = $container->getElements(); - $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote', 'TextBox')) ? true : false; + $withoutP = in_array($containerClass, array('TextRun', 'Footnote', 'Endnote')) ? true : false; if (count($subelements) > 0) { foreach ($subelements as $subelement) { $writerClass = str_replace('PhpOffice\\PhpWord\\Element', $this->namespace, get_class($subelement)); diff --git a/src/PhpWord/Writer/Word2007/Element/TextBox.php b/src/PhpWord/Writer/Word2007/Element/TextBox.php index d464b967..6b63c31a 100644 --- a/src/PhpWord/Writer/Word2007/Element/TextBox.php +++ b/src/PhpWord/Writer/Word2007/Element/TextBox.php @@ -54,12 +54,13 @@ class TextBox extends AbstractElement $styleWriter->write(); $xmlWriter->startElement('v:textbox'); $styleWriter->writeInnerMargin(); + + // TextBox content, serving as a container $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(); $xmlWriter->endElement(); // v:shape