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