1<?php
2
3namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx;
4
5use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
6use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
7
8class SheetViews extends BaseParserClass
9{
10    private $sheetViewXml;
11
12    private $worksheet;
13
14    public function __construct(\SimpleXMLElement $sheetViewXml, Worksheet $workSheet)
15    {
16        $this->sheetViewXml = $sheetViewXml;
17        $this->worksheet = $workSheet;
18    }
19
20    public function load()
21    {
22        $this->zoomScale();
23        $this->view();
24        $this->gridLines();
25        $this->headers();
26        $this->direction();
27        $this->showZeros();
28
29        if (isset($this->sheetViewXml->pane)) {
30            $this->pane();
31        }
32        if (isset($this->sheetViewXml->selection, $this->sheetViewXml->selection['sqref'])) {
33            $this->selection();
34        }
35    }
36
37    private function zoomScale()
38    {
39        if (isset($this->sheetViewXml['zoomScale'])) {
40            $zoomScale = (int) ($this->sheetViewXml['zoomScale']);
41            if ($zoomScale <= 0) {
42                // setZoomScale will throw an Exception if the scale is less than or equals 0
43                // that is OK when manually creating documents, but we should be able to read all documents
44                $zoomScale = 100;
45            }
46
47            $this->worksheet->getSheetView()->setZoomScale($zoomScale);
48        }
49
50        if (isset($this->sheetViewXml['zoomScaleNormal'])) {
51            $zoomScaleNormal = (int) ($this->sheetViewXml['zoomScaleNormal']);
52            if ($zoomScaleNormal <= 0) {
53                // setZoomScaleNormal will throw an Exception if the scale is less than or equals 0
54                // that is OK when manually creating documents, but we should be able to read all documents
55                $zoomScaleNormal = 100;
56            }
57
58            $this->worksheet->getSheetView()->setZoomScaleNormal($zoomScaleNormal);
59        }
60    }
61
62    private function view()
63    {
64        if (isset($this->sheetViewXml['view'])) {
65            $this->worksheet->getSheetView()->setView((string) $this->sheetViewXml['view']);
66        }
67    }
68
69    private function gridLines()
70    {
71        if (isset($this->sheetViewXml['showGridLines'])) {
72            $this->worksheet->setShowGridLines(
73                self::boolean((string) $this->sheetViewXml['showGridLines'])
74            );
75        }
76    }
77
78    private function headers()
79    {
80        if (isset($this->sheetViewXml['showRowColHeaders'])) {
81            $this->worksheet->setShowRowColHeaders(
82                self::boolean((string) $this->sheetViewXml['showRowColHeaders'])
83            );
84        }
85    }
86
87    private function direction()
88    {
89        if (isset($this->sheetViewXml['rightToLeft'])) {
90            $this->worksheet->setRightToLeft(
91                self::boolean((string) $this->sheetViewXml['rightToLeft'])
92            );
93        }
94    }
95
96    private function showZeros()
97    {
98        if (isset($this->sheetViewXml['showZeros'])) {
99            $this->worksheet->getSheetView()->setShowZeros(
100                self::boolean((string) $this->sheetViewXml['showZeros'])
101            );
102        }
103    }
104
105    private function pane()
106    {
107        $xSplit = 0;
108        $ySplit = 0;
109        $topLeftCell = null;
110
111        if (isset($this->sheetViewXml->pane['xSplit'])) {
112            $xSplit = (int) ($this->sheetViewXml->pane['xSplit']);
113        }
114
115        if (isset($this->sheetViewXml->pane['ySplit'])) {
116            $ySplit = (int) ($this->sheetViewXml->pane['ySplit']);
117        }
118
119        if (isset($this->sheetViewXml->pane['topLeftCell'])) {
120            $topLeftCell = (string) $this->sheetViewXml->pane['topLeftCell'];
121        }
122
123        $this->worksheet->freezePane(
124            Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1),
125            $topLeftCell
126        );
127    }
128
129    private function selection()
130    {
131        $sqref = (string) $this->sheetViewXml->selection['sqref'];
132        $sqref = explode(' ', $sqref);
133        $sqref = $sqref[0];
134
135        $this->worksheet->setSelectedCells($sqref);
136    }
137}
138