<?php
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
> use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Borders;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PhpOffice\PhpSpreadsheet\Style\Style;
use SimpleXMLElement;
> use stdClass;
class Styles extends BaseParserClass
{
/**
* Theme instance.
*
< * @var Theme
> * @var ?Theme
*/
< private static $theme = null;
> private $theme;
> /** @var array */
private $styles = [];
> private $workbookPalette = [];
>
private $cellStyles = [];
> /** @var array */
> /** @var array */
private $styleXml;
> /** @var SimpleXMLElement */
< public function __construct(SimpleXMLElement $styleXml)
> /** @var string */
> private $namespace = '';
>
> public function setNamespace(string $namespace): void
> {
> $this->namespace = $namespace;
> }
>
> public function setWorkbookPalette(array $palette): void
> {
> $this->workbookPalette = $palette;
> }
>
> /**
> * Cast SimpleXMLElement to bool to overcome Scrutinizer problem.
> *
> * @param mixed $value
> */
> private static function castBool($value): bool
> {
> return (bool) $value;
> }
>
> private function getStyleAttributes(SimpleXMLElement $value): SimpleXMLElement
> {
> $attr = null;
> if (self::castBool($value)) {
> $attr = $value->attributes('');
> if ($attr === null || count($attr) === 0) {
> $attr = $value->attributes($this->namespace);
> }
> }
>
> return Xlsx::testSimpleXml($attr);
> }
>
> public function setStyleXml(SimpleXmlElement $styleXml): void
{
$this->styleXml = $styleXml;
}
< public function setStyleBaseData(?Theme $theme = null, $styles = [], $cellStyles = []): void
> public function setTheme(Theme $theme): void
{
< self::$theme = $theme;
> $this->theme = $theme;
> }
>
> public function setStyleBaseData(?Theme $theme = null, array $styles = [], array $cellStyles = []): void
> {
> $this->theme = $theme;
$this->styles = $styles;
$this->cellStyles = $cellStyles;
}
< private static function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
> public function readFontStyle(Font $fontStyle, SimpleXMLElement $fontStyleXml): void
{
< $fontStyle->setName((string) $fontStyleXml->name['val']);
< $fontStyle->setSize((float) $fontStyleXml->sz['val']);
<
> if (isset($fontStyleXml->name)) {
> $attr = $this->getStyleAttributes($fontStyleXml->name);
> if (isset($attr['val'])) {
> $fontStyle->setName((string) $attr['val']);
> }
> }
> if (isset($fontStyleXml->sz)) {
> $attr = $this->getStyleAttributes($fontStyleXml->sz);
> if (isset($attr['val'])) {
> $fontStyle->setSize((float) $attr['val']);
> }
> }
if (isset($fontStyleXml->b)) {
< $fontStyle->setBold(!isset($fontStyleXml->b['val']) || self::boolean((string) $fontStyleXml->b['val']));
> $attr = $this->getStyleAttributes($fontStyleXml->b);
> $fontStyle->setBold(!isset($attr['val']) || self::boolean((string) $attr['val']));
}
if (isset($fontStyleXml->i)) {
< $fontStyle->setItalic(!isset($fontStyleXml->i['val']) || self::boolean((string) $fontStyleXml->i['val']));
> $attr = $this->getStyleAttributes($fontStyleXml->i);
> $fontStyle->setItalic(!isset($attr['val']) || self::boolean((string) $attr['val']));
}
if (isset($fontStyleXml->strike)) {
< $fontStyle->setStrikethrough(!isset($fontStyleXml->strike['val']) || self::boolean((string) $fontStyleXml->strike['val']));
> $attr = $this->getStyleAttributes($fontStyleXml->strike);
> $fontStyle->setStrikethrough(!isset($attr['val']) || self::boolean((string) $attr['val']));
}
< $fontStyle->getColor()->setARGB(self::readColor($fontStyleXml->color));
> $fontStyle->getColor()->setARGB($this->readColor($fontStyleXml->color));
< if (isset($fontStyleXml->u) && !isset($fontStyleXml->u['val'])) {
> if (isset($fontStyleXml->u)) {
> $attr = $this->getStyleAttributes($fontStyleXml->u);
> if (!isset($attr['val'])) {
$fontStyle->setUnderline(Font::UNDERLINE_SINGLE);
< } elseif (isset($fontStyleXml->u, $fontStyleXml->u['val'])) {
< $fontStyle->setUnderline((string) $fontStyleXml->u['val']);
> } else {
> $fontStyle->setUnderline((string) $attr['val']);
}
<
< if (isset($fontStyleXml->vertAlign, $fontStyleXml->vertAlign['val'])) {
< $verticalAlign = strtolower((string) $fontStyleXml->vertAlign['val']);
> }
> if (isset($fontStyleXml->vertAlign)) {
> $attr = $this->getStyleAttributes($fontStyleXml->vertAlign);
> if (isset($attr['val'])) {
> $verticalAlign = strtolower((string) $attr['val']);
if ($verticalAlign === 'superscript') {
$fontStyle->setSuperscript(true);
< }
< if ($verticalAlign === 'subscript') {
> } elseif ($verticalAlign === 'subscript') {
$fontStyle->setSubscript(true);
}
}
}
> }
< private static function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml): void
> private function readNumberFormat(NumberFormat $numfmtStyle, SimpleXMLElement $numfmtStyleXml): void
{
< if ($numfmtStyleXml->count() === 0) {
> if ((string) $numfmtStyleXml['formatCode'] !== '') {
> $numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmtStyleXml['formatCode']));
>
return;
}
< $numfmt = $numfmtStyleXml->attributes();
< if ($numfmt->count() > 0 && isset($numfmt['formatCode'])) {
< $numfmtStyle->setFormatCode((string) $numfmt['formatCode']);
> $numfmt = $this->getStyleAttributes($numfmtStyleXml);
> if (isset($numfmt['formatCode'])) {
> $numfmtStyle->setFormatCode(self::formatGeneral((string) $numfmt['formatCode']));
}
}
< private static function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
> public function readFillStyle(Fill $fillStyle, SimpleXMLElement $fillStyleXml): void
{
if ($fillStyleXml->gradientFill) {
/** @var SimpleXMLElement $gradientFill */
$gradientFill = $fillStyleXml->gradientFill[0];
< if (!empty($gradientFill['type'])) {
< $fillStyle->setFillType((string) $gradientFill['type']);
< }
< $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));
> $attr = $this->getStyleAttributes($gradientFill);
> if (!empty($attr['type'])) {
> $fillStyle->setFillType((string) $attr['type']);
> }
> $fillStyle->setRotation((float) ($attr['degree']));
> $gradientFill->registerXPathNamespace('sml', Namespaces::MAIN);
> $fillStyle->getStartColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=0]'))->color));
> $fillStyle->getEndColor()->setARGB($this->readColor(self::getArrayItem($gradientFill->xpath('sml:stop[@position=1]'))->color));
} elseif ($fillStyleXml->patternFill) {
< $patternType = (string) $fillStyleXml->patternFill['patternType'] != '' ? (string) $fillStyleXml->patternFill['patternType'] : 'solid';
< $fillStyle->setFillType($patternType);
> $defaultFillStyle = Fill::FILL_NONE;
if ($fillStyleXml->patternFill->fgColor) {
< $fillStyle->getStartColor()->setARGB(self::readColor($fillStyleXml->patternFill->fgColor, true));
< } else {
< $fillStyle->getStartColor()->setARGB('FF000000');
> $fillStyle->getStartColor()->setARGB($this->readColor($fillStyleXml->patternFill->fgColor, true));
> $defaultFillStyle = Fill::FILL_SOLID;
}
if ($fillStyleXml->patternFill->bgColor) {
< $fillStyle->getEndColor()->setARGB(self::readColor($fillStyleXml->patternFill->bgColor, true));
> $fillStyle->getEndColor()->setARGB($this->readColor($fillStyleXml->patternFill->bgColor, true));
> $defaultFillStyle = Fill::FILL_SOLID;
}
>
}
> $type = '';
}
> if ((string) $fillStyleXml->patternFill['patternType'] !== '') {
> $type = (string) $fillStyleXml->patternFill['patternType'];
private static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
> } else {
{
> $attr = $this->getStyleAttributes($fillStyleXml->patternFill);
$diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
> $type = (string) $attr['patternType'];
$diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
> }
if (!$diagonalUp && !$diagonalDown) {
> $patternType = ($type === '') ? $defaultFillStyle : $type;
$borderStyle->setDiagonalDirection(Borders::DIAGONAL_NONE);
>
} elseif ($diagonalUp && !$diagonalDown) {
> $fillStyle->setFillType($patternType);
< private static function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
> public function readBorderStyle(Borders $borderStyle, SimpleXMLElement $borderStyleXml): void
< $diagonalUp = self::boolean((string) $borderStyleXml['diagonalUp']);
< $diagonalDown = self::boolean((string) $borderStyleXml['diagonalDown']);
< if (!$diagonalUp && !$diagonalDown) {
> $diagonalUp = $this->getAttribute($borderStyleXml, 'diagonalUp');
> $diagonalUp = self::boolean($diagonalUp);
> $diagonalDown = $this->getAttribute($borderStyleXml, 'diagonalDown');
> $diagonalDown = self::boolean($diagonalDown);
> if ($diagonalUp === false) {
> if ($diagonalDown === false) {
< } elseif ($diagonalUp && !$diagonalDown) {
< $borderStyle->setDiagonalDirection(Borders::DIAGONAL_UP);
< } elseif (!$diagonalUp && $diagonalDown) {
> } else {
self::readBorder($borderStyle->getLeft(), $borderStyleXml->left);
> }
self::readBorder($borderStyle->getRight(), $borderStyleXml->right);
> } elseif ($diagonalDown === false) {
self::readBorder($borderStyle->getTop(), $borderStyleXml->top);
> $borderStyle->setDiagonalDirection(Borders::DIAGONAL_UP);
< self::readBorder($borderStyle->getLeft(), $borderStyleXml->left);
< self::readBorder($borderStyle->getRight(), $borderStyleXml->right);
< self::readBorder($borderStyle->getTop(), $borderStyleXml->top);
< self::readBorder($borderStyle->getBottom(), $borderStyleXml->bottom);
< self::readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
> if (isset($borderStyleXml->left)) {
> $this->readBorder($borderStyle->getLeft(), $borderStyleXml->left);
> }
> if (isset($borderStyleXml->right)) {
> $this->readBorder($borderStyle->getRight(), $borderStyleXml->right);
> }
> if (isset($borderStyleXml->top)) {
> $this->readBorder($borderStyle->getTop(), $borderStyleXml->top);
> }
> if (isset($borderStyleXml->bottom)) {
> $this->readBorder($borderStyle->getBottom(), $borderStyleXml->bottom);
> }
> if (isset($borderStyleXml->diagonal)) {
> $this->readBorder($borderStyle->getDiagonal(), $borderStyleXml->diagonal);
> }
> }
>
> private function getAttribute(SimpleXMLElement $xml, string $attribute): string
> {
> $style = '';
> if ((string) $xml[$attribute] !== '') {
> $style = (string) $xml[$attribute];
> } else {
> $attr = $this->getStyleAttributes($xml);
> if (isset($attr[$attribute])) {
> $style = (string) $attr[$attribute];
> }
> }
>
> return $style;
< private static function readBorder(Border $border, SimpleXMLElement $borderXml): void
> private function readBorder(Border $border, SimpleXMLElement $borderXml): void
< if (isset($borderXml['style'])) {
< $border->setBorderStyle((string) $borderXml['style']);
> $style = $this->getAttribute($borderXml, 'style');
> if ($style !== '') {
> $border->setBorderStyle((string) $style);
> } else {
> $border->setBorderStyle(Border::BORDER_NONE);
}
if (isset($borderXml->color)) {
< $border->getColor()->setARGB(self::readColor($borderXml->color));
> $border->getColor()->setARGB($this->readColor($borderXml->color));
}
}
< private static function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
> public function readAlignmentStyle(Alignment $alignment, SimpleXMLElement $alignmentXml): void
{
< $alignment->setHorizontal((string) $alignmentXml->alignment['horizontal']);
< $alignment->setVertical((string) $alignmentXml->alignment['vertical']);
> $horizontal = $this->getAttribute($alignmentXml, 'horizontal');
> $alignment->setHorizontal($horizontal);
> $vertical = $this->getAttribute($alignmentXml, 'vertical');
> $alignment->setVertical((string) $vertical);
>
> $textRotation = (int) $this->getAttribute($alignmentXml, 'textRotation');
> if ($textRotation > 90) {
> $textRotation = 90 - $textRotation;
> }
> $alignment->setTextRotation($textRotation);
< $textRotation = 0;
< if ((int) $alignmentXml->alignment['textRotation'] <= 90) {
< $textRotation = (int) $alignmentXml->alignment['textRotation'];
< } elseif ((int) $alignmentXml->alignment['textRotation'] > 90) {
< $textRotation = 90 - (int) $alignmentXml->alignment['textRotation'];
> $wrapText = $this->getAttribute($alignmentXml, 'wrapText');
> $alignment->setWrapText(self::boolean((string) $wrapText));
> $shrinkToFit = $this->getAttribute($alignmentXml, 'shrinkToFit');
> $alignment->setShrinkToFit(self::boolean((string) $shrinkToFit));
> $indent = (int) $this->getAttribute($alignmentXml, 'indent');
> $alignment->setIndent(max($indent, 0));
> $readingOrder = (int) $this->getAttribute($alignmentXml, 'readingOrder');
> $alignment->setReadOrder(max($readingOrder, 0));
}
< $alignment->setTextRotation((int) $textRotation);
< $alignment->setWrapText(self::boolean((string) $alignmentXml->alignment['wrapText']));
< $alignment->setShrinkToFit(self::boolean((string) $alignmentXml->alignment['shrinkToFit']));
< $alignment->setIndent((int) ((string) $alignmentXml->alignment['indent']) > 0 ? (int) ((string) $alignmentXml->alignment['indent']) : 0);
< $alignment->setReadOrder((int) ((string) $alignmentXml->alignment['readingOrder']) > 0 ? (int) ((string) $alignmentXml->alignment['readingOrder']) : 0);
> private static function formatGeneral(string $formatString): string
> {
> if ($formatString === 'GENERAL') {
> $formatString = NumberFormat::FORMAT_GENERAL;
> }
>
> return $formatString;
}
< private function readStyle(Style $docStyle, $style): void
> /**
> * Read style.
> *
> * @param SimpleXMLElement|stdClass $style
> */
> public function readStyle(Style $docStyle, $style): void
{
< if ($style->numFmt instanceof SimpleXMLElement) {
< self::readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
> if ($style instanceof SimpleXMLElement) {
> $this->readNumberFormat($docStyle->getNumberFormat(), $style->numFmt);
} else {
< $docStyle->getNumberFormat()->setFormatCode($style->numFmt);
> $docStyle->getNumberFormat()->setFormatCode(self::formatGeneral((string) $style->numFmt));
}
if (isset($style->font)) {
< self::readFontStyle($docStyle->getFont(), $style->font);
> $this->readFontStyle($docStyle->getFont(), $style->font);
}
if (isset($style->fill)) {
< self::readFillStyle($docStyle->getFill(), $style->fill);
> $this->readFillStyle($docStyle->getFill(), $style->fill);
}
if (isset($style->border)) {
< self::readBorderStyle($docStyle->getBorders(), $style->border);
> $this->readBorderStyle($docStyle->getBorders(), $style->border);
}
< if (isset($style->alignment->alignment)) {
< self::readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
> if (isset($style->alignment)) {
> $this->readAlignmentStyle($docStyle->getAlignment(), $style->alignment);
}
// protection
if (isset($style->protection)) {
< $this->readProtectionLocked($docStyle, $style);
< $this->readProtectionHidden($docStyle, $style);
> $this->readProtectionLocked($docStyle, $style->protection);
> $this->readProtectionHidden($docStyle, $style->protection);
}
// top-level style settings
if (isset($style->quotePrefix)) {
< $docStyle->setQuotePrefix(true);
> $docStyle->setQuotePrefix((bool) $style->quotePrefix);
}
}
< private function readProtectionLocked(Style $docStyle, $style): void
> /**
> * Read protection locked attribute.
> */
> public function readProtectionLocked(Style $docStyle, SimpleXMLElement $style): void
{
< if (isset($style->protection['locked'])) {
< if (self::boolean((string) $style->protection['locked'])) {
> $locked = '';
> if ((string) $style['locked'] !== '') {
> $locked = (string) $style['locked'];
> } else {
> $attr = $this->getStyleAttributes($style);
> if (isset($attr['locked'])) {
> $locked = (string) $attr['locked'];
> }
> }
> if ($locked !== '') {
> if (self::boolean($locked)) {
$docStyle->getProtection()->setLocked(Protection::PROTECTION_PROTECTED);
} else {
$docStyle->getProtection()->setLocked(Protection::PROTECTION_UNPROTECTED);
}
}
}
< private function readProtectionHidden(Style $docStyle, $style): void
> /**
> * Read protection hidden attribute.
> */
> public function readProtectionHidden(Style $docStyle, SimpleXMLElement $style): void
{
< if (isset($style->protection['hidden'])) {
< if (self::boolean((string) $style->protection['hidden'])) {
> $hidden = '';
> if ((string) $style['hidden'] !== '') {
> $hidden = (string) $style['hidden'];
> } else {
> $attr = $this->getStyleAttributes($style);
> if (isset($attr['hidden'])) {
> $hidden = (string) $attr['hidden'];
> }
> }
> if ($hidden !== '') {
> if (self::boolean((string) $hidden)) {
$docStyle->getProtection()->setHidden(Protection::PROTECTION_PROTECTED);
} else {
$docStyle->getProtection()->setHidden(Protection::PROTECTION_UNPROTECTED);
}
}
}
< private static function readColor($color, $background = false)
> public function readColor(SimpleXMLElement $color, bool $background = false): string
{
< 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);
> $attr = $this->getStyleAttributes($color);
> if (isset($attr['rgb'])) {
> return (string) $attr['rgb'];
> }
> if (isset($attr['indexed'])) {
> $indexedColor = (int) $attr['indexed'];
> if ($indexedColor >= count($this->workbookPalette)) {
> return Color::indexedColor($indexedColor - 7, $background)->getARGB() ?? '';
> }
>
> return Color::indexedColor($indexedColor, $background, $this->workbookPalette)->getARGB() ?? '';
> }
> if (isset($attr['theme'])) {
> if ($this->theme !== null) {
> $returnColour = $this->theme->getColourByIndex((int) $attr['theme']);
> if (isset($attr['tint'])) {
> $tintAdjust = (float) $attr['tint'];
> $returnColour = Color::changeBrightness($returnColour ?? '', $tintAdjust);
}
return 'FF' . $returnColour;
}
}
return ($background) ? 'FFFFFFFF' : 'FF000000';
}
< public function dxfs($readDataOnly = false)
> public function dxfs(bool $readDataOnly = false): array
{
$dxfs = [];
if (!$readDataOnly && $this->styleXml) {
// Conditional Styles
if ($this->styleXml->dxfs) {
foreach ($this->styleXml->dxfs->dxf as $dxf) {
$style = new Style(false, true);
$this->readStyle($style, $dxf);
$dxfs[] = $style;
}
}
// Cell Styles
if ($this->styleXml->cellStyles) {
< foreach ($this->styleXml->cellStyles->cellStyle as $cellStyle) {
> foreach ($this->styleXml->cellStyles->cellStyle as $cellStylex) {
> $cellStyle = Xlsx::getAttributes($cellStylex);
if ((int) ($cellStyle['builtinId']) == 0) {
if (isset($this->cellStyles[(int) ($cellStyle['xfId'])])) {
// Set default style
$style = new Style();
$this->readStyle($style, $this->cellStyles[(int) ($cellStyle['xfId'])]);
// normal style, currently not using it for anything
}
}
}
}
}
return $dxfs;
}
< public function styles()
> public function styles(): array
{
return $this->styles;
}
< private static function getArrayItem($array, $key = 0)
> /**
> * Get array item.
> *
> * @param mixed $array (usually array, in theory can be false)
> *
> * @return stdClass
> */
> private static function getArrayItem($array, int $key = 0)
{
< return $array[$key] ?? null;
> return is_array($array) ? ($array[$key] ?? null) : null;
}
}