Failed Conditions
Push — master ( 454d94...b2070f )
by Adrien
27:46
created

SheetViews::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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