1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Reader\Xml; |
4
|
|
|
|
5
|
|
|
use SimpleXMLElement; |
6
|
|
|
|
7
|
|
|
class Style |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Formats. |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $styles = []; |
15
|
|
|
|
16
|
14 |
|
public function parseStyles(SimpleXMLElement $xml, array $namespaces): array |
17
|
|
|
{ |
18
|
14 |
|
if (!isset($xml->Styles)) { |
19
|
1 |
|
return []; |
20
|
|
|
} |
21
|
|
|
|
22
|
13 |
|
$alignmentStyleParser = new Style\Alignment(); |
23
|
13 |
|
$borderStyleParser = new Style\Border(); |
24
|
13 |
|
$fontStyleParser = new Style\Font(); |
25
|
13 |
|
$fillStyleParser = new Style\Fill(); |
26
|
13 |
|
$numberFormatStyleParser = new Style\NumberFormat(); |
27
|
|
|
|
28
|
13 |
|
foreach ($xml->Styles[0] as $style) { |
29
|
13 |
|
$style_ss = self::getAttributes($style, $namespaces['ss']); |
30
|
13 |
|
$styleID = (string) $style_ss['ID']; |
31
|
13 |
|
$this->styles[$styleID] = $this->styles['Default'] ?? []; |
32
|
|
|
|
33
|
13 |
|
$alignment = $border = $font = $fill = $numberFormat = []; |
34
|
|
|
|
35
|
13 |
|
foreach ($style as $styleType => $styleDatax) { |
36
|
13 |
|
$styleData = $styleDatax ?? new SimpleXMLElement('<xml></xml>'); |
37
|
13 |
|
$styleAttributes = $styleData->attributes($namespaces['ss']); |
38
|
|
|
|
39
|
|
|
switch ($styleType) { |
40
|
13 |
|
case 'Alignment': |
41
|
13 |
|
if ($styleAttributes) { |
42
|
13 |
|
$alignment = $alignmentStyleParser->parseStyle($styleAttributes); |
43
|
|
|
} |
44
|
|
|
|
45
|
13 |
|
break; |
46
|
13 |
|
case 'Borders': |
47
|
13 |
|
$border = $borderStyleParser->parseStyle($styleData, $namespaces); |
48
|
|
|
|
49
|
13 |
|
break; |
50
|
13 |
|
case 'Font': |
51
|
13 |
|
if ($styleAttributes) { |
52
|
13 |
|
$font = $fontStyleParser->parseStyle($styleAttributes); |
53
|
|
|
} |
54
|
|
|
|
55
|
13 |
|
break; |
56
|
13 |
|
case 'Interior': |
57
|
12 |
|
if ($styleAttributes) { |
58
|
9 |
|
$fill = $fillStyleParser->parseStyle($styleAttributes); |
59
|
|
|
} |
60
|
|
|
|
61
|
12 |
|
break; |
62
|
13 |
|
case 'NumberFormat': |
63
|
13 |
|
if ($styleAttributes) { |
64
|
10 |
|
$numberFormat = $numberFormatStyleParser->parseStyle($styleAttributes); |
65
|
|
|
} |
66
|
|
|
|
67
|
13 |
|
break; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
13 |
|
$this->styles[$styleID] = array_merge($alignment, $border, $font, $fill, $numberFormat); |
72
|
|
|
} |
73
|
|
|
|
74
|
13 |
|
return $this->styles; |
75
|
|
|
} |
76
|
|
|
|
77
|
13 |
|
protected static function getAttributes(?SimpleXMLElement $simple, string $node): SimpleXMLElement |
78
|
|
|
{ |
79
|
13 |
|
return ($simple === null) |
80
|
|
|
? new SimpleXMLElement('<xml></xml>') |
81
|
13 |
|
: ($simple->attributes($node) ?? new SimpleXMLElement('<xml></xml>')); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|