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

PageSetup   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 85.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 138
ccs 73
cts 85
cp 0.8588
rs 9.36
wmc 38

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A margins() 0 10 2
A columnBreaks() 0 7 3
A rowBreaks() 0 5 3
C pageSetup() 0 32 12
A load() 0 12 2
A pageBreaks() 0 7 5
B headerFooter() 0 36 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 PageSetup extends BaseParserClass
9
{
10
    private $worksheet;
11
12
    private $worksheetXml;
13
14 47
    public function __construct(Worksheet $workSheet, \SimpleXMLElement $worksheetXml = null)
15
    {
16 47
        $this->worksheet = $workSheet;
17 47
        $this->worksheetXml = $worksheetXml;
18 47
    }
19
20 47
    public function load(array $unparsedLoadedData)
21
    {
22 47
        if (!$this->worksheetXml) {
23
            return $unparsedLoadedData;
24
        }
25
26 47
        $this->margins($this->worksheetXml, $this->worksheet);
27 47
        $unparsedLoadedData = $this->pageSetup($this->worksheetXml, $this->worksheet, $unparsedLoadedData);
28 47
        $this->headerFooter($this->worksheetXml, $this->worksheet);
29 47
        $this->pageBreaks($this->worksheetXml, $this->worksheet);
30
31 47
        return $unparsedLoadedData;
32
    }
33
34 47
    private function margins(\SimpleXMLElement $xmlSheet, Worksheet $worksheet)
35
    {
36 47
        if ($xmlSheet->pageMargins) {
37 46
            $docPageMargins = $worksheet->getPageMargins();
38 46
            $docPageMargins->setLeft((float) ($xmlSheet->pageMargins['left']));
39 46
            $docPageMargins->setRight((float) ($xmlSheet->pageMargins['right']));
40 46
            $docPageMargins->setTop((float) ($xmlSheet->pageMargins['top']));
41 46
            $docPageMargins->setBottom((float) ($xmlSheet->pageMargins['bottom']));
42 46
            $docPageMargins->setHeader((float) ($xmlSheet->pageMargins['header']));
43 46
            $docPageMargins->setFooter((float) ($xmlSheet->pageMargins['footer']));
44
        }
45 47
    }
46
47 47
    private function pageSetup(\SimpleXMLElement $xmlSheet, Worksheet $worksheet, array $unparsedLoadedData)
48
    {
49 47
        if ($xmlSheet->pageSetup) {
50 43
            $docPageSetup = $worksheet->getPageSetup();
51
52 43
            if (isset($xmlSheet->pageSetup['orientation'])) {
53 43
                $docPageSetup->setOrientation((string) $xmlSheet->pageSetup['orientation']);
54
            }
55 43
            if (isset($xmlSheet->pageSetup['paperSize'])) {
56 40
                $docPageSetup->setPaperSize((int) ($xmlSheet->pageSetup['paperSize']));
57
            }
58 43
            if (isset($xmlSheet->pageSetup['scale'])) {
59 33
                $docPageSetup->setScale((int) ($xmlSheet->pageSetup['scale']), false);
60
            }
61 43
            if (isset($xmlSheet->pageSetup['fitToHeight']) && (int) ($xmlSheet->pageSetup['fitToHeight']) >= 0) {
62 33
                $docPageSetup->setFitToHeight((int) ($xmlSheet->pageSetup['fitToHeight']), false);
63
            }
64 43
            if (isset($xmlSheet->pageSetup['fitToWidth']) && (int) ($xmlSheet->pageSetup['fitToWidth']) >= 0) {
65 33
                $docPageSetup->setFitToWidth((int) ($xmlSheet->pageSetup['fitToWidth']), false);
66
            }
67 43
            if (isset($xmlSheet->pageSetup['firstPageNumber'], $xmlSheet->pageSetup['useFirstPageNumber']) &&
68 43
                self::boolean((string) $xmlSheet->pageSetup['useFirstPageNumber'])) {
69
                $docPageSetup->setFirstPageNumber((int) ($xmlSheet->pageSetup['firstPageNumber']));
70
            }
71
72 43
            $relAttributes = $xmlSheet->pageSetup->attributes('http://schemas.openxmlformats.org/officeDocument/2006/relationships');
73 43
            if (isset($relAttributes['id'])) {
74 9
                $unparsedLoadedData['sheets'][$worksheet->getCodeName()]['pageSetupRelId'] = (string) $relAttributes['id'];
75
            }
76
        }
77
78 47
        return $unparsedLoadedData;
79
    }
80
81 47
    private function headerFooter(\SimpleXMLElement $xmlSheet, Worksheet $worksheet)
82
    {
83 47
        if ($xmlSheet->headerFooter) {
84 37
            $docHeaderFooter = $worksheet->getHeaderFooter();
85
86 37
            if (isset($xmlSheet->headerFooter['differentOddEven']) &&
87 37
                self::boolean((string) $xmlSheet->headerFooter['differentOddEven'])) {
88
                $docHeaderFooter->setDifferentOddEven(true);
89
            } else {
90 37
                $docHeaderFooter->setDifferentOddEven(false);
91
            }
92 37
            if (isset($xmlSheet->headerFooter['differentFirst']) &&
93 37
                self::boolean((string) $xmlSheet->headerFooter['differentFirst'])) {
94
                $docHeaderFooter->setDifferentFirst(true);
95
            } else {
96 37
                $docHeaderFooter->setDifferentFirst(false);
97
            }
98 37
            if (isset($xmlSheet->headerFooter['scaleWithDoc']) &&
99 37
                !self::boolean((string) $xmlSheet->headerFooter['scaleWithDoc'])) {
100
                $docHeaderFooter->setScaleWithDocument(false);
101
            } else {
102 37
                $docHeaderFooter->setScaleWithDocument(true);
103
            }
104 37
            if (isset($xmlSheet->headerFooter['alignWithMargins']) &&
105 37
                !self::boolean((string) $xmlSheet->headerFooter['alignWithMargins'])) {
106 3
                $docHeaderFooter->setAlignWithMargins(false);
107
            } else {
108 34
                $docHeaderFooter->setAlignWithMargins(true);
109
            }
110
111 37
            $docHeaderFooter->setOddHeader((string) $xmlSheet->headerFooter->oddHeader);
112 37
            $docHeaderFooter->setOddFooter((string) $xmlSheet->headerFooter->oddFooter);
113 37
            $docHeaderFooter->setEvenHeader((string) $xmlSheet->headerFooter->evenHeader);
114 37
            $docHeaderFooter->setEvenFooter((string) $xmlSheet->headerFooter->evenFooter);
115 37
            $docHeaderFooter->setFirstHeader((string) $xmlSheet->headerFooter->firstHeader);
116 37
            $docHeaderFooter->setFirstFooter((string) $xmlSheet->headerFooter->firstFooter);
117
        }
118 47
    }
119
120 47
    private function pageBreaks(\SimpleXMLElement $xmlSheet, Worksheet $worksheet)
121
    {
122 47
        if ($xmlSheet->rowBreaks && $xmlSheet->rowBreaks->brk) {
123 1
            $this->rowBreaks($xmlSheet, $worksheet);
124
        }
125 47
        if ($xmlSheet->colBreaks && $xmlSheet->colBreaks->brk) {
126
            $this->columnBreaks($xmlSheet, $worksheet);
127
        }
128 47
    }
129
130 1
    private function rowBreaks(\SimpleXMLElement $xmlSheet, Worksheet $worksheet)
131
    {
132 1
        foreach ($xmlSheet->rowBreaks->brk as $brk) {
133 1
            if ($brk['man']) {
134 1
                $worksheet->setBreak("A{$brk['id']}", Worksheet::BREAK_ROW);
135
            }
136
        }
137 1
    }
138
139
    private function columnBreaks(\SimpleXMLElement $xmlSheet, Worksheet $worksheet)
140
    {
141
        foreach ($xmlSheet->colBreaks->brk as $brk) {
142
            if ($brk['man']) {
143
                $worksheet->setBreak(
144
                    Coordinate::stringFromColumnIndex(((int) $brk['id']) + 1) . '1',
145
                    Worksheet::BREAK_COLUMN
146
                );
147
            }
148
        }
149
    }
150
}
151