Completed
Pull Request — develop (#523)
by Franck
19:56 queued 07:44
created

PptSlideLayouts   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 89.61%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 150
ccs 69
cts 77
cp 0.8961
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 11 3
A writeSlideLayoutRelationships() 0 21 1
B writeSlideLayout() 0 94 4
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
	 * @throws \Exception
16
     */
17 112
    public function render()
18
    {
19 112
        foreach ($this->oPresentation->getAllMasterSlides() as $oSlideMaster) {
20 112
            foreach ($oSlideMaster->getAllSlideLayouts() as $oSlideLayout) {
21 112
                $this->oZip->addFromString('ppt/slideLayouts/_rels/slideLayout' . $oSlideLayout->layoutNr . '.xml.rels', $this->writeSlideLayoutRelationships($oSlideLayout,$oSlideMaster->getRelsIndex()));
22 112
                $this->oZip->addFromString('ppt/slideLayouts/slideLayout' . $oSlideLayout->layoutNr . '.xml', $this->writeSlideLayout($oSlideLayout));
23 112
            }
24 112
        }
25
26 112
        return $this->oZip;
27
    }
28
29
30
    /**
31
     * Write slide layout relationships to XML format
32
     *
33
     * @param  int       $masterId
34
     * @return string    XML Output
35
     * @throws \Exception
36
     */
37 112
    public function writeSlideLayoutRelationships($oSlideLayout,$masterId = 1)
0 ignored issues
show
Coding Style introduced by
Expected 1 space between comma and argument "$masterId"; 0 found
Loading history...
38
    {
39
        // Create XML writer
40 112
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
41
42
        // XML header
43 112
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
44
45
        // Relationships
46 112
        $objWriter->startElement('Relationships');
47 112
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
48 112
        $relId = 0;
49
        // Write drawing relationships?
50 112
        $relId = $this->writeDrawingRelations($oSlideLayout, $objWriter, ++$relId);
51
        // Write slideMaster relationship
52 112
        $this->writeRelationship($objWriter, $relId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', '../slideMasters/slideMaster' . $masterId . '.xml');
53 112
        $objWriter->endElement();
54
55
        // Return
56 112
        return $objWriter->getData();
57
    }
58
59
    /**
60
     * Write slide to XML format
61
     *
62
     * @param  \PhpOffice\PhpPresentation\Slide\SlideLayout $pSlideLayout
63
     * @return string XML Output
64
     * @throws \Exception
65
     */
66 112
    public function writeSlideLayout(SlideLayout $pSlideLayout)
67
    {
68
        // Create XML writer
69 112
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
70
        // XML header
71 112
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
72
        // p:sldLayout
73 112
        $objWriter->startElement('p:sldLayout');
74 112
        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
75 112
        $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
76 112
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
77 112
        $objWriter->writeAttribute('preserve', 1);
78
        // p:sldLayout\p:cSld
79 112
        $objWriter->startElement('p:cSld');
80 112
        $objWriter->writeAttributeIf($pSlideLayout->getLayoutName() != '', 'name', $pSlideLayout->getLayoutName());
81
        // Background
82 112
        $this->writeSlideBackground($pSlideLayout, $objWriter);
83
        // p:sldLayout\p:cSld\p:spTree
84 112
        $objWriter->startElement('p:spTree');
85
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
86 112
        $objWriter->startElement('p:nvGrpSpPr');
87
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvPr
88 112
        $objWriter->startElement('p:cNvPr');
89 112
        $objWriter->writeAttribute('id', '1');
90 112
        $objWriter->writeAttribute('name', '');
91 112
        $objWriter->endElement();
92
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvGrpSpPr
93 112
        $objWriter->writeElement('p:cNvGrpSpPr', null);
94
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:nvPr
95 112
        $objWriter->writeElement('p:nvPr', null);
96
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
97 112
        $objWriter->endElement();
98
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr
99 112
        $objWriter->startElement('p:grpSpPr');
100
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm
101 112
        $objWriter->startElement('a:xfrm');
102
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:off
103 112
        $objWriter->startElement('a:off');
104 112
        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
105 112
        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
106 112
        $objWriter->endElement();
107
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:ext
108 112
        $objWriter->startElement('a:ext');
109 112
        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
110 112
        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
111 112
        $objWriter->endElement();
112
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chOff
113 112
        $objWriter->startElement('a:chOff');
114 112
        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
115 112
        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
116 112
        $objWriter->endElement();
117
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chExt
118 112
        $objWriter->startElement('a:chExt');
119 112
        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
120 112
        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
121 112
        $objWriter->endElement();
122
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\
123 112
        $objWriter->endElement();
124
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\
125 112
        $objWriter->endElement();
126
127
        // Loop shapes
128 112
        $this->writeShapeCollection($objWriter, $pSlideLayout->getShapeCollection());
129
        // p:sldLayout\p:cSld\p:spTree\
130 112
        $objWriter->endElement();
131
        // p:sldLayout\p:cSld\
132 112
        $objWriter->endElement();
133
134
        // p:sldLayout\p:clrMapOvr
135 112
        $objWriter->startElement('p:clrMapOvr');
136 112
        $arrayDiff = array_diff_assoc(ColorMap::$mappingDefault, $pSlideLayout->colorMap->getMapping());
137 112
        if (!empty($arrayDiff)) {
138
            // p:sldLayout\p:clrMapOvr\a:overrideClrMapping
139
            $objWriter->startElement('a:overrideClrMapping');
140
            foreach ($pSlideLayout->colorMap->getMapping() as $n => $v) {
141
                $objWriter->writeAttribute($n, $v);
142
            }
143
            $objWriter->endElement();
144
        } else {
145
            // p:sldLayout\p:clrMapOvr\a:masterClrMapping
146 112
            $objWriter->writeElement('a:masterClrMapping');
147
        }
148
        // p:sldLayout\p:clrMapOvr\
149 112
        $objWriter->endElement();
150
151 112
        if (!is_null($pSlideLayout->getTransition())) {
152
            $this->writeSlideTransition($objWriter, $pSlideLayout->getTransition());
153
        }
154
155
        // p:sldLayout\
156 112
        $objWriter->endElement();
157
158 112
        return $objWriter->getData();
159
    }
160
}
161