Completed
Pull Request — develop (#216)
by Franck
07:08
created

PptSlideLayouts   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80.95%

Importance

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