Split some functions for code quality improvement
This commit is contained in:
parent
b7374f8936
commit
2ab5a42193
@ -18,7 +18,7 @@
|
||||
namespace PhpOffice\PhpWord\Writer\RTF\Element;
|
||||
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Font as FontStyle;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
|
||||
/**
|
||||
@ -64,56 +64,81 @@ class Text extends Element
|
||||
$this->parentWriter->setLastParagraphStyle();
|
||||
}
|
||||
|
||||
if ($fontStyle instanceof Font) {
|
||||
if ($fontStyle->getColor() != null) {
|
||||
$idxColor = array_search($fontStyle->getColor(), $this->parentWriter->getColorTable());
|
||||
if ($fontStyle instanceof FontStyle) {
|
||||
$rtfText .= $this->writeFontStyleBegin($fontStyle);
|
||||
}
|
||||
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
|
||||
$rtfText .= ' ';
|
||||
}
|
||||
$rtfText .= $this->element->getText();
|
||||
if ($fontStyle instanceof FontStyle) {
|
||||
$rtfText .= $this->writeFontStyleEnd($fontStyle);
|
||||
}
|
||||
if (!$this->withoutP) {
|
||||
$rtfText .= '\par' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $rtfText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write font style beginning
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function writeFontStyleBegin(FontStyle $style)
|
||||
{
|
||||
$rtfText = '';
|
||||
if ($style->getColor() != null) {
|
||||
$idxColor = array_search($style->getColor(), $this->parentWriter->getColorTable());
|
||||
if ($idxColor !== false) {
|
||||
$rtfText .= '\cf' . ($idxColor + 1);
|
||||
}
|
||||
} else {
|
||||
$rtfText .= '\cf0';
|
||||
}
|
||||
if ($fontStyle->getName() != null) {
|
||||
$idxFont = array_search($fontStyle->getName(), $this->parentWriter->getFontTable());
|
||||
if ($style->getName() != null) {
|
||||
$idxFont = array_search($style->getName(), $this->parentWriter->getFontTable());
|
||||
if ($idxFont !== false) {
|
||||
$rtfText .= '\f' . $idxFont;
|
||||
}
|
||||
} else {
|
||||
$rtfText .= '\f0';
|
||||
}
|
||||
if ($fontStyle->isBold()) {
|
||||
if ($style->isBold()) {
|
||||
$rtfText .= '\b';
|
||||
}
|
||||
if ($fontStyle->isItalic()) {
|
||||
if ($style->isItalic()) {
|
||||
$rtfText .= '\i';
|
||||
}
|
||||
if ($fontStyle->getSize()) {
|
||||
$rtfText .= '\fs' . ($fontStyle->getSize() * 2);
|
||||
if ($style->getSize()) {
|
||||
$rtfText .= '\fs' . ($style->getSize() * 2);
|
||||
}
|
||||
}
|
||||
if ($this->parentWriter->getLastParagraphStyle() != '' || $fontStyle) {
|
||||
$rtfText .= ' ';
|
||||
}
|
||||
$rtfText .= $this->element->getText();
|
||||
|
||||
if ($fontStyle instanceof Font) {
|
||||
return $rtfText;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write font style ending
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function writeFontStyleEnd(FontStyle $style)
|
||||
{
|
||||
$rtfText = '';
|
||||
$rtfText .= '\cf0';
|
||||
$rtfText .= '\f0';
|
||||
|
||||
if ($fontStyle->isBold()) {
|
||||
if ($style->isBold()) {
|
||||
$rtfText .= '\b0';
|
||||
}
|
||||
if ($fontStyle->isItalic()) {
|
||||
if ($style->isItalic()) {
|
||||
$rtfText .= '\i0';
|
||||
}
|
||||
if ($fontStyle->getSize()) {
|
||||
if ($style->getSize()) {
|
||||
$rtfText .= '\fs' . (PhpWord::DEFAULT_FONT_SIZE * 2);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->withoutP) {
|
||||
$rtfText .= '\par' . PHP_EOL;
|
||||
}
|
||||
return $rtfText;
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,9 @@
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\Word2007\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Style\Cell;
|
||||
use PhpOffice\PhpWord\Element\Cell as CellElement;
|
||||
use PhpOffice\PhpWord\Element\Row as RowElement;
|
||||
use PhpOffice\PhpWord\Style\Cell as CellStyle;
|
||||
use PhpOffice\PhpWord\Style\Table as TableStyle;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Cell as CellStyleWriter;
|
||||
use PhpOffice\PhpWord\Writer\Word2007\Style\Table as TableStyleWriter;
|
||||
@ -89,7 +91,17 @@ class Table extends Element
|
||||
|
||||
// Table rows
|
||||
for ($i = 0; $i < $rowCount; $i++) {
|
||||
$row = $rows[$i];
|
||||
$this->writeRow($rows[$i]);
|
||||
}
|
||||
$this->xmlWriter->endElement();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write row
|
||||
*/
|
||||
private function writeRow(RowElement $row)
|
||||
{
|
||||
$height = $row->getHeight();
|
||||
$rowStyle = $row->getStyle();
|
||||
|
||||
@ -115,15 +127,25 @@ class Table extends Element
|
||||
$this->xmlWriter->endElement();
|
||||
}
|
||||
foreach ($row->getCells() as $cell) {
|
||||
$this->writeCell($cell);
|
||||
}
|
||||
$this->xmlWriter->endElement(); // w:tr
|
||||
}
|
||||
|
||||
/**
|
||||
* Write cell
|
||||
*/
|
||||
private function writeCell(CellElement $cell)
|
||||
{
|
||||
$cellStyle = $cell->getStyle();
|
||||
$width = $cell->getWidth();
|
||||
|
||||
$this->xmlWriter->startElement('w:tc');
|
||||
$this->xmlWriter->startElement('w:tcPr');
|
||||
$this->xmlWriter->startElement('w:tcW');
|
||||
$this->xmlWriter->writeAttribute('w:w', $width);
|
||||
$this->xmlWriter->writeAttribute('w:w', $cell->getWidth());
|
||||
$this->xmlWriter->writeAttribute('w:type', 'dxa');
|
||||
$this->xmlWriter->endElement(); // w:tcW
|
||||
if ($cellStyle instanceof Cell) {
|
||||
if ($cellStyle instanceof CellStyle) {
|
||||
$styleWriter = new CellStyleWriter($this->xmlWriter, $cellStyle);
|
||||
$styleWriter->write();
|
||||
}
|
||||
@ -131,9 +153,4 @@ class Table extends Element
|
||||
$this->parentWriter->writeContainerElements($this->xmlWriter, $cell);
|
||||
$this->xmlWriter->endElement(); // w:tc
|
||||
}
|
||||
$this->xmlWriter->endElement(); // w:tr
|
||||
}
|
||||
$this->xmlWriter->endElement();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,9 +17,10 @@
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\Word2007\Part;
|
||||
|
||||
use PhpOffice\PhpWord\Shared\XMLWriter;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Style\Numbering as NumberingStyle;
|
||||
use PhpOffice\PhpWord\Style\NumberingLevel;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
|
||||
/**
|
||||
* Word2007 numbering part writer: word/numbering.xml
|
||||
@ -66,16 +67,44 @@ class Numbering extends AbstractPart
|
||||
$xmlWriter->endElement(); // w:multiLevelType
|
||||
|
||||
if (is_array($levels)) {
|
||||
foreach ($levels as $levelNum => $levelObject) {
|
||||
if ($levelObject instanceof NumberingLevel) {
|
||||
$tabPos = $levelObject->getTabPos();
|
||||
$left = $levelObject->getLeft();
|
||||
$hanging = $levelObject->getHanging();
|
||||
$font = $levelObject->getFont();
|
||||
$hint = $levelObject->getHint();
|
||||
foreach ($levels as $level) {
|
||||
$this->writeLevel($xmlWriter, $level);
|
||||
}
|
||||
}
|
||||
$xmlWriter->endElement(); // w:abstractNum
|
||||
}
|
||||
}
|
||||
|
||||
// Numbering definition instances
|
||||
foreach ($styles as $style) {
|
||||
if ($style instanceof NumberingStyle) {
|
||||
$xmlWriter->startElement('w:num');
|
||||
$xmlWriter->writeAttribute('w:numId', $style->getNumId());
|
||||
$xmlWriter->startElement('w:abstractNumId');
|
||||
$xmlWriter->writeAttribute('w:val', $style->getNumId());
|
||||
$xmlWriter->endElement(); // w:abstractNumId
|
||||
$xmlWriter->endElement(); // w:num
|
||||
}
|
||||
}
|
||||
|
||||
$xmlWriter->endElement(); // w:numbering
|
||||
|
||||
return $xmlWriter->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write level
|
||||
*/
|
||||
private function writeLevel(XMLWriter $xmlWriter, NumberingLevel $level)
|
||||
{
|
||||
$tabPos = $level->getTabPos();
|
||||
$left = $level->getLeft();
|
||||
$hanging = $level->getHanging();
|
||||
$font = $level->getFont();
|
||||
$hint = $level->getHint();
|
||||
|
||||
$xmlWriter->startElement('w:lvl');
|
||||
$xmlWriter->writeAttribute('w:ilvl', $levelNum);
|
||||
$xmlWriter->writeAttribute('w:ilvl', $level->getLevel());
|
||||
|
||||
// Numbering level properties
|
||||
$properties = array(
|
||||
@ -88,9 +117,9 @@ class Numbering extends AbstractPart
|
||||
);
|
||||
foreach ($properties as $property => $nodeName) {
|
||||
$getMethod = "get{$property}";
|
||||
if (!is_null($levelObject->$getMethod())) {
|
||||
if (!is_null($level->$getMethod())) {
|
||||
$xmlWriter->startElement("w:{$nodeName}");
|
||||
$xmlWriter->writeAttribute('w:val', $levelObject->$getMethod());
|
||||
$xmlWriter->writeAttribute('w:val', $level->$getMethod());
|
||||
$xmlWriter->endElement(); // w:start
|
||||
}
|
||||
}
|
||||
@ -136,28 +165,6 @@ class Numbering extends AbstractPart
|
||||
}
|
||||
$xmlWriter->endElement(); // w:lvl
|
||||
}
|
||||
}
|
||||
}
|
||||
$xmlWriter->endElement(); // w:abstractNum
|
||||
}
|
||||
}
|
||||
|
||||
// Numbering definition instances
|
||||
foreach ($styles as $style) {
|
||||
if ($style instanceof NumberingStyle) {
|
||||
$xmlWriter->startElement('w:num');
|
||||
$xmlWriter->writeAttribute('w:numId', $style->getNumId());
|
||||
$xmlWriter->startElement('w:abstractNumId');
|
||||
$xmlWriter->writeAttribute('w:val', $style->getNumId());
|
||||
$xmlWriter->endElement(); // w:abstractNumId
|
||||
$xmlWriter->endElement(); // w:num
|
||||
}
|
||||
}
|
||||
|
||||
$xmlWriter->endElement(); // w:numbering
|
||||
|
||||
return $xmlWriter->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get random hexadecimal number value
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user