Completed
Pull Request — develop (#411)
by Franck
06:21
created

DocPropsCustom   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 75
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C render() 0 69 8
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
4
5
use PhpOffice\Common\XMLWriter;
6
use PhpOffice\PhpPresentation\DocumentProperties;
7
8
class DocPropsCustom extends AbstractDecoratorWriter
9
{
10
    /**
11
     * @return \PhpOffice\Common\Adapter\Zip\ZipInterface
12
     */
13 122
    public function render()
14
    {
15
        // Variables
16 122
        $pId = 0;
17
18
        // Create XML writer
19 122
        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
20
21
        // XML header
22 122
        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
23
24
        // Properties
25 122
        $objWriter->startElement('Properties');
26 122
        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
27 122
        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
28
29 122
        if ($this->getPresentation()->getPresentationProperties()->isMarkedAsFinal()) {
30
            // property
31 2
            $objWriter->startElement('property');
32 2
            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
33 2
            $objWriter->writeAttribute('pid', (++$pId) * 2);
34 2
            $objWriter->writeAttribute('name', '_MarkAsFinal');
35
36
            // property > vt:bool
37 2
            $objWriter->writeElement('vt:bool', 'true');
38
39
            // > property
40 2
            $objWriter->endElement();
41
        }
42
43 122
        $oDocumentProperties = $this->oPresentation->getDocumentProperties();
44 122
        foreach ($oDocumentProperties->getCustomProperties() as $customProperty) {
45 7
            $propertyValue = $oDocumentProperties->getCustomPropertyValue($customProperty);
46 7
            $propertyType = $oDocumentProperties->getCustomPropertyType($customProperty);
47
48 7
            $objWriter->startElement('property');
49 7
            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
50 7
            $objWriter->writeAttribute('pid', (++$pId) * 2);
51 7
            $objWriter->writeAttribute('name', $customProperty);
52
            switch ($propertyType) {
53 7
                case DocumentProperties::PROPERTY_TYPE_INTEGER:
54 1
                    $objWriter->writeElement('vt:i4', $propertyValue);
55 1
                    break;
56 6
                case DocumentProperties::PROPERTY_TYPE_FLOAT:
57 1
                    $objWriter->writeElement('vt:r8', $propertyValue);
58 1
                    break;
59 5
                case DocumentProperties::PROPERTY_TYPE_BOOLEAN:
60 1
                    $objWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
61 1
                    break;
62 4
                case DocumentProperties::PROPERTY_TYPE_DATE:
63 1
                    $objWriter->startElement('vt:filetime');
64 1
                    $objWriter->writeRaw(date(DATE_W3C, $propertyValue));
65 1
                    $objWriter->endElement();
66 1
                    break;
67
                default:
68 3
                    $objWriter->writeElement('vt:lpwstr', $propertyValue);
69 3
                    break;
70
            }
71 7
            $objWriter->endElement();
72
        }
73
74
        // > Properties
75 122
        $objWriter->endElement();
76
77 122
        $this->oZip->addFromString('docProps/custom.xml', $objWriter->getData());
78
79
        // Return
80 122
        return $this->oZip;
81
    }
82
}
83