Completed
Pull Request — develop (#230)
by Franck
08:45
created

PptSlideLayouts::render()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 6
nc 3
nop 0
crap 3
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\Drawing as CommonDrawing;
6
use PhpOffice\Common\XMLWriter;
7
use PhpOffice\PhpPresentation\Slide;
8
use PhpOffice\PhpPresentation\Slide\SlideLayout;
9
use PhpOffice\PhpPresentation\Style\ColorMap;
10
11
class PptSlideLayouts extends AbstractSlide
12
{
13
    /**
14
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
15
     */
16 95
    public function render()
17
    {
18 95
        foreach ($this->oPresentation->getAllMasterSlides() as $oSlideMaster) {
19 95
            foreach ($oSlideMaster->getAllSlideLayouts() as $oSlideLayout) {
20 95
                $this->oZip->addFromString('ppt/slideLayouts/_rels/slideLayout' . $oSlideLayout->layoutNr . '.xml.rels', $this->writeSlideLayoutRelationships($oSlideLayout->layoutNr, $oSlideMaster->getRelsIndex()));
21 95
                $this->oZip->addFromString('ppt/slideLayouts/slideLayout' . $oSlideLayout->layoutNr . '.xml', $this->writeSlideLayout($oSlideLayout));
22 95
            }
23 95
        }
24
25 95
        return $this->oZip;
26
    }
27
28
29
    /**
30
     * Write slide layout relationships to XML format
31
     *
32
     * @param  int       $slideLayoutIndex
33
     * @param  int       $masterId
34
     * @return string    XML Output
35
     * @throws \Exception
36
     */
37 95
    public function writeSlideLayoutRelationships($slideLayoutIndex, $masterId = 1)
0 ignored issues
show
Unused Code introduced by
The parameter $slideLayoutIndex is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        // Create XML writer
40 95
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
41
42
        // XML header
43 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
44
45
        // Relationships
46 95
        $objWriter->startElement('Relationships');
47 95
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
48
49
        // Write slideMaster relationship
50 95
        $this->writeRelationship($objWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', '../slideMasters/slideMaster' . $masterId . '.xml');
51
52 95
        $objWriter->endElement();
53
54
        // Return
55 95
        return $objWriter->getData();
56
    }
57
58
    /**
59
     * Write slide to XML format
60
     *
61
     * @param  \PhpOffice\PhpPresentation\Slide\SlideLayout $pSlideLayout
62
     * @return string XML Output
63
     * @throws \Exception
64
     */
65 95
    public function writeSlideLayout(SlideLayout $pSlideLayout)
66
    {
67
        // Create XML writer
68 95
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
69
        // XML header
70 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
71
        // p:sldLayout
72 95
        $objWriter->startElement('p:sldLayout');
73 95
        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
74 95
        $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
75 95
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
76 95
        $objWriter->writeAttribute('preserve', 1);
77
        // p:sldLayout\p:cSld
78 95
        $objWriter->startElement('p:cSld');
79 95
        $objWriter->writeAttributeIf($pSlideLayout->getLayoutName() != '', 'name', $pSlideLayout->getLayoutName());
80
        // Background
81 95
        $this->writeSlideBackground($pSlideLayout, $objWriter);
82
        // p:sldLayout\p:cSld\p:spTree
83 95
        $objWriter->startElement('p:spTree');
84
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
85 95
        $objWriter->startElement('p:nvGrpSpPr');
86
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvPr
87 95
        $objWriter->startElement('p:cNvPr');
88 95
        $objWriter->writeAttribute('id', '1');
89 95
        $objWriter->writeAttribute('name', '');
90 95
        $objWriter->endElement();
91
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvGrpSpPr
92 95
        $objWriter->writeElement('p:cNvGrpSpPr', null);
93
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:nvPr
94 95
        $objWriter->writeElement('p:nvPr', null);
95
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
96 95
        $objWriter->endElement();
97
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr
98 95
        $objWriter->startElement('p:grpSpPr');
99
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm
100 95
        $objWriter->startElement('a:xfrm');
101
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:off
102 95
        $objWriter->startElement('a:off');
103 95
        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
104 95
        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
105 95
        $objWriter->endElement();
106
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:ext
107 95
        $objWriter->startElement('a:ext');
108 95
        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
109 95
        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
110 95
        $objWriter->endElement();
111
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chOff
112 95
        $objWriter->startElement('a:chOff');
113 95
        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
114 95
        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
115 95
        $objWriter->endElement();
116
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chExt
117 95
        $objWriter->startElement('a:chExt');
118 95
        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
119 95
        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
120 95
        $objWriter->endElement();
121
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\
122 95
        $objWriter->endElement();
123
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\
124 95
        $objWriter->endElement();
125
126
        // Loop shapes
127 95
        $this->writeShapeCollection($objWriter, $pSlideLayout->getShapeCollection());
128
        // p:sldLayout\p:cSld\p:spTree\
129 95
        $objWriter->endElement();
130
        // p:sldLayout\p:cSld\
131 95
        $objWriter->endElement();
132
133
        // p:sldLayout\p:clrMapOvr
134 95
        $objWriter->startElement('p:clrMapOvr');
135 95
        $arrayDiff = array_diff_assoc(ColorMap::$mappingDefault, $pSlideLayout->colorMap->getMapping());
136 95
        if (!empty($arrayDiff)) {
137
            // p:sldLayout\p:clrMapOvr\a:overrideClrMapping
138
            $objWriter->startElement('a:overrideClrMapping');
139
            foreach ($pSlideLayout->colorMap->getMapping() as $n => $v) {
140
                $objWriter->writeAttribute($n, $v);
141
            }
142
            $objWriter->endElement();
143
        } else {
144
            // p:sldLayout\p:clrMapOvr\a:masterClrMapping
145 95
            $objWriter->writeElement('a:masterClrMapping');
146
        }
147
        // p:sldLayout\p:clrMapOvr\
148 95
        $objWriter->endElement();
149
150 95
        if (!is_null($pSlideLayout->getTransition())) {
151
            $this->writeTransition($objWriter, $pSlideLayout->getTransition());
0 ignored issues
show
Bug introduced by
The method writeTransition() does not seem to exist on object<PhpOffice\PhpPres...nt2007\PptSlideLayouts>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
        }
153
154
        // p:sldLayout\
155 95
        $objWriter->endElement();
156
157 95
        return $objWriter->getData();
158
    }
159
}
160