Refactoring based on Scrutinizer recommendation
This commit is contained in:
parent
0c1e47d7a7
commit
51d69a44c6
|
|
@ -34,7 +34,7 @@ class Image extends Element
|
||||||
*/
|
*/
|
||||||
public function write()
|
public function write()
|
||||||
{
|
{
|
||||||
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Text) {
|
if (!$this->element instanceof \PhpOffice\PhpWord\Element\Image) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,22 +58,21 @@ class Text extends Element
|
||||||
$fontStyle = $styleWriter->write();
|
$fontStyle = $styleWriter->write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$openingTags = '';
|
||||||
|
$endingTags = '';
|
||||||
if ($hasParagraphStyle) {
|
if ($hasParagraphStyle) {
|
||||||
$attribute = $pStyleIsObject ? 'style' : 'class';
|
$attribute = $pStyleIsObject ? 'style' : 'class';
|
||||||
$html .= "<p {$attribute}=\"{$paragraphStyle}\">";
|
$openingTags = "<p {$attribute}=\"{$paragraphStyle}\">";
|
||||||
|
$endingTags = '</p>' . PHP_EOL;
|
||||||
}
|
}
|
||||||
if ($fontStyle) {
|
if ($fontStyle) {
|
||||||
$attribute = $fontStyleIsObject ? 'style' : 'class';
|
$attribute = $fontStyleIsObject ? 'style' : 'class';
|
||||||
$html .= "<span {$attribute}=\"{$fontStyle}\">";
|
$openingTags = $openingTags . "<span {$attribute}=\"{$fontStyle}\">";
|
||||||
}
|
$endingTags = '</span>' . $endingTags;
|
||||||
$html .= htmlspecialchars($this->element->getText());
|
|
||||||
if ($fontStyle) {
|
|
||||||
$html .= '</span>';
|
|
||||||
}
|
|
||||||
if ($hasParagraphStyle) {
|
|
||||||
$html .= '</p>' . PHP_EOL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$html = $openingTags . htmlspecialchars($this->element->getText()) . $endingTags;
|
||||||
|
|
||||||
return $html;
|
return $html;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Element\Footnote as FootnoteElement;
|
||||||
|
use PhpOffice\PhpWord\Element\TextRun as TextRunElement;
|
||||||
use PhpOffice\PhpWord\Style\Paragraph;
|
use PhpOffice\PhpWord\Style\Paragraph;
|
||||||
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
|
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
|
||||||
|
|
||||||
|
|
@ -34,7 +36,7 @@ class TextRun extends Element
|
||||||
*/
|
*/
|
||||||
public function write()
|
public function write()
|
||||||
{
|
{
|
||||||
if (!$this->element instanceof \PhpOffice\PhpWord\Element\TextRun) {
|
if (!($this->element instanceof TextRunElement || $this->element instanceof FootnoteElement)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,40 +34,48 @@ class Font extends AbstractStyle
|
||||||
*/
|
*/
|
||||||
public function write()
|
public function write()
|
||||||
{
|
{
|
||||||
if (!($this->style instanceof \PhpOffice\PhpWord\Style\Font)) {
|
if (!$this->style instanceof \PhpOffice\PhpWord\Style\Font) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$font = $this->style->getName();
|
||||||
|
$size = $this->style->getSize();
|
||||||
|
$color = $this->style->getColor();
|
||||||
|
$fgColor = $this->style->getFgColor();
|
||||||
|
$underline = $this->style->getUnderline() != FontStyle::UNDERLINE_NONE;
|
||||||
|
$lineThrough = $this->style->isStrikethrough() || $this->style->isDoubleStrikethrough();
|
||||||
|
|
||||||
$css = array();
|
$css = array();
|
||||||
if (PhpWord::DEFAULT_FONT_NAME != $this->style->getName()) {
|
|
||||||
$css['font-family'] = "'" . $this->style->getName() . "'";
|
$css['font-family'] = $this->getValueIf($font != PhpWord::DEFAULT_FONT_NAME, "'{$font}'");
|
||||||
}
|
$css['font-size'] = $this->getValueIf($size != PhpWord::DEFAULT_FONT_SIZE, "{$size}pt");
|
||||||
if (PhpWord::DEFAULT_FONT_SIZE != $this->style->getSize()) {
|
$css['color'] = $this->getValueIf($color != PhpWord::DEFAULT_FONT_COLOR, "#{$color}");
|
||||||
$css['font-size'] = $this->style->getSize() . 'pt';
|
$css['background'] = $this->getValueIf($fgColor != '', $fgColor);
|
||||||
}
|
$css['font-weight'] = $this->getValueIf($this->style->isBold(), 'bold');
|
||||||
if (PhpWord::DEFAULT_FONT_COLOR != $this->style->getColor()) {
|
$css['font-style'] = $this->getValueIf($this->style->isItalic(), 'italic');
|
||||||
$css['color'] = '#' . $this->style->getColor();
|
|
||||||
}
|
$css['text-decoration'] = '';
|
||||||
$css['background'] = $this->style->getFgColor();
|
$css['text-decoration'] .= $this->getValueIf($underline, 'underline ');
|
||||||
if ($this->style->isBold()) {
|
$css['text-decoration'] .= $this->getValueIf($lineThrough, 'line-through ');
|
||||||
$css['font-weight'] = 'bold';
|
|
||||||
}
|
|
||||||
if ($this->style->isItalic()) {
|
|
||||||
$css['font-style'] = 'italic';
|
|
||||||
}
|
|
||||||
if ($this->style->isSuperScript()) {
|
if ($this->style->isSuperScript()) {
|
||||||
$css['vertical-align'] = 'super';
|
$css['vertical-align'] = 'super';
|
||||||
} elseif ($this->style->isSubScript()) {
|
} elseif ($this->style->isSubScript()) {
|
||||||
$css['vertical-align'] = 'sub';
|
$css['vertical-align'] = 'sub';
|
||||||
}
|
}
|
||||||
$css['text-decoration'] = '';
|
|
||||||
if ($this->style->getUnderline() != FontStyle::UNDERLINE_NONE) {
|
|
||||||
$css['text-decoration'] .= 'underline ';
|
|
||||||
}
|
|
||||||
if ($this->style->isStrikethrough()) {
|
|
||||||
$css['text-decoration'] .= 'line-through ';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->assembleCss($css);
|
return $this->assembleCss($css);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value if ...
|
||||||
|
*
|
||||||
|
* @param bool $condition
|
||||||
|
* @param string $value
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function getValueIf($condition, $value)
|
||||||
|
{
|
||||||
|
return $condition ? $value : '';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,9 @@
|
||||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
|
||||||
use PhpOffice\PhpWord\Style;
|
use PhpOffice\PhpWord\Style;
|
||||||
|
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||||
|
use PhpOffice\PhpWord\Style\Paragraph as ParagraphStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text element RTF writer
|
* Text element RTF writer
|
||||||
|
|
@ -51,15 +52,7 @@ class Text extends Element
|
||||||
|
|
||||||
if ($paragraphStyle && !$this->withoutP) {
|
if ($paragraphStyle && !$this->withoutP) {
|
||||||
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
|
if ($this->parentWriter->getLastParagraphStyle() != $this->element->getParagraphStyle()) {
|
||||||
$rtfText .= '\pard\nowidctlpar';
|
$rtfText .= $this->writeParagraphStyle($paragraphStyle);
|
||||||
if ($paragraphStyle->getSpaceAfter() != null) {
|
|
||||||
$rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
|
|
||||||
}
|
|
||||||
if ($paragraphStyle->getAlign() != null) {
|
|
||||||
if ($paragraphStyle->getAlign() == 'center') {
|
|
||||||
$rtfText .= '\qc';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
|
$this->parentWriter->setLastParagraphStyle($this->element->getParagraphStyle());
|
||||||
} else {
|
} else {
|
||||||
$this->parentWriter->setLastParagraphStyle();
|
$this->parentWriter->setLastParagraphStyle();
|
||||||
|
|
@ -85,6 +78,26 @@ class Text extends Element
|
||||||
return $rtfText;
|
return $rtfText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write paragraph style
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
private function writeParagraphStyle(ParagraphStyle $paragraphStyle)
|
||||||
|
{
|
||||||
|
$rtfText = '\pard\nowidctlpar';
|
||||||
|
if ($paragraphStyle->getSpaceAfter() != null) {
|
||||||
|
$rtfText .= '\sa' . $paragraphStyle->getSpaceAfter();
|
||||||
|
}
|
||||||
|
if ($paragraphStyle->getAlign() != null) {
|
||||||
|
if ($paragraphStyle->getAlign() == 'center') {
|
||||||
|
$rtfText .= '\qc';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $rtfText;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write font style beginning
|
* Write font style beginning
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -125,6 +125,8 @@ class TOC extends Element
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write style
|
* Write style
|
||||||
|
*
|
||||||
|
* @param int $indent
|
||||||
*/
|
*/
|
||||||
private function writeStyle($indent)
|
private function writeStyle($indent)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue