| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Reader\Xml; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Style\Protection; |
| 6 |
use SimpleXMLElement; |
| 7 |
|
| 8 |
class Style |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Formats. |
| 12 |
* |
| 13 |
* @var mixed[] |
| 14 |
*/ |
| 15 |
protected array $styles = []; |
| 16 |
|
| 17 |
/** |
| 18 |
* @param string[] $namespaces |
| 19 |
* |
| 20 |
* @return mixed[] |
| 21 |
*/ |
| 22 |
public function parseStyles(SimpleXMLElement $xml, array $namespaces): array |
| 23 |
{ |
| 24 |
$children = $xml->children('urn:schemas-microsoft-com:office:spreadsheet'); |
| 25 |
$stylesXml = $children->Styles[0]; |
| 26 |
if (!isset($stylesXml)) { |
| 27 |
return []; |
| 28 |
} |
| 29 |
|
| 30 |
$alignmentStyleParser = new Style\Alignment(); |
| 31 |
$borderStyleParser = new Style\Border(); |
| 32 |
$fontStyleParser = new Style\Font(); |
| 33 |
$fillStyleParser = new Style\Fill(); |
| 34 |
$numberFormatStyleParser = new Style\NumberFormat(); |
| 35 |
|
| 36 |
foreach ($stylesXml as $style) { |
| 37 |
/** @var SimpleXMLElement $style */ |
| 38 |
$style_ss = self::getAttributes($style, $namespaces['ss']); |
| 39 |
$styleID = (string) $style_ss['ID']; |
| 40 |
$this->styles[$styleID] = $this->styles['Default'] ?? []; |
| 41 |
|
| 42 |
$alignment = $border = $font = $fill = $numberFormat = $protection = []; |
| 43 |
|
| 44 |
foreach ($style as $styleType => $styleDatax) { |
| 45 |
$styleData = self::getSxml($styleDatax); |
| 46 |
$styleAttributes = $styleData->attributes($namespaces['ss']); |
| 47 |
|
| 48 |
switch ($styleType) { |
| 49 |
case 'Alignment': |
| 50 |
if ($styleAttributes) { |
| 51 |
$alignment = $alignmentStyleParser->parseStyle($styleAttributes); |
| 52 |
} |
| 53 |
|
| 54 |
break; |
| 55 |
case 'Borders': |
| 56 |
$border = $borderStyleParser->parseStyle($styleData, $namespaces); |
| 57 |
|
| 58 |
break; |
| 59 |
case 'Font': |
| 60 |
if ($styleAttributes) { |
| 61 |
$font = $fontStyleParser->parseStyle($styleAttributes); |
| 62 |
} |
| 63 |
|
| 64 |
break; |
| 65 |
case 'Interior': |
| 66 |
if ($styleAttributes) { |
| 67 |
$fill = $fillStyleParser->parseStyle($styleAttributes); |
| 68 |
} |
| 69 |
|
| 70 |
break; |
| 71 |
case 'NumberFormat': |
| 72 |
if ($styleAttributes) { |
| 73 |
$numberFormat = $numberFormatStyleParser->parseStyle($styleAttributes); |
| 74 |
} |
| 75 |
|
| 76 |
break; |
| 77 |
case 'Protection': |
| 78 |
$locked = $hidden = null; |
| 79 |
$styleAttributesP = array_key_exists('x', $namespaces) ? $styleData->attributes($namespaces['x']) : []; |
| 80 |
if (isset($styleAttributes['Protected'])) { |
| 81 |
$locked = ((bool) (string) $styleAttributes['Protected']) ? Protection::PROTECTION_PROTECTED : Protection::PROTECTION_UNPROTECTED; |
| 82 |
} |
| 83 |
if (isset($styleAttributesP['HideFormula'])) { |
| 84 |
$hidden = ((bool) (string) $styleAttributesP['HideFormula']) ? Protection::PROTECTION_PROTECTED : Protection::PROTECTION_UNPROTECTED; |
| 85 |
} |
| 86 |
if ($locked !== null || $hidden !== null) { |
| 87 |
$protection['protection'] = []; |
| 88 |
if ($locked !== null) { |
| 89 |
$protection['protection']['locked'] = $locked; |
| 90 |
} |
| 91 |
if ($hidden !== null) { |
| 92 |
$protection['protection']['hidden'] = $hidden; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
break; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
$this->styles[$styleID] = array_merge($alignment, $border, $font, $fill, $numberFormat, $protection); |
| 101 |
} |
| 102 |
|
| 103 |
return $this->styles; |
| 104 |
} |
| 105 |
|
| 106 |
private static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement |
| 107 |
{ |
| 108 |
return ($simple === null) ? new SimpleXMLElement('<xml></xml>') : ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>')); |
| 109 |
} |
| 110 |
|
| 111 |
private static function getSxml(?SimpleXMLElement $simple): SimpleXMLElement |
| 112 |
{ |
| 113 |
return ($simple !== null) ? $simple : new SimpleXMLElement('<xml></xml>'); |
| 114 |
} |
| 115 |
} |
| 116 |
|