Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
 
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use SimpleXMLElement;
7
 
8
class WorkbookView
9
{
10
    private Spreadsheet $spreadsheet;
11
 
12
    public function __construct(Spreadsheet $spreadsheet)
13
    {
14
        $this->spreadsheet = $spreadsheet;
15
    }
16
 
17
    public function viewSettings(SimpleXMLElement $xmlWorkbook, string $mainNS, array $mapSheetId, bool $readDataOnly): void
18
    {
19
        // Default active sheet index to the first loaded worksheet from the file
20
        $this->spreadsheet->setActiveSheetIndex(0);
21
 
22
        $workbookView = $xmlWorkbook->children($mainNS)->bookViews->workbookView;
23
        if ($readDataOnly !== true && !empty($workbookView)) {
24
            $workbookViewAttributes = self::testSimpleXml(self::getAttributes($workbookView));
25
            // active sheet index
26
            $activeTab = (int) $workbookViewAttributes->activeTab; // refers to old sheet index
27
            // keep active sheet index if sheet is still loaded, else first sheet is set as the active worksheet
28
            if (isset($mapSheetId[$activeTab])) {
29
                $this->spreadsheet->setActiveSheetIndex($mapSheetId[$activeTab]);
30
            }
31
 
32
            $this->horizontalScroll($workbookViewAttributes);
33
            $this->verticalScroll($workbookViewAttributes);
34
            $this->sheetTabs($workbookViewAttributes);
35
            $this->minimized($workbookViewAttributes);
36
            $this->autoFilterDateGrouping($workbookViewAttributes);
37
            $this->firstSheet($workbookViewAttributes);
38
            $this->visibility($workbookViewAttributes);
39
            $this->tabRatio($workbookViewAttributes);
40
        }
41
    }
42
 
43
    public static function testSimpleXml(mixed $value): SimpleXMLElement
44
    {
45
        return ($value instanceof SimpleXMLElement)
46
            ? $value
47
            : new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><root></root>');
48
    }
49
 
50
    public static function getAttributes(?SimpleXMLElement $value, string $ns = ''): SimpleXMLElement
51
    {
52
        return self::testSimpleXml($value === null ? $value : $value->attributes($ns));
53
    }
54
 
55
    /**
56
     * Convert an 'xsd:boolean' XML value to a PHP boolean value.
57
     * A valid 'xsd:boolean' XML value can be one of the following
58
     * four values: 'true', 'false', '1', '0'.  It is case sensitive.
59
     *
60
     * Note that just doing '(bool) $xsdBoolean' is not safe,
61
     * since '(bool) "false"' returns true.
62
     *
63
     * @see https://www.w3.org/TR/xmlschema11-2/#boolean
64
     *
65
     * @param string $xsdBoolean An XML string value of type 'xsd:boolean'
66
     *
67
     * @return bool  Boolean value
68
     */
69
    private function castXsdBooleanToBool(string $xsdBoolean): bool
70
    {
71
        if ($xsdBoolean === 'false') {
72
            return false;
73
        }
74
 
75
        return (bool) $xsdBoolean;
76
    }
77
 
78
    private function horizontalScroll(SimpleXMLElement $workbookViewAttributes): void
79
    {
80
        if (isset($workbookViewAttributes->showHorizontalScroll)) {
81
            $showHorizontalScroll = (string) $workbookViewAttributes->showHorizontalScroll;
82
            $this->spreadsheet->setShowHorizontalScroll($this->castXsdBooleanToBool($showHorizontalScroll));
83
        }
84
    }
85
 
86
    private function verticalScroll(SimpleXMLElement $workbookViewAttributes): void
87
    {
88
        if (isset($workbookViewAttributes->showVerticalScroll)) {
89
            $showVerticalScroll = (string) $workbookViewAttributes->showVerticalScroll;
90
            $this->spreadsheet->setShowVerticalScroll($this->castXsdBooleanToBool($showVerticalScroll));
91
        }
92
    }
93
 
94
    private function sheetTabs(SimpleXMLElement $workbookViewAttributes): void
95
    {
96
        if (isset($workbookViewAttributes->showSheetTabs)) {
97
            $showSheetTabs = (string) $workbookViewAttributes->showSheetTabs;
98
            $this->spreadsheet->setShowSheetTabs($this->castXsdBooleanToBool($showSheetTabs));
99
        }
100
    }
101
 
102
    private function minimized(SimpleXMLElement $workbookViewAttributes): void
103
    {
104
        if (isset($workbookViewAttributes->minimized)) {
105
            $minimized = (string) $workbookViewAttributes->minimized;
106
            $this->spreadsheet->setMinimized($this->castXsdBooleanToBool($minimized));
107
        }
108
    }
109
 
110
    private function autoFilterDateGrouping(SimpleXMLElement $workbookViewAttributes): void
111
    {
112
        if (isset($workbookViewAttributes->autoFilterDateGrouping)) {
113
            $autoFilterDateGrouping = (string) $workbookViewAttributes->autoFilterDateGrouping;
114
            $this->spreadsheet->setAutoFilterDateGrouping($this->castXsdBooleanToBool($autoFilterDateGrouping));
115
        }
116
    }
117
 
118
    private function firstSheet(SimpleXMLElement $workbookViewAttributes): void
119
    {
120
        if (isset($workbookViewAttributes->firstSheet)) {
121
            $firstSheet = (string) $workbookViewAttributes->firstSheet;
122
            $this->spreadsheet->setFirstSheetIndex((int) $firstSheet);
123
        }
124
    }
125
 
126
    private function visibility(SimpleXMLElement $workbookViewAttributes): void
127
    {
128
        if (isset($workbookViewAttributes->visibility)) {
129
            $visibility = (string) $workbookViewAttributes->visibility;
130
            $this->spreadsheet->setVisibility($visibility);
131
        }
132
    }
133
 
134
    private function tabRatio(SimpleXMLElement $workbookViewAttributes): void
135
    {
136
        if (isset($workbookViewAttributes->tabRatio)) {
137
            $tabRatio = (string) $workbookViewAttributes->tabRatio;
138
            $this->spreadsheet->setTabRatio((int) $tabRatio);
139
        }
140
    }
141
}