From 27fa3ba2337de7be653d516ca688b460c8c481e4 Mon Sep 17 00:00:00 2001 From: Ivan Lanin Date: Thu, 15 May 2014 01:13:22 +0700 Subject: [PATCH] Remove fontStyle parameter from ListItemRun and various small fixes --- .scrutinizer.yml | 1 - CHANGELOG.md | 1 + src/PhpWord/Element/AbstractContainer.php | 5 +- src/PhpWord/Element/ListItemRun.php | 3 +- src/PhpWord/Element/Title.php | 56 ----- src/PhpWord/Style/Border.php | 140 +++++------ src/PhpWord/Style/Table.php | 229 ++++++++++-------- src/PhpWord/Writer/HTML/Element/Text.php | 2 +- .../Word2007/Element/AbstractElement.php | 2 - .../Writer/Word2007/Part/ContentTypes.php | 2 - src/PhpWord/Writer/Word2007/Style/Cell.php | 2 +- src/PhpWord/Writer/Word2007/Style/Section.php | 2 +- src/PhpWord/Writer/Word2007/Style/Table.php | 4 +- 13 files changed, 204 insertions(+), 245 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 40cc99a4..ab53c762 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -9,7 +9,6 @@ tools: enabled: true config: standard: PSR2 - php_cpd: true php_mess_detector: enabled: true config: diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b35a21..3bed3012 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r - RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158 - Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237 - RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196 +- ListItemRun: Remove fontStyle parameter because ListItemRun is inherited from TextRun and TextRun doesn't have fontStyle - @ivanlanin ### Bugfixes diff --git a/src/PhpWord/Element/AbstractContainer.php b/src/PhpWord/Element/AbstractContainer.php index 5afddc5f..81dbedc2 100644 --- a/src/PhpWord/Element/AbstractContainer.php +++ b/src/PhpWord/Element/AbstractContainer.php @@ -213,14 +213,13 @@ abstract class AbstractContainer extends AbstractElement * Add listitemrun element * * @param int $depth - * @param mixed $fontStyle * @param mixed $listStyle * @param mixed $paragraphStyle * @return \PhpOffice\PhpWord\Element\ListItemRun */ - public function addListItemRun($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) + public function addListItemRun($depth = 0, $listStyle = null, $paragraphStyle = null) { - return $this->addElement('ListItemRun', $depth, $fontStyle, $listStyle, $paragraphStyle); + return $this->addElement('ListItemRun', $depth, $listStyle, $paragraphStyle); } /** diff --git a/src/PhpWord/Element/ListItemRun.php b/src/PhpWord/Element/ListItemRun.php index 2a2a51af..fb219f91 100644 --- a/src/PhpWord/Element/ListItemRun.php +++ b/src/PhpWord/Element/ListItemRun.php @@ -48,11 +48,10 @@ class ListItemRun extends TextRun * Create a new ListItem * * @param int $depth - * @param mixed $fontStyle * @param array|string|null $listStyle * @param mixed $paragraphStyle */ - public function __construct($depth = 0, $fontStyle = null, $listStyle = null, $paragraphStyle = null) + public function __construct($depth = 0, $listStyle = null, $paragraphStyle = null) { $this->depth = $depth; diff --git a/src/PhpWord/Element/Title.php b/src/PhpWord/Element/Title.php index 3a1b3049..d5206879 100644 --- a/src/PhpWord/Element/Title.php +++ b/src/PhpWord/Element/Title.php @@ -46,14 +46,6 @@ class Title extends AbstractElement */ private $style; - /** - * Title anchor - * - * @var string - * @deprecated 0.10.0 - */ - private $anchor; - /** * Create a new Title Element * @@ -101,52 +93,4 @@ class Title extends AbstractElement { return $this->style; } - - /** - * Set Anchor - * - * @param string $anchor - * @deprecated 0.10.0 - * @codeCoverageIgnore - */ - public function setAnchor($anchor) - { - $this->anchor = $anchor; - } - - /** - * Get Anchor - * - * @return string - * @deprecated 0.10.0 - * @codeCoverageIgnore - */ - public function getAnchor() - { - return '_Toc' . (252634154 + $this->getRelationId()); - } - - /** - * Set Bookmark ID - * - * @param int $value - * @deprecated 0.11.0 - * @codeCoverageIgnore - */ - public function setBookmarkId($value) - { - $this->setRelationId($value); - } - - /** - * Get bookmark ID - * - * @return int - * @deprecated 0.11.0 - * @codeCoverageIgnore - */ - public function getBookmarkId() - { - return $this->getRelationId(); - } } diff --git a/src/PhpWord/Style/Border.php b/src/PhpWord/Style/Border.php index f7c479fe..f0fd8650 100644 --- a/src/PhpWord/Style/Border.php +++ b/src/PhpWord/Style/Border.php @@ -78,22 +78,6 @@ class Border extends AbstractStyle */ protected $borderBottomColor; - /** - * Set border size - * - * @param int|float $value - * @return self - */ - public function setBorderSize($value = null) - { - $this->setBorderTopSize($value); - $this->setBorderLeftSize($value); - $this->setBorderRightSize($value); - $this->setBorderBottomSize($value); - - return $this; - } - /** * Get border size * @@ -110,17 +94,17 @@ class Border extends AbstractStyle } /** - * Set border color + * Set border size * - * @param string $value + * @param int|float $value * @return self */ - public function setBorderColor($value = null) + public function setBorderSize($value = null) { - $this->setBorderTopColor($value); - $this->setBorderLeftColor($value); - $this->setBorderRightColor($value); - $this->setBorderBottomColor($value); + $this->setBorderTopSize($value); + $this->setBorderLeftSize($value); + $this->setBorderRightSize($value); + $this->setBorderBottomSize($value); return $this; } @@ -141,14 +125,17 @@ class Border extends AbstractStyle } /** - * Set border top size + * Set border color * - * @param int|float $value + * @param string $value * @return self */ - public function setBorderTopSize($value = null) + public function setBorderColor($value = null) { - $this->borderTopSize = $value; + $this->setBorderTopColor($value); + $this->setBorderLeftColor($value); + $this->setBorderRightColor($value); + $this->setBorderBottomColor($value); return $this; } @@ -163,6 +150,29 @@ class Border extends AbstractStyle return $this->borderTopSize; } + /** + * Set border top size + * + * @param int|float $value + * @return self + */ + public function setBorderTopSize($value = null) + { + $this->borderTopSize = $this->setNumericVal($value, $this->borderTopSize); + + return $this; + } + + /** + * Get border top color + * + * @return string + */ + public function getBorderTopColor() + { + return $this->borderTopColor; + } + /** * Set border top color * @@ -177,13 +187,13 @@ class Border extends AbstractStyle } /** - * Get border top color + * Get border left size * - * @return string + * @return int|float */ - public function getBorderTopColor() + public function getBorderLeftSize() { - return $this->borderTopColor; + return $this->borderLeftSize; } /** @@ -194,19 +204,19 @@ class Border extends AbstractStyle */ public function setBorderLeftSize($value = null) { - $this->borderLeftSize = $value; + $this->borderLeftSize = $this->setNumericVal($value, $this->borderLeftSize); return $this; } /** - * Get border left size + * Get border left color * - * @return int|float + * @return string */ - public function getBorderLeftSize() + public function getBorderLeftColor() { - return $this->borderLeftSize; + return $this->borderLeftColor; } /** @@ -223,13 +233,13 @@ class Border extends AbstractStyle } /** - * Get border left color + * Get border right size * - * @return string + * @return int|float */ - public function getBorderLeftColor() + public function getBorderRightSize() { - return $this->borderLeftColor; + return $this->borderRightSize; } /** @@ -240,19 +250,19 @@ class Border extends AbstractStyle */ public function setBorderRightSize($value = null) { - $this->borderRightSize = $value; + $this->borderRightSize = $this->setNumericVal($value, $this->borderRightSize); return $this; } /** - * Get border right size + * Get border right color * - * @return int|float + * @return string */ - public function getBorderRightSize() + public function getBorderRightColor() { - return $this->borderRightSize; + return $this->borderRightColor; } /** @@ -269,13 +279,13 @@ class Border extends AbstractStyle } /** - * Get border right color + * Get border bottom size * - * @return string + * @return int|float */ - public function getBorderRightColor() + public function getBorderBottomSize() { - return $this->borderRightColor; + return $this->borderBottomSize; } /** @@ -286,19 +296,19 @@ class Border extends AbstractStyle */ public function setBorderBottomSize($value = null) { - $this->borderBottomSize = $value; + $this->borderBottomSize = $this->setNumericVal($value, $this->borderBottomSize); return $this; } /** - * Get border bottom size + * Get border bottom color * - * @return int|float + * @return string */ - public function getBorderBottomSize() + public function getBorderBottomColor() { - return $this->borderBottomSize; + return $this->borderBottomColor; } /** @@ -315,30 +325,14 @@ class Border extends AbstractStyle } /** - * Get border bottom color - * - * @return string - */ - public function getBorderBottomColor() - { - return $this->borderBottomColor; - } - - /** - * Has borders? + * Check if any of the border is not null * * @return bool */ - public function hasBorders() + public function hasBorder() { - $hasBorders = false; $borders = $this->getBorderSize(); - for ($i = 0; $i < count($borders); $i++) { - if (!is_null($borders[$i])) { - $hasBorders = true; - } - } - return $hasBorders; + return $borders !== array_filter($borders, 'is_null'); } } diff --git a/src/PhpWord/Style/Table.php b/src/PhpWord/Style/Table.php index c077f499..9d5cca87 100644 --- a/src/PhpWord/Style/Table.php +++ b/src/PhpWord/Style/Table.php @@ -34,35 +34,35 @@ class Table extends Border * * @var \PhpOffice\PhpWord\Style\Table */ - private $firstRow = null; + private $firstRow; /** * Cell margin top * * @var int */ - private $cellMarginTop = null; + private $cellMarginTop; /** * Cell margin left * * @var int */ - private $cellMarginLeft = null; + private $cellMarginLeft; /** * Cell margin right * * @var int */ - private $cellMarginRight = null; + private $cellMarginRight; /** * Cell margin bottom * * @var int */ - private $cellMarginBottom = null; + private $cellMarginBottom; /** * Border size inside horizontal @@ -123,9 +123,12 @@ class Table extends Border public function __construct($tableStyle = null, $firstRowStyle = null) { $this->alignment = new Alignment(); - if (!is_null($firstRowStyle) && is_array($firstRowStyle)) { - $this->firstRow = clone $this; + if ($tableStyle !== null && is_array($tableStyle)) { + $this->setStyleByArray($tableStyle); + } + if ($firstRowStyle !== null && is_array($firstRowStyle)) { + $this->firstRow = clone $this; unset($this->firstRow->firstRow); unset($this->firstRow->cellMarginBottom); unset($this->firstRow->cellMarginTop); @@ -137,10 +140,6 @@ class Table extends Border unset($this->firstRow->borderInsideHSize); $this->firstRow->setStyleByArray($firstRowStyle); } - - if (!is_null($tableStyle) && is_array($tableStyle)) { - $this->setStyleByArray($tableStyle); - } } /** @@ -162,36 +161,20 @@ class Table extends Border { if (!is_null($this->shading)) { return $this->shading->getFill(); - } else { - return null; } + + return null; } /** * Set background * * @param string $value - * @return \PhpOffice\PhpWord\Style\Table + * @return self */ public function setBgColor($value = null) { $this->setShading(array('fill' => $value)); - } - - /** - * Set TLRBHV Border Size - * - * @param int $value Border size in eighths of a point (1/8 point) - * @return self - */ - public function setBorderSize($value = null) - { - $this->setBorderTopSize($value); - $this->setBorderLeftSize($value); - $this->setBorderRightSize($value); - $this->setBorderBottomSize($value); - $this->setBorderInsideHSize($value); - $this->setBorderInsideVSize($value); return $this; } @@ -214,19 +197,19 @@ class Table extends Border } /** - * Set TLRBHV Border Color + * Set TLRBHV Border Size * - * @param string $value + * @param int $value Border size in eighths of a point (1/8 point) * @return self */ - public function setBorderColor($value = null) + public function setBorderSize($value = null) { - $this->setBorderTopColor($value); - $this->setBorderLeftColor($value); - $this->setBorderRightColor($value); - $this->setBorderBottomColor($value); - $this->setBorderInsideHColor($value); - $this->setBorderInsideVColor($value); + $this->setBorderTopSize($value); + $this->setBorderLeftSize($value); + $this->setBorderRightSize($value); + $this->setBorderBottomSize($value); + $this->setBorderInsideHSize($value); + $this->setBorderInsideVSize($value); return $this; } @@ -249,13 +232,21 @@ class Table extends Border } /** - * Set border size inside horizontal + * Set TLRBHV Border Color * - * @param int $value + * @param string $value + * @return self */ - public function setBorderInsideHSize($value = null) + public function setBorderColor($value = null) { - $this->borderInsideHSize = $value; + $this->setBorderTopColor($value); + $this->setBorderLeftColor($value); + $this->setBorderRightColor($value); + $this->setBorderBottomColor($value); + $this->setBorderInsideHColor($value); + $this->setBorderInsideVColor($value); + + return $this; } /** @@ -265,37 +256,20 @@ class Table extends Border */ public function getBorderInsideHSize() { - return (isset($this->borderInsideHSize)) ? $this->borderInsideHSize : null; + return isset($this->borderInsideHSize) ? $this->borderInsideHSize : null; } /** - * Set border size inside vertical + * Set border size inside horizontal * * @param int $value + * @return self */ - public function setBorderInsideVSize($value = null) + public function setBorderInsideHSize($value = null) { - $this->borderInsideVSize = $value; - } + $this->borderInsideHSize = $this->setNumericVal($value, $this->borderInsideHSize); - /** - * Get border size inside vertical - * - * @return int - */ - public function getBorderInsideVSize() - { - return (isset($this->borderInsideVSize)) ? $this->borderInsideVSize : null; - } - - /** - * Set border color inside horizontal - * - * @param string $value - */ - public function setBorderInsideHColor($value = null) - { - $this->borderInsideHColor = $value; + return $this; } /** @@ -305,17 +279,43 @@ class Table extends Border */ public function getBorderInsideHColor() { - return (isset($this->borderInsideHColor)) ? $this->borderInsideHColor : null; + return isset($this->borderInsideHColor) ? $this->borderInsideHColor : null; } /** - * Set border color inside vertical + * Set border color inside horizontal * * @param string $value + * @return self */ - public function setBorderInsideVColor($value = null) + public function setBorderInsideHColor($value = null) { - $this->borderInsideVColor = $value; + $this->borderInsideHColor = $value ; + + return $this; + } + + /** + * Get border size inside vertical + * + * @return int + */ + public function getBorderInsideVSize() + { + return isset($this->borderInsideVSize) ? $this->borderInsideVSize : null; + } + + /** + * Set border size inside vertical + * + * @param int $value + * @return self + */ + public function setBorderInsideVSize($value = null) + { + $this->borderInsideVSize = $this->setNumericVal($value, $this->borderInsideVSize); + + return $this; } /** @@ -325,17 +325,20 @@ class Table extends Border */ public function getBorderInsideVColor() { - return (isset($this->borderInsideVColor)) ? $this->borderInsideVColor : null; + return isset($this->borderInsideVColor) ? $this->borderInsideVColor : null; } /** - * Set cell margin top + * Set border color inside vertical * - * @param int $value + * @param string $value + * @return self */ - public function setCellMarginTop($value = null) + public function setBorderInsideVColor($value = null) { - $this->cellMarginTop = $value; + $this->borderInsideVColor = $value; + + return $this; } /** @@ -349,13 +352,16 @@ class Table extends Border } /** - * Set cell margin left + * Set cell margin top * * @param int $value + * @return self */ - public function setCellMarginLeft($value = null) + public function setCellMarginTop($value = null) { - $this->cellMarginLeft = $value; + $this->cellMarginTop = $this->setNumericVal($value, $this->cellMarginTop); + + return $this; } /** @@ -369,13 +375,16 @@ class Table extends Border } /** - * Set cell margin right + * Set cell margin left * * @param int $value + * @return self */ - public function setCellMarginRight($value = null) + public function setCellMarginLeft($value = null) { - $this->cellMarginRight = $value; + $this->cellMarginLeft = $this->setNumericVal($value, $this->cellMarginLeft); + + return $this; } /** @@ -389,13 +398,16 @@ class Table extends Border } /** - * Set cell margin bottom + * Set cell margin right * * @param int $value + * @return self */ - public function setCellMarginBottom($value = null) + public function setCellMarginRight($value = null) { - $this->cellMarginBottom = $value; + $this->cellMarginRight = $this->setNumericVal($value, $this->cellMarginRight); + + return $this; } /** @@ -409,16 +421,16 @@ class Table extends Border } /** - * Set TLRB cell margin + * Set cell margin bottom * - * @param int $value Margin in twips + * @param int $value + * @return self */ - public function setCellMargin($value = null) + public function setCellMarginBottom($value = null) { - $this->setCellMarginTop($value); - $this->setCellMarginLeft($value); - $this->setCellMarginRight($value); - $this->setCellMarginBottom($value); + $this->cellMarginBottom = $this->setNumericVal($value, $this->cellMarginBottom); + + return $this; } /** @@ -428,25 +440,40 @@ class Table extends Border */ public function getCellMargin() { - return array($this->cellMarginTop, $this->cellMarginLeft, $this->cellMarginRight, $this->cellMarginBottom); + return array( + $this->cellMarginTop, + $this->cellMarginLeft, + $this->cellMarginRight, + $this->cellMarginBottom + ); } /** - * Has margins? + * Set TLRB cell margin + * + * @param int $value Margin in twips + * @return self + */ + public function setCellMargin($value = null) + { + $this->setCellMarginTop($value); + $this->setCellMarginLeft($value); + $this->setCellMarginRight($value); + $this->setCellMarginBottom($value); + + return $this; + } + + /** + * Check if any of the margin is not null * * @return bool */ - public function hasMargins() + public function hasMargin() { - $hasMargins = false; $margins = $this->getCellMargin(); - for ($i = 0; $i < count($margins); $i++) { - if (!is_null($margins[$i])) { - $hasMargins = true; - } - } - return $hasMargins; + return $margins !== array_filter($margins, 'is_null'); } /** diff --git a/src/PhpWord/Writer/HTML/Element/Text.php b/src/PhpWord/Writer/HTML/Element/Text.php index 44740f72..cbabc645 100644 --- a/src/PhpWord/Writer/HTML/Element/Text.php +++ b/src/PhpWord/Writer/HTML/Element/Text.php @@ -145,7 +145,7 @@ class Text extends AbstractElement /** @var \PhpOffice\PhpWord\Element\Text $element Type hint */ $element = $this->element; $style = ''; - if (method_exists($element, 'getParagraphStyle')) { + if (!method_exists($element, 'getParagraphStyle')) { return $style; } diff --git a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php index be3e8463..82465afe 100644 --- a/src/PhpWord/Writer/Word2007/Element/AbstractElement.php +++ b/src/PhpWord/Writer/Word2007/Element/AbstractElement.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\Element\AbstractElement as Element; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\String; use PhpOffice\PhpWord\Shared\XMLWriter; @@ -83,7 +82,6 @@ abstract class AbstractElement * Get element * * @return \PhpOffice\PhpWord\Element\AbstractElement - * @throws \PhpOffice\PhpWord\Exception\Exception */ protected function getElement() { diff --git a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php index c97654be..f8cb2f26 100644 --- a/src/PhpWord/Writer/Word2007/Part/ContentTypes.php +++ b/src/PhpWord/Writer/Word2007/Part/ContentTypes.php @@ -17,7 +17,6 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Part; -use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Shared\XMLWriter; /** @@ -77,7 +76,6 @@ class ContentTypes extends AbstractPart * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter XML Writer * @param array $parts * @param boolean $isDefault - * @throws \PhpOffice\PhpWord\Exception\Exception */ private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault) { diff --git a/src/PhpWord/Writer/Word2007/Style/Cell.php b/src/PhpWord/Writer/Word2007/Style/Cell.php index ce7efb3b..ae2668ae 100644 --- a/src/PhpWord/Writer/Word2007/Style/Cell.php +++ b/src/PhpWord/Writer/Word2007/Style/Cell.php @@ -59,7 +59,7 @@ class Cell extends AbstractStyle $xmlWriter->writeElementIf(!is_null($vAlign), 'w:vAlign', 'w:val', $vAlign); // Border - if ($style->hasBorders()) { + if ($style->hasBorder()) { $xmlWriter->startElement('w:tcBorders'); $styleWriter = new MarginBorder($xmlWriter); diff --git a/src/PhpWord/Writer/Word2007/Style/Section.php b/src/PhpWord/Writer/Word2007/Style/Section.php index 61658e03..5758e3a8 100644 --- a/src/PhpWord/Writer/Word2007/Style/Section.php +++ b/src/PhpWord/Writer/Word2007/Style/Section.php @@ -66,7 +66,7 @@ class Section extends AbstractStyle $xmlWriter->endElement(); // Borders - if ($style->hasBorders()) { + if ($style->hasBorder()) { $xmlWriter->startElement('w:pgBorders'); $xmlWriter->writeAttribute('w:offsetFrom', 'page'); diff --git a/src/PhpWord/Writer/Word2007/Style/Table.php b/src/PhpWord/Writer/Word2007/Style/Table.php index 68a074ea..f6babe6d 100644 --- a/src/PhpWord/Writer/Word2007/Style/Table.php +++ b/src/PhpWord/Writer/Word2007/Style/Table.php @@ -102,7 +102,7 @@ class Table extends AbstractStyle */ private function writeMargin(XMLWriter $xmlWriter, TableStyle $style) { - if ($style->hasMargins()) { + if ($style->hasMargin()) { $xmlWriter->startElement('w:tblCellMar'); $styleWriter = new MarginBorder($xmlWriter); @@ -118,7 +118,7 @@ class Table extends AbstractStyle */ private function writeBorder(XMLWriter $xmlWriter, TableStyle $style) { - if ($style->hasBorders()) { + if ($style->hasBorder()) { $xmlWriter->startElement('w:tblBorders'); $styleWriter = new MarginBorder($xmlWriter);