Completed
Pull Request — develop (#186)
by
unknown
07:55
created

PptSlideMasters::render()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

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