Refactor: Reduce cyclomatic complexity
This commit is contained in:
parent
fbce1c8fc9
commit
2086e9e262
|
|
@ -488,49 +488,23 @@ class DocumentProperties
|
|||
*/
|
||||
public static function convertProperty($propertyValue, $propertyType)
|
||||
{
|
||||
switch ($propertyType) {
|
||||
case 'empty': // Empty
|
||||
$conversion = self::getConversion($propertyType);
|
||||
|
||||
switch ($conversion) {
|
||||
case 'empty': // Empty
|
||||
return '';
|
||||
case 'null': // Null
|
||||
case 'null': // Null
|
||||
return null;
|
||||
case 'i1': // 1-Byte Signed Integer
|
||||
case 'i2': // 2-Byte Signed Integer
|
||||
case 'i4': // 4-Byte Signed Integer
|
||||
case 'i8': // 8-Byte Signed Integer
|
||||
case 'int': // Integer
|
||||
case 'int': // Signed integer
|
||||
return (int) $propertyValue;
|
||||
case 'ui1': // 1-Byte Unsigned Integer
|
||||
case 'ui2': // 2-Byte Unsigned Integer
|
||||
case 'ui4': // 4-Byte Unsigned Integer
|
||||
case 'ui8': // 8-Byte Unsigned Integer
|
||||
case 'uint': // Unsigned Integer
|
||||
case 'uint': // Unsigned integer
|
||||
return abs((int) $propertyValue);
|
||||
case 'r4': // 4-Byte Real Number
|
||||
case 'r8': // 8-Byte Real Number
|
||||
case 'decimal': // Decimal
|
||||
case 'float': // Float
|
||||
return (float) $propertyValue;
|
||||
case 'date': // Date and Time
|
||||
case 'filetime': // File Time
|
||||
case 'date': // Date
|
||||
return strtotime($propertyValue);
|
||||
case 'bool': // Boolean
|
||||
case 'bool': // Boolean
|
||||
return ($propertyValue == 'true') ? true : false;
|
||||
case 'lpstr': // LPSTR
|
||||
case 'lpwstr': // LPWSTR
|
||||
case 'bstr': // Basic String
|
||||
case 'cy': // Currency
|
||||
case 'error': // Error Status Code
|
||||
case 'vector': // Vector
|
||||
case 'array': // Array
|
||||
case 'blob': // Binary Blob
|
||||
case 'oblob': // Binary Blob Object
|
||||
case 'stream': // Binary Stream
|
||||
case 'ostream': // Binary Stream Object
|
||||
case 'storage': // Binary Storage
|
||||
case 'ostorage': // Binary Storage Object
|
||||
case 'vstream': // Binary Versioned Stream
|
||||
case 'clsid': // Class ID
|
||||
case 'cf': // Clipboard Data
|
||||
return $propertyValue;
|
||||
}
|
||||
|
||||
return $propertyValue;
|
||||
|
|
@ -569,10 +543,36 @@ class DocumentProperties
|
|||
*/
|
||||
private function setValue($value, $default)
|
||||
{
|
||||
if (is_null($value) || $value == '') {
|
||||
if ($value === null || $value == '') {
|
||||
$value = $default;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get conversion model depending on property type
|
||||
*
|
||||
* @param string $propertyType
|
||||
* @return string
|
||||
*/
|
||||
private static function getConversion($propertyType)
|
||||
{
|
||||
$conversions = array(
|
||||
'empty' => array('empty'),
|
||||
'null' => array('null'),
|
||||
'int' => array('i1', 'i2', 'i4', 'i8', 'int'),
|
||||
'uint' => array('ui1', 'ui2', 'ui4', 'ui8', 'uint'),
|
||||
'float' => array('r4', 'r8', 'decimal'),
|
||||
'bool' => array('bool'),
|
||||
'date' => array('date', 'filetime'),
|
||||
);
|
||||
foreach ($conversions as $conversion => $types) {
|
||||
if (in_array($propertyType, $types)) {
|
||||
return $conversion;
|
||||
}
|
||||
}
|
||||
|
||||
return 'string';
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@
|
|||
namespace PhpOffice\PhpWord\Reader\ODText;
|
||||
|
||||
use PhpOffice\PhpWord\Reader\Word2007\AbstractPart as Word2007AbstractPart;
|
||||
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||
|
||||
/**
|
||||
* Abstract part reader
|
||||
|
|
@ -28,75 +27,4 @@ use PhpOffice\PhpWord\Shared\XMLReader;
|
|||
*/
|
||||
abstract class AbstractPart extends Word2007AbstractPart
|
||||
{
|
||||
/**
|
||||
* Read w:p (override)
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||
* @param \DOMElement $domNode
|
||||
* @param mixed $parent
|
||||
* @param string $docPart
|
||||
*
|
||||
* @todo Get font style for preserve text
|
||||
*/
|
||||
protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:r (override)
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||
* @param \DOMElement $domNode
|
||||
* @param mixed $parent
|
||||
* @param string $docPart
|
||||
* @param mixed $paragraphStyle
|
||||
*/
|
||||
protected function readRun(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart, $paragraphStyle = null)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:tbl (override)
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||
* @param \DOMElement $domNode
|
||||
* @param mixed $parent
|
||||
* @param string $docPart
|
||||
*/
|
||||
protected function readTable(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:pPr (override)
|
||||
*/
|
||||
protected function readParagraphStyle(XMLReader $xmlReader, \DOMElement $domNode)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:rPr (override)
|
||||
*/
|
||||
protected function readFontStyle(XMLReader $xmlReader, \DOMElement $domNode)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:tblPr (override)
|
||||
*/
|
||||
protected function readTableStyle(XMLReader $xmlReader, \DOMElement $domNode)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Read style definition (override)
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||
* @param \DOMElement $parentNode
|
||||
* @param array $styleDefs
|
||||
* @return array
|
||||
*/
|
||||
protected function readStyleDefs(XMLReader $xmlReader, \DOMElement $parentNode = null, $styleDefs = array())
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ abstract class AbstractPart
|
|||
*
|
||||
* @todo Get font style for preserve text
|
||||
*/
|
||||
protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart)
|
||||
protected function readParagraph(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart = 'document')
|
||||
{
|
||||
// Paragraph style
|
||||
$paragraphStyle = null;
|
||||
|
|
@ -248,7 +248,7 @@ abstract class AbstractPart
|
|||
* @param mixed $parent
|
||||
* @param string $docPart
|
||||
*/
|
||||
protected function readTable(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart)
|
||||
protected function readTable(XMLReader $xmlReader, \DOMElement $domNode, &$parent, $docPart = 'document')
|
||||
{
|
||||
// Table style
|
||||
$tblStyle = null;
|
||||
|
|
|
|||
|
|
@ -19,14 +19,23 @@ namespace PhpOffice\PhpWord\Reader\Word2007;
|
|||
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||
use PhpOffice\PhpWord\Element\Section;
|
||||
|
||||
/**
|
||||
* Document reader
|
||||
*
|
||||
* @since 0.10.0
|
||||
* @SuppressWarnings(PHPMD.UnusedPrivateMethod) For readWPNode
|
||||
*/
|
||||
class Document extends AbstractPart
|
||||
{
|
||||
/**
|
||||
* PhpWord object
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\PhpWord
|
||||
*/
|
||||
private $phpWord;
|
||||
|
||||
/**
|
||||
* Read document.xml
|
||||
*
|
||||
|
|
@ -34,45 +43,18 @@ class Document extends AbstractPart
|
|||
*/
|
||||
public function read(PhpWord &$phpWord)
|
||||
{
|
||||
$this->phpWord = $phpWord;
|
||||
$xmlReader = new XMLReader();
|
||||
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
|
||||
$readMethods = array('w:p' => 'readWPNode', 'w:tbl' => 'readTable', 'w:sectPr' => 'readWSectPrNode');
|
||||
|
||||
$nodes = $xmlReader->getElements('w:body/*');
|
||||
if ($nodes->length > 0) {
|
||||
$section = $phpWord->addSection();
|
||||
$section = $this->phpWord->addSection();
|
||||
foreach ($nodes as $node) {
|
||||
switch ($node->nodeName) {
|
||||
|
||||
case 'w:p': // Paragraph
|
||||
// Page break
|
||||
// @todo <w:lastRenderedPageBreak>
|
||||
if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') {
|
||||
$section->addPageBreak(); // PageBreak
|
||||
}
|
||||
|
||||
// Paragraph
|
||||
$this->readParagraph($xmlReader, $node, $section, 'document');
|
||||
// Section properties
|
||||
if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
|
||||
$settingsNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
|
||||
if (!is_null($settingsNode)) {
|
||||
$settings = $this->readSectionStyle($xmlReader, $settingsNode);
|
||||
$section->setSettings($settings);
|
||||
$this->readHeaderFooter($settings, $section);
|
||||
}
|
||||
$section = $phpWord->addSection();
|
||||
}
|
||||
break;
|
||||
|
||||
case 'w:tbl': // Table
|
||||
$this->readTable($xmlReader, $node, $section, 'document');
|
||||
break;
|
||||
|
||||
case 'w:sectPr': // Last section
|
||||
$settings = $this->readSectionStyle($xmlReader, $node);
|
||||
$section->setSettings($settings);
|
||||
$this->readHeaderFooter($settings, $section);
|
||||
break;
|
||||
if (array_key_exists($node->nodeName, $readMethods)) {
|
||||
$readMethod = $readMethods[$node->nodeName];
|
||||
$this->$readMethod($xmlReader, $node, $section);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -84,14 +66,16 @@ class Document extends AbstractPart
|
|||
* @param array $settings
|
||||
* @param \PhpOffice\PhpWord\Element\Section $section
|
||||
*/
|
||||
private function readHeaderFooter($settings, &$section)
|
||||
private function readHeaderFooter($settings, Section &$section)
|
||||
{
|
||||
$readMethods = array('w:p' => 'readParagraph', 'w:tbl' => 'readTable');
|
||||
|
||||
if (is_array($settings) && array_key_exists('hf', $settings)) {
|
||||
foreach ($settings['hf'] as $rId => $hfSetting) {
|
||||
if (array_key_exists($rId, $this->rels['document'])) {
|
||||
list($hfType, $xmlFile, $docPart) = array_values($this->rels['document'][$rId]);
|
||||
$method = "add{$hfType}";
|
||||
$hfObject = $section->$method($hfSetting['type']);
|
||||
$addMethod = "add{$hfType}";
|
||||
$hfObject = $section->$addMethod($hfSetting['type']);
|
||||
|
||||
// Read header/footer content
|
||||
$xmlReader = new XMLReader();
|
||||
|
|
@ -99,15 +83,9 @@ class Document extends AbstractPart
|
|||
$nodes = $xmlReader->getElements('*');
|
||||
if ($nodes->length > 0) {
|
||||
foreach ($nodes as $node) {
|
||||
switch ($node->nodeName) {
|
||||
|
||||
case 'w:p': // Paragraph
|
||||
$this->readParagraph($xmlReader, $node, $hfObject, $docPart);
|
||||
break;
|
||||
|
||||
case 'w:tbl': // Table
|
||||
$this->readTable($xmlReader, $node, $hfObject, $docPart);
|
||||
break;
|
||||
if (array_key_exists($node->nodeName, $readMethods)) {
|
||||
$readMethod = $readMethods[$node->nodeName];
|
||||
$this->$readMethod($xmlReader, $node, $hfObject, $docPart);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -157,4 +135,47 @@ class Document extends AbstractPart
|
|||
|
||||
return $styles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:p node
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||
* @param \DOMElement $node
|
||||
* @param \PhpOffice\PhpWord\Element\Section $section
|
||||
*
|
||||
* @todo <w:lastRenderedPageBreak>
|
||||
*/
|
||||
private function readWPNode(XMLReader $xmlReader, \DOMElement $node, Section &$section)
|
||||
{
|
||||
// Page break
|
||||
if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') {
|
||||
$section->addPageBreak(); // PageBreak
|
||||
}
|
||||
|
||||
// Paragraph
|
||||
$this->readParagraph($xmlReader, $node, $section);
|
||||
|
||||
// Section properties
|
||||
if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
|
||||
$sectPrNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
|
||||
$this->readWSectPrNode($xmlReader, $sectPrNode, $section);
|
||||
$section = $this->phpWord->addSection();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read w:sectPr node
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
|
||||
* @param \DOMElement $node
|
||||
* @param \PhpOffice\PhpWord\Element\Section $section
|
||||
*/
|
||||
private function readWSectPrNode(XMLReader $xmlReader, \DOMElement $node, Section &$section)
|
||||
{
|
||||
if ($node !== null) {
|
||||
$settings = $this->readSectionStyle($xmlReader, $node);
|
||||
$section->setSettings($settings);
|
||||
$this->readHeaderFooter($settings, $section);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,18 +33,18 @@ use PhpOffice\PhpWord\Writer\RTF\Element\Container;
|
|||
class RTF extends AbstractWriter implements WriterInterface
|
||||
{
|
||||
/**
|
||||
* Color register
|
||||
* Font table
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $colorTable;
|
||||
private $fontTable = array();
|
||||
|
||||
/**
|
||||
* Font register
|
||||
* Color table
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $fontTable;
|
||||
private $colorTable = array();
|
||||
|
||||
/**
|
||||
* Last paragraph style
|
||||
|
|
@ -81,14 +81,6 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
$this->cleanupTempFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get color table
|
||||
*/
|
||||
public function getColorTable()
|
||||
{
|
||||
return $this->colorTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get font table
|
||||
*/
|
||||
|
|
@ -97,6 +89,14 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
return $this->fontTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get color table
|
||||
*/
|
||||
public function getColorTable()
|
||||
{
|
||||
return $this->colorTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last paragraph style
|
||||
*/
|
||||
|
|
@ -122,8 +122,7 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
*/
|
||||
private function writeDocument()
|
||||
{
|
||||
$this->fontTable = $this->populateFontTable();
|
||||
$this->colorTable = $this->populateColorTable();
|
||||
$this->populateTableGroups();
|
||||
|
||||
// Set the default character set
|
||||
$content = '{\rtf1';
|
||||
|
|
@ -148,6 +147,8 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
$content .= ';}' . PHP_EOL;
|
||||
|
||||
$content .= '{\*\generator PhpWord;}' . PHP_EOL; // Set the generator
|
||||
|
||||
// Document formatting
|
||||
$content .= '\viewkind4'; // Set the view mode of the document
|
||||
$content .= '\uc1'; // Set the numberof bytes that follows a unicode character
|
||||
$content .= '\pard'; // Resets to default paragraph properties.
|
||||
|
|
@ -155,7 +156,7 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
$content .= '\lang1036'; // Applies a language to a text run (1036 : French (France))
|
||||
$content .= '\kerning1'; // Point size (in half-points) above which to kern character pairs
|
||||
$content .= '\fs' . (Settings::getDefaultFontSize() * 2); // Set the font size in half-points
|
||||
$content .= PHP_EOL;
|
||||
$content .= PHP_EOL . PHP_EOL;
|
||||
|
||||
// Body
|
||||
$content .= $this->writeContent();
|
||||
|
|
@ -184,102 +185,58 @@ class RTF extends AbstractWriter implements WriterInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Get all fonts
|
||||
*
|
||||
* @return array
|
||||
* Populate font and color table group
|
||||
*/
|
||||
private function populateFontTable()
|
||||
private function populateTableGroups()
|
||||
{
|
||||
$phpWord = $this->getPhpWord();
|
||||
$fontTable = array();
|
||||
$fontTable[] = Settings::getDefaultFontName();
|
||||
$this->fontTable[] = Settings::getDefaultFontName();
|
||||
|
||||
// Browse styles
|
||||
// Search font in styles
|
||||
$styles = Style::getStyles();
|
||||
if (count($styles) > 0) {
|
||||
foreach ($styles as $style) {
|
||||
// Font
|
||||
if ($style instanceof Font) {
|
||||
if (in_array($style->getName(), $fontTable) == false) {
|
||||
$fontTable[] = $style->getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($styles as $style) {
|
||||
$this->pushTableGroupItems($style);
|
||||
}
|
||||
|
||||
// Search all fonts used
|
||||
// Search font in elements
|
||||
$sections = $phpWord->getSections();
|
||||
$countSections = count($sections);
|
||||
if ($countSections > 0) {
|
||||
foreach ($sections as $section) {
|
||||
$elements = $section->getElements();
|
||||
foreach ($elements as $element) {
|
||||
if (method_exists($element, 'getFontStyle')) {
|
||||
$fontStyle = $element->getFontStyle();
|
||||
if ($fontStyle instanceof Font) {
|
||||
if (in_array($fontStyle->getName(), $fontTable) == false) {
|
||||
$fontTable[] = $fontStyle->getName();
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($sections as $section) {
|
||||
$elements = $section->getElements();
|
||||
foreach ($elements as $element) {
|
||||
if (method_exists($element, 'getFontStyle')) {
|
||||
$this->pushTableGroupItems($element->getFontStyle());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $fontTable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all colors
|
||||
* Push font and color table group items
|
||||
*
|
||||
* @return array
|
||||
* @param \PhpOffice\PhpWord\Style\AbstractStyle
|
||||
*/
|
||||
private function populateColorTable()
|
||||
private function pushTableGroupItems($style)
|
||||
{
|
||||
$phpWord = $this->getPhpWord();
|
||||
$defaultFontColor = Settings::DEFAULT_FONT_COLOR;
|
||||
$colorTable = array();
|
||||
|
||||
// Browse styles
|
||||
$styles = Style::getStyles();
|
||||
if (count($styles) > 0) {
|
||||
foreach ($styles as $style) {
|
||||
// Font
|
||||
if ($style instanceof Font) {
|
||||
$color = $style->getColor();
|
||||
$fgcolor = $style->getFgColor();
|
||||
if (!in_array($color, $colorTable) && $color != $defaultFontColor && !empty($color)) {
|
||||
$colorTable[] = $color;
|
||||
}
|
||||
if (!in_array($fgcolor, $colorTable) && $fgcolor != $defaultFontColor && !empty($fgcolor)) {
|
||||
$colorTable[] = $fgcolor;
|
||||
}
|
||||
}
|
||||
}
|
||||
$defaultFont = Settings::getDefaultFontName();
|
||||
$defaultColor = Settings::DEFAULT_FONT_COLOR;
|
||||
if ($style instanceof Font) {
|
||||
$this->pushTableGroupItem($this->fontTable, $style->getName(), $defaultFont);
|
||||
$this->pushTableGroupItem($this->colorTable, $style->getColor(), $defaultColor);
|
||||
$this->pushTableGroupItem($this->colorTable, $style->getFgColor(), $defaultColor);
|
||||
}
|
||||
}
|
||||
|
||||
// Search all fonts used
|
||||
$sections = $phpWord->getSections();
|
||||
$countSections = count($sections);
|
||||
if ($countSections > 0) {
|
||||
foreach ($sections as $section) {
|
||||
$elements = $section->getElements();
|
||||
foreach ($elements as $element) {
|
||||
if (method_exists($element, 'getFontStyle')) {
|
||||
$fontStyle = $element->getFontStyle();
|
||||
if ($fontStyle instanceof Font) {
|
||||
if (in_array($fontStyle->getColor(), $colorTable) == false) {
|
||||
$colorTable[] = $fontStyle->getColor();
|
||||
}
|
||||
if (in_array($fontStyle->getFgColor(), $colorTable) == false) {
|
||||
$colorTable[] = $fontStyle->getFgColor();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Push individual font and color into corresponding table group
|
||||
*
|
||||
* @param array $tableGroup
|
||||
* @param string $item
|
||||
* @param string $default
|
||||
*/
|
||||
private function pushTableGroupItem(&$tableGroup, $item, $default)
|
||||
{
|
||||
if (!in_array($item, $tableGroup) && $item !== null && $item != $default) {
|
||||
$tableGroup[] = $item;
|
||||
}
|
||||
|
||||
return $colorTable;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue