Eliminate a big chunck of duplicated code for reading styles (#2021)
* Eliminate a big chunk of duplicated code for reading styles
This commit is contained in:
parent
b05dc31850
commit
55a41e8cae
|
|
@ -3940,31 +3940,6 @@ parameters:
|
|||
count: 1
|
||||
path: src/PhpSpreadsheet/Reader/Xlsx.php
|
||||
|
||||
-
|
||||
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\:\\:readColor\\(\\) has no return typehint specified\\.$#"
|
||||
count: 1
|
||||
path: src/PhpSpreadsheet/Reader/Xlsx.php
|
||||
|
||||
-
|
||||
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\:\\:readColor\\(\\) has parameter \\$background with no typehint specified\\.$#"
|
||||
count: 1
|
||||
path: src/PhpSpreadsheet/Reader/Xlsx.php
|
||||
|
||||
-
|
||||
message: "#^Method PhpOffice\\\\PhpSpreadsheet\\\\Reader\\\\Xlsx\\:\\:readColor\\(\\) has parameter \\$color with no typehint specified\\.$#"
|
||||
count: 1
|
||||
path: src/PhpSpreadsheet/Reader/Xlsx.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$hex of static method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Color\\:\\:changeBrightness\\(\\) expects string, string\\|null given\\.$#"
|
||||
count: 1
|
||||
path: src/PhpSpreadsheet/Reader/Xlsx.php
|
||||
|
||||
-
|
||||
message: "#^Parameter \\#1 \\$pValue of method PhpOffice\\\\PhpSpreadsheet\\\\Style\\\\Font\\:\\:setSize\\(\\) expects float, string given\\.$#"
|
||||
count: 1
|
||||
path: src/PhpSpreadsheet/Reader/Xlsx.php
|
||||
|
||||
-
|
||||
message: "#^Cannot access property \\$r on SimpleXMLElement\\|null\\.$#"
|
||||
count: 2
|
||||
|
|
|
|||
|
|
@ -27,12 +27,8 @@ use PhpOffice\PhpSpreadsheet\Shared\File;
|
|||
use PhpOffice\PhpSpreadsheet\Shared\Font;
|
||||
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
||||
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Border;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Borders;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Color;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Fill;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Protection;
|
||||
use PhpOffice\PhpSpreadsheet\Style\Style;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
|
|
@ -1570,31 +1566,6 @@ class Xlsx extends BaseReader
|
|||
return $excel;
|
||||
}
|
||||
|
||||
private static function readColor($color, $background = false)
|
||||
{
|
||||
if (isset($color['rgb'])) {
|
||||
return (string) $color['rgb'];
|
||||
} elseif (isset($color['indexed'])) {
|
||||
return Color::indexedColor($color['indexed'] - 7, $background)->getARGB();
|
||||
} elseif (isset($color['theme'])) {
|
||||
if (self::$theme !== null) {
|
||||
$returnColour = self::$theme->getColourByIndex((int) $color['theme']);
|
||||
if (isset($color['tint'])) {
|
||||
$tintAdjust = (float) $color['tint'];
|
||||
$returnColour = Color::changeBrightness($returnColour, $tintAdjust);
|
||||
}
|
||||
|
||||
return 'FF' . $returnColour;
|
||||
}
|
||||
}
|
||||
|
||||
if ($background) {
|
||||
return 'FFFFFFFF';
|
||||
}
|
||||
|
||||
return 'FF000000';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SimpleXMLElement|stdClass $style
|
||||
*/
|
||||
|
|
@ -1604,116 +1575,28 @@ class Xlsx extends BaseReader
|
|||
|
||||
// font
|
||||
if (isset($style->font)) {
|
||||
$docStyle->getFont()->setName((string) $style->font->name['val']);
|
||||
$docStyle->getFont()->setSize((string) $style->font->sz['val']);
|
||||
if (isset($style->font->b)) {
|
||||
$docStyle->getFont()->setBold(!isset($style->font->b['val']) || self::boolean((string) $style->font->b['val']));
|
||||
}
|
||||
if (isset($style->font->i)) {
|
||||
$docStyle->getFont()->setItalic(!isset($style->font->i['val']) || self::boolean((string) $style->font->i['val']));
|
||||
}
|
||||
if (isset($style->font->strike)) {
|
||||
$docStyle->getFont()->setStrikethrough(!isset($style->font->strike['val']) || self::boolean((string) $style->font->strike['val']));
|
||||
}
|
||||
$docStyle->getFont()->getColor()->setARGB(self::readColor($style->font->color));
|
||||
|
||||
if (isset($style->font->u) && !isset($style->font->u['val'])) {
|
||||
$docStyle->getFont()->setUnderline(\PhpOffice\PhpSpreadsheet\Style\Font::UNDERLINE_SINGLE);
|
||||
} elseif (isset($style->font->u, $style->font->u['val'])) {
|
||||
$docStyle->getFont()->setUnderline((string) $style->font->u['val']);
|
||||
}
|
||||
|
||||
if (isset($style->font->vertAlign, $style->font->vertAlign['val'])) {
|
||||
$vertAlign = strtolower((string) $style->font->vertAlign['val']);
|
||||
if ($vertAlign == 'superscript') {
|
||||
$docStyle->getFont()->setSuperscript(true);
|
||||
}
|
||||
if ($vertAlign == 'subscript') {
|
||||
$docStyle->getFont()->setSubscript(true);
|
||||
}
|
||||
}
|
||||
Styles::readFontStyle($docStyle->getFont(), $style->font);
|
||||
}
|
||||
|
||||
// fill
|
||||
if (isset($style->fill)) {
|
||||
if ($style->fill->gradientFill) {
|
||||
/** @var SimpleXMLElement $gradientFill */
|
||||
$gradientFill = $style->fill->gradientFill[0];
|
||||
if (!empty($gradientFill['type'])) {
|
||||
$docStyle->getFill()->setFillType((string) $gradientFill['type']);
|
||||
}
|
||||
$docStyle->getFill()->setRotation((float) ($gradientFill['degree']));
|
||||
$gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
|
||||
$docStyle->getFill()->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
|
||||
$docStyle->getFill()->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
|
||||
} elseif ($style->fill->patternFill) {
|
||||
$patternType = (string) $style->fill->patternFill['patternType'] != '' ? (string) $style->fill->patternFill['patternType'] : Fill::FILL_NONE;
|
||||
$docStyle->getFill()->setFillType($patternType);
|
||||
if ($style->fill->patternFill->fgColor) {
|
||||
$docStyle->getFill()->getStartColor()->setARGB(self::readColor($style->fill->patternFill->fgColor, true));
|
||||
}
|
||||
if ($style->fill->patternFill->bgColor) {
|
||||
$docStyle->getFill()->getEndColor()->setARGB(self::readColor($style->fill->patternFill->bgColor, true));
|
||||
}
|
||||
}
|
||||
Styles::readFillStyle($docStyle->getFill(), $style->fill);
|
||||
}
|
||||
|
||||
// border
|
||||
if (isset($style->border)) {
|
||||
$diagonalUp = self::boolean((string) $style->border['diagonalUp']);
|
||||
$diagonalDown = self::boolean((string) $style->border['diagonalDown']);
|
||||
if (!$diagonalUp && !$diagonalDown) {
|
||||
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_NONE);
|
||||
} elseif ($diagonalUp && !$diagonalDown) {
|
||||
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_UP);
|
||||
} elseif (!$diagonalUp && $diagonalDown) {
|
||||
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_DOWN);
|
||||
} else {
|
||||
$docStyle->getBorders()->setDiagonalDirection(Borders::DIAGONAL_BOTH);
|
||||
}
|
||||
self::readBorder($docStyle->getBorders()->getLeft(), $style->border->left);
|
||||
self::readBorder($docStyle->getBorders()->getRight(), $style->border->right);
|
||||
self::readBorder($docStyle->getBorders()->getTop(), $style->border->top);
|
||||
self::readBorder($docStyle->getBorders()->getBottom(), $style->border->bottom);
|
||||
self::readBorder($docStyle->getBorders()->getDiagonal(), $style->border->diagonal);
|
||||
Styles::readBorderStyle($docStyle->getBorders(), $style->border);
|
||||
}
|
||||
|
||||
// alignment
|
||||
if (isset($style->alignment)) {
|
||||
$docStyle->getAlignment()->setHorizontal((string) $style->alignment['horizontal']);
|
||||
$docStyle->getAlignment()->setVertical((string) $style->alignment['vertical']);
|
||||
|
||||
$textRotation = 0;
|
||||
if ((int) $style->alignment['textRotation'] <= 90) {
|
||||
$textRotation = (int) $style->alignment['textRotation'];
|
||||
} elseif ((int) $style->alignment['textRotation'] > 90) {
|
||||
$textRotation = 90 - (int) $style->alignment['textRotation'];
|
||||
}
|
||||
|
||||
$docStyle->getAlignment()->setTextRotation((int) $textRotation);
|
||||
$docStyle->getAlignment()->setWrapText(self::boolean((string) $style->alignment['wrapText']));
|
||||
$docStyle->getAlignment()->setShrinkToFit(self::boolean((string) $style->alignment['shrinkToFit']));
|
||||
$docStyle->getAlignment()->setIndent((int) ((string) $style->alignment['indent']) > 0 ? (int) ((string) $style->alignment['indent']) : 0);
|
||||
$docStyle->getAlignment()->setReadOrder((int) ((string) $style->alignment['readingOrder']) > 0 ? (int) ((string) $style->alignment['readingOrder']) : 0);
|
||||
Styles::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
|
||||
}
|
||||
|
||||
// protection
|
||||
if (isset($style->protection)) {
|
||||
if (isset($style->protection['locked'])) {
|
||||
if (self::boolean((string) $style->protection['locked'])) {
|
||||
$docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
|
||||
} else {
|
||||
$docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($style->protection['hidden'])) {
|
||||
if (self::boolean((string) $style->protection['hidden'])) {
|
||||
$docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
|
||||
} else {
|
||||
$docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
|
||||
}
|
||||
}
|
||||
Styles::readProtectionLocked($docStyle, $style->protection);
|
||||
Styles::readProtectionHidden($docStyle, $style->protection);
|
||||
}
|
||||
|
||||
// top-level style settings
|
||||
|
|
@ -1722,19 +1605,6 @@ class Xlsx extends BaseReader
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SimpleXMLElement $eleBorder
|
||||
*/
|
||||
private static function readBorder(Border $docBorder, $eleBorder): void
|
||||
{
|
||||
if (isset($eleBorder['style'])) {
|
||||
$docBorder->setBorderStyle((string) $eleBorder['style']);
|
||||
}
|
||||
if (isset($eleBorder->color)) {
|
||||
$docBorder->getColor()->setARGB(self::readColor($eleBorder->color));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SimpleXMLElement | null $is
|
||||
*
|
||||
|
|
@ -1763,7 +1633,7 @@ class Xlsx extends BaseReader
|
|||
$objText->getFont()->setSize((float) $run->rPr->sz['val']);
|
||||
}
|
||||
if (isset($run->rPr->color)) {
|
||||
$objText->getFont()->setColor(new Color(self::readColor($run->rPr->color)));
|
||||
$objText->getFont()->setColor(new Color(Styles::readColor($run->rPr->color)));
|
||||
}
|
||||
if (
|
||||
(isset($run->rPr->b['val']) && self::boolean((string) $run->rPr->b['val'])) ||
|
||||
|
|
@ -1949,11 +1819,17 @@ class Xlsx extends BaseReader
|
|||
}
|
||||
|
||||
if ($xmlWorkbook->workbookProtection['revisionsPassword']) {
|
||||
$excel->getSecurity()->setRevisionsPassword((string) $xmlWorkbook->workbookProtection['revisionsPassword'], true);
|
||||
$excel->getSecurity()->setRevisionsPassword(
|
||||
(string) $xmlWorkbook->workbookProtection['revisionsPassword'],
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
if ($xmlWorkbook->workbookProtection['workbookPassword']) {
|
||||
$excel->getSecurity()->setWorkbookPassword((string) $xmlWorkbook->workbookProtection['workbookPassword'], true);
|
||||
$excel->getSecurity()->setWorkbookPassword(
|
||||
(string) $xmlWorkbook->workbookProtection['workbookPassword'],
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ class Styles extends BaseParserClass
|
|||
$this->cellStyles = $cellStyles;
|
||||
}
|
||||
|
||||
private static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
|
||||
public static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
|
||||
{
|
||||
$fontStyle->setName((string) $fontStyleXml->name['val']);
|
||||
$fontStyle->setSize((float) $fontStyleXml->sz['val']);
|
||||
|
|
@ -52,7 +52,9 @@ class Styles extends BaseParserClass
|
|||
$fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
|
||||
}
|
||||
if (isset($fontStyleXml->strike)) {
|
||||
$fontStyle->setStrikethrough(!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val']));
|
||||
$fontStyle->setStrikethrough(
|
||||
!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val'])
|
||||
);
|
||||
}
|
||||
$fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));
|
||||
|
||||
|
|
@ -84,7 +86,7 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
}
|
||||
|
||||
private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
|
||||
public static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
|
||||
{
|
||||
if ($fillStyleXml->gradientFill) {
|
||||
/** @var SimpleXMLElement $gradientFill */
|
||||
|
|
@ -94,15 +96,20 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
$fillStyle->setRotation((float) ($gradientFill['degree']));
|
||||
$gradientFill->registerXPathNamespace('sml', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
|
||||
$fillStyle->getStartColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
|
||||
$fillStyle->getEndColor()->setARGB(self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
|
||||
$fillStyle->getStartColor()->setARGB(
|
||||
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color)
|
||||
);
|
||||
$fillStyle->getEndColor()->setARGB(
|
||||
self::readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color)
|
||||
);
|
||||
} elseif ($fillStyleXml->patternFill) {
|
||||
$patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : Fill::FILL_NONE;
|
||||
$patternType = (string) $fillStyleXml->patternFill['patternType'] != ''
|
||||
? (string) $fillStyleXml->patternFill['patternType']
|
||||
: Fill::FILL_NONE;
|
||||
|
||||
$fillStyle->setFillType($patternType);
|
||||
if ($fillStyleXml->patternFill->fgColor) {
|
||||
$fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
|
||||
} else {
|
||||
$fillStyle->getStartColor()->setARGB('FF000000');
|
||||
}
|
||||
if ($fillStyleXml->patternFill->bgColor) {
|
||||
$fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
|
||||
|
|
@ -110,7 +117,7 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
}
|
||||
|
||||
private static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
|
||||
public static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
|
||||
{
|
||||
$diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
|
||||
$diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
|
||||
|
|
@ -141,7 +148,7 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
}
|
||||
|
||||
private static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
|
||||
public static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
|
||||
{
|
||||
$alignment->setHorizontal((string) $alignmentXml['horizontal']);
|
||||
$alignment->setVertical((string) $alignmentXml['vertical']);
|
||||
|
|
@ -156,8 +163,12 @@ class Styles extends BaseParserClass
|
|||
$alignment->setTextRotation((int) $textRotation);
|
||||
$alignment->setWrapText(self::boolean((string) $alignmentXml['wrapText']));
|
||||
$alignment->setShrinkToFit(self::boolean((string) $alignmentXml['shrinkToFit']));
|
||||
$alignment->setIndent((int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0);
|
||||
$alignment->setReadOrder((int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0);
|
||||
$alignment->setIndent(
|
||||
(int) ((string) $alignmentXml['indent']) > 0 ? (int) ((string) $alignmentXml['indent']) : 0
|
||||
);
|
||||
$alignment->setReadOrder(
|
||||
(int) ((string) $alignmentXml['readingOrder']) > 0 ? (int) ((string) $alignmentXml['readingOrder']) : 0
|
||||
);
|
||||
}
|
||||
|
||||
private function readStyle(Style $docStyle, $style): void
|
||||
|
|
@ -186,8 +197,8 @@ class Styles extends BaseParserClass
|
|||
|
||||
// protection
|
||||
if (isset($style->protection)) {
|
||||
$this->readProtectionLocked($docStyle, $style);
|
||||
$this->readProtectionHidden($docStyle, $style);
|
||||
self::readProtectionLocked($docStyle, $style);
|
||||
self::readProtectionHidden($docStyle, $style);
|
||||
}
|
||||
|
||||
// top-level style settings
|
||||
|
|
@ -196,7 +207,7 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
}
|
||||
|
||||
private function readProtectionLocked(Style $docStyle, $style): void
|
||||
public static function readProtectionLocked(Style $docStyle, $style): void
|
||||
{
|
||||
if (isset($style->protection['locked'])) {
|
||||
if (self::boolean((string) $style->protection['locked'])) {
|
||||
|
|
@ -207,7 +218,7 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
}
|
||||
|
||||
private function readProtectionHidden(Style $docStyle, $style): void
|
||||
public static function readProtectionHidden(Style $docStyle, $style): void
|
||||
{
|
||||
if (isset($style->protection['hidden'])) {
|
||||
if (self::boolean((string) $style->protection['hidden'])) {
|
||||
|
|
@ -218,7 +229,7 @@ class Styles extends BaseParserClass
|
|||
}
|
||||
}
|
||||
|
||||
private static function readColor($color, $background = false)
|
||||
public static function readColor($color, $background = false)
|
||||
{
|
||||
if (isset($color['rgb'])) {
|
||||
return (string) $color['rgb'];
|
||||
|
|
|
|||
Loading…
Reference in New Issue