Completed
Pull Request — develop (#188)
by Franck
06:55
created

PptSlideMasters::writeSlideMasterRelationships()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 44
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 6.0359

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 44
ccs 18
cts 20
cp 0.9
rs 8.439
cc 6
eloc 20
nc 18
nop 1
crap 6.0359
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\XMLWriter;
6
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault;
7
8
class PptSlideMasters extends AbstractDecoratorWriter
9
{
10
    /**
11
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
12
     */
13 74
    public function render()
14
    {
15 74
        $oLayoutPack = new PackDefault();
16
17 74
        foreach ($oLayoutPack->getMasterSlides() as $masterSlide) {
18
            // Add slide masters to ZIP file
19 74
            $this->oZip->addFromString('ppt/slideMasters/_rels/slideMaster' . $masterSlide['masterid'] . '.xml.rels', $this->writeSlideMasterRelationships($masterSlide['masterid']));
20 74
            $this->oZip->addFromString('ppt/slideMasters/slideMaster' . $masterSlide['masterid'] . '.xml', $masterSlide['body']);
21
        }
22
23
        // Add layoutpack relations
24 74
        $otherRelations = $oLayoutPack->getMasterSlideRelations();
25 74
        foreach ($otherRelations as $otherRelation) {
26
            if (strpos($otherRelation['target'], 'http://') !== 0) {
27
                $this->oZip->addFromString($this->absoluteZipPath('ppt/slideMasters/' . $otherRelation['target']), $otherRelation['contents']);
28
            }
29
        }
30
31 74
        return $this->oZip;
32
    }
33
34
    /**
35
     * Write slide master relationships to XML format
36
     *
37
     * @param  int       $masterId Master slide id
38
     * @return string    XML Output
39
     * @throws \Exception
40
     */
41 74
    public function writeSlideMasterRelationships($masterId = 1)
42
    {
43
        // Create XML writer
44 74
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
45
46
        // XML header
47 74
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
48
49
        // Relationships
50 74
        $objWriter->startElement('Relationships');
51 74
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
52
53
        // Keep content id
54 74
        $contentId = 0;
55
56
        // Lookup layouts
57 74
        $layouts    = array();
58 74
        $oLayoutPack = new PackDefault();
59 74
        foreach ($oLayoutPack->getLayouts() as $key => $layout) {
60 74
            if ($layout['masterid'] == $masterId) {
61 74
                $layouts[$key] = $layout;
62
            }
63
        }
64
65
        // Write slideLayout relationships
66 74
        foreach ($layouts as $key => $layout) {
67 74
            $this->writeRelationship($objWriter, ++$contentId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout', '../slideLayouts/slideLayout' . $key . '.xml');
68
        }
69
70
        // Relationship theme/theme1.xml
71 74
        $this->writeRelationship($objWriter, ++$contentId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme', '../theme/theme' . $masterId . '.xml');
72
73
        // Other relationships
74 74
        $otherRelations = $oLayoutPack->getMasterSlideRelations();
75 74
        foreach ($otherRelations as $otherRelation) {
76
            if ($otherRelation['masterid'] == $masterId) {
77
                $this->writeRelationship($objWriter, ++$contentId, $otherRelation['type'], $otherRelation['target']);
78
            }
79
        }
80 74
        $objWriter->endElement();
81
82
        // Return
83 74
        return $objWriter->getData();
84
    }
85
}
86