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

PptTheme::render()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

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