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

PptSlideLayouts   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 61
ccs 17
cts 21
cp 0.8095
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 18 4
B writeSlideLayoutRelationships() 0 30 3
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 PptSlideLayouts extends AbstractDecoratorWriter
9
{
10 75
    public function render()
11
    {
12 75
        $oLayoutPack = new PackDefault();
13
14
        // Add slide layouts to ZIP file
15 75
        foreach ($oLayoutPack->getLayouts() as $key => $layout) {
16 75
            $this->oZip->addFromString('ppt/slideLayouts/_rels/slideLayout' . $key . '.xml.rels', $this->writeSlideLayoutRelationships($key, $layout['masterid']));
17 75
            $this->oZip->addFromString('ppt/slideLayouts/slideLayout' . $key . '.xml', utf8_encode($layout['body']));
18
        }
19
20 75
        foreach ($oLayoutPack->getLayoutRelations() as $otherRelation) {
21
            if (strpos($otherRelation['target'], 'http://') !== 0) {
22
                $this->oZip->addFromString($this->absoluteZipPath('ppt/slideLayouts/' . $otherRelation['target']), $otherRelation['contents']);
23
            }
24
        }
25
26 75
        return $this->oZip;
27
    }
28
29
30
    /**
31
     * Write slide layout relationships to XML format
32
     *
33
     * @param  int       $slideLayoutIndex
34
     * @param  int       $masterId
35
     * @return string    XML Output
36
     * @throws \Exception
37
     */
38 75
    public function writeSlideLayoutRelationships($slideLayoutIndex, $masterId = 1)
39
    {
40
        // Create XML writer
41 75
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
42
43
        // Layout pack
44 75
        $oLayoutPack = new PackDefault();
45
46
        // XML header
47 75
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
48
49
        // Relationships
50 75
        $objWriter->startElement('Relationships');
51 75
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
52
53
        // Write slideMaster relationship
54 75
        $this->writeRelationship($objWriter, 1, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster', '../slideMasters/slideMaster' . $masterId . '.xml');
55
56
        // Other relationships
57 75
        foreach ($oLayoutPack->getLayoutRelations() as $otherRelation) {
58
            if ($otherRelation['layoutId'] == $slideLayoutIndex) {
59
                $this->writeRelationship($objWriter, $otherRelation['id'], $otherRelation['type'], $otherRelation['target']);
60
            }
61
        }
62
63 75
        $objWriter->endElement();
64
65
        // Return
66 75
        return $objWriter->getData();
67
    }
68
}
69