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

PptSlideLayouts::writeSlideLayoutRelationships()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 30
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 10
cts 12
cp 0.8333
rs 8.8571
cc 3
eloc 12
nc 3
nop 2
crap 3.0416
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