Completed
Pull Request — develop (#208)
by Franck
08:10
created

PptTheme::writeTheme()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

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