Failed Conditions
Push — master ( 4e6d68...8ea48e )
by Adrien
11:30 queued 01:31
created

SheetViews::showZeros()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 2
cts 4
cp 0.5
crap 2.5
rs 10
c 0
b 0
f 0
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 68
    public function __construct(\SimpleXMLElement $sheetViewXml, Worksheet $workSheet)
15
    {
16 68
        $this->sheetViewXml = $sheetViewXml;
17 68
        $this->worksheet = $workSheet;
18 68
    }
19
20 68
    public function load()
21
    {
22 68
        $this->zoomScale();
23 68
        $this->view();
24 68
        $this->gridLines();
25 68
        $this->headers();
26 68
        $this->direction();
27 68
        $this->showZeros();
28
29 68
        if (isset($this->sheetViewXml->pane)) {
30 4
            $this->pane();
31
        }
32 68
        if (isset($this->sheetViewXml->selection, $this->sheetViewXml->selection['sqref'])) {
33 63
            $this->selection();
34
        }
35 68
    }
36
37 68
    private function zoomScale()
38
    {
39 68
        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 68
        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 68
    }
61
62 68
    private function view()
63
    {
64 68
        if (isset($this->sheetViewXml['view'])) {
65
            $this->worksheet->getSheetView()->setView((string) $this->sheetViewXml['view']);
66
        }
67 68
    }
68
69 68
    private function gridLines()
70
    {
71 68
        if (isset($this->sheetViewXml['showGridLines'])) {
72 52
            $this->worksheet->setShowGridLines(
73 52
                self::boolean((string) $this->sheetViewXml['showGridLines'])
74
            );
75
        }
76 68
    }
77
78 68
    private function headers()
79
    {
80 68
        if (isset($this->sheetViewXml['showRowColHeaders'])) {
81 52
            $this->worksheet->setShowRowColHeaders(
82 52
                self::boolean((string) $this->sheetViewXml['showRowColHeaders'])
83
            );
84
        }
85 68
    }
86
87 68
    private function direction()
88
    {
89 68
        if (isset($this->sheetViewXml['rightToLeft'])) {
90
            $this->worksheet->setRightToLeft(
91
                self::boolean((string) $this->sheetViewXml['rightToLeft'])
92
            );
93
        }
94 68
    }
95
96 68
    private function showZeros()
97
    {
98 68
        if (isset($this->sheetViewXml['showZeros'])) {
99
            $this->worksheet->getSheetView()->setShowZeros(
100
                self::boolean((string) $this->sheetViewXml['showZeros'])
101
            );
102
        }
103 68
    }
104
105 4
    private function pane()
106
    {
107 4
        $xSplit = 0;
108 4
        $ySplit = 0;
109 4
        $topLeftCell = null;
110
111 4
        if (isset($this->sheetViewXml->pane['xSplit'])) {
112 1
            $xSplit = (int) ($this->sheetViewXml->pane['xSplit']);
113
        }
114
115 4
        if (isset($this->sheetViewXml->pane['ySplit'])) {
116 4
            $ySplit = (int) ($this->sheetViewXml->pane['ySplit']);
117
        }
118
119 4
        if (isset($this->sheetViewXml->pane['topLeftCell'])) {
120 4
            $topLeftCell = (string) $this->sheetViewXml->pane['topLeftCell'];
121
        }
122
123 4
        $this->worksheet->freezePane(
124 4
            Coordinate::stringFromColumnIndex($xSplit + 1) . ($ySplit + 1),
125
            $topLeftCell
126
        );
127 4
    }
128
129 63
    private function selection()
130
    {
131 63
        $sqref = (string) $this->sheetViewXml->selection['sqref'];
132 63
        $sqref = explode(' ', $sqref);
133 63
        $sqref = $sqref[0];
134
135 63
        $this->worksheet->setSelectedCells($sqref);
136 63
    }
137
}
138