Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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