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

PptSlideLayouts   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.86%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 8
c 5
b 2
f 0
lcom 1
cbo 7
dl 0
loc 169
ccs 65
cts 70
cp 0.9286
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 20 3
B writeSlideLayoutRelationships() 0 31 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
     */
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
            }
23
        }
24
25
        /*
26
        foreach ($oLayoutPack->getLayoutRelations() as $otherRelation) {
27
          if (strpos($otherRelation['target'], 'http://') === 0) {
28
              continue;
29
          }
30
          $this->oZip->addFromString($this->absoluteZipPath('ppt/slideLayouts/' . $otherRelation['target']), $otherRelation['contents']);
31
        }
32
        */
33
34 95
        return $this->oZip;
35
    }
36
37
38
    /**
39
     * Write slide layout relationships to XML format
40
     *
41
     * @param  int       $slideLayoutIndex
42
     * @param  int       $masterId
43
     * @return string    XML Output
44
     * @throws \Exception
45
     */
46 95
    public function writeSlideLayoutRelationships($slideLayoutIndex, $masterId = 1)
47
    {
48
        // Create XML writer
49 95
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
50
51
        // XML header
52 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
53
54
        // Relationships
55 95
        $objWriter->startElement('Relationships');
56 95
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
57
58
        // Write slideMaster relationship
59 95
        $this->writeRelationship($objWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', '../slideMasters/slideMaster' . $masterId . '.xml');
60
61
        // Other relationships
62
        //@todo
63
        $slideLayoutIndex;
64
        /*
65
        foreach ($oLayoutPack->getLayoutRelations() as $otherRelation) {
66
            if ($otherRelation['layoutId'] == $slideLayoutIndex) {
67
                $this->writeRelationship($objWriter, $otherRelation['id'], $otherRelation['type'], $otherRelation['target']);
68
            }
69
        }
70
        */
71
72 95
        $objWriter->endElement();
73
74
        // Return
75 95
        return $objWriter->getData();
76
    }
77
78
    /**
79
     * Write slide to XML format
80
     *
81
     * @param  \PhpOffice\PhpPresentation\Slide\SlideLayout $pSlideLayout
82
     * @return string XML Output
83
     * @throws \Exception
84
     */
85 95
    public function writeSlideLayout(SlideLayout $pSlideLayout)
86
    {
87
        // Create XML writer
88 95
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
89
        // XML header
90 95
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
91
        // p:sldLayout
92 95
        $objWriter->startElement('p:sldLayout');
93 95
        $objWriter->writeAttribute('xmlns:a', 'http://schemas.openxmlformats.org/drawingml/2006/main');
94 95
        $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships');
95 95
        $objWriter->writeAttribute('xmlns:p', 'http://schemas.openxmlformats.org/presentationml/2006/main');
96 95
        $objWriter->writeAttribute('preserve', 1);
97
        // p:sldLayout\p:cSld
98 95
        $objWriter->startElement('p:cSld');
99 95
        $objWriter->writeAttributeIf($pSlideLayout->getLayoutName() != '', 'name', $pSlideLayout->getLayoutName());
100
        // Background
101 95
        $this->writeSlideBackground($pSlideLayout, $objWriter);
102
        // p:sldLayout\p:cSld\p:spTree
103 95
        $objWriter->startElement('p:spTree');
104
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
105 95
        $objWriter->startElement('p:nvGrpSpPr');
106
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvPr
107 95
        $objWriter->startElement('p:cNvPr');
108 95
        $objWriter->writeAttribute('id', '1');
109 95
        $objWriter->writeAttribute('name', '');
110 95
        $objWriter->endElement();
111
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:cNvGrpSpPr
112 95
        $objWriter->writeElement('p:cNvGrpSpPr', null);
113
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr\p:nvPr
114 95
        $objWriter->writeElement('p:nvPr', null);
115
        // p:sldLayout\p:cSld\p:spTree\p:nvGrpSpPr
116 95
        $objWriter->endElement();
117
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr
118 95
        $objWriter->startElement('p:grpSpPr');
119
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm
120 95
        $objWriter->startElement('a:xfrm');
121
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:off
122 95
        $objWriter->startElement('a:off');
123 95
        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
124 95
        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
125 95
        $objWriter->endElement();
126
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:ext
127 95
        $objWriter->startElement('a:ext');
128 95
        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
129 95
        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
130 95
        $objWriter->endElement();
131
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chOff
132 95
        $objWriter->startElement('a:chOff');
133 95
        $objWriter->writeAttribute('x', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetX()));
134 95
        $objWriter->writeAttribute('y', CommonDrawing::pixelsToEmu($pSlideLayout->getOffsetY()));
135 95
        $objWriter->endElement();
136
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\a:chExt
137 95
        $objWriter->startElement('a:chExt');
138 95
        $objWriter->writeAttribute('cx', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentX()));
139 95
        $objWriter->writeAttribute('cy', CommonDrawing::pixelsToEmu($pSlideLayout->getExtentY()));
140 95
        $objWriter->endElement();
141
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\a:xfrm\
142 95
        $objWriter->endElement();
143
        // p:sldLayout\p:cSld\p:spTree\p:grpSpPr\
144 95
        $objWriter->endElement();
145
146
        // Loop shapes
147 95
        $this->writeShapeCollection($objWriter, $pSlideLayout->getShapeCollection());
148
        // p:sldLayout\p:cSld\p:spTree\
149 95
        $objWriter->endElement();
150
        // p:sldLayout\p:cSld\
151 95
        $objWriter->endElement();
152
153
        // p:sldLayout\p:clrMapOvr
154 95
        $objWriter->startElement('p:clrMapOvr');
155 95
        $arrayDiff = array_diff_assoc(ColorMap::$mappingDefault, $pSlideLayout->colorMap->getMapping());
156 95
        if (!empty($arrayDiff)) {
157
            // p:sldLayout\p:clrMapOvr\a:overrideClrMapping
158
            $objWriter->startElement('a:overrideClrMapping');
159
            foreach ($pSlideLayout->colorMap->getMapping() as $n => $v) {
160
                $objWriter->writeAttribute($n, $v);
161
            }
162
            $objWriter->endElement();
163
        } else {
164
            // p:sldLayout\p:clrMapOvr\a:masterClrMapping
165 95
            $objWriter->writeElement('a:masterClrMapping');
166
        }
167
        // p:sldLayout\p:clrMapOvr\
168 95
        $objWriter->endElement();
169
170 95
        if (!is_null($pSlideLayout->getTransition())) {
171
            $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...
172
        }
173
174
        // p:sldLayout\
175 95
        $objWriter->endElement();
176
177 95
        return $objWriter->getData();
178
    }
179
}
180