Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DocPropsCustom
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2/**
3 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4 * presentations documents.
5 *
6 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7 * General Public License version 3 as published by the Free Software Foundation.
8 *
9 * For the full copyright and license information, please read the LICENSE
10 * file that was distributed with this source code. For the full list of
11 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12 *
13 * @see        https://github.com/PHPOffice/PHPPresentation
14 *
15 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16 */
17
18declare(strict_types=1);
19
20namespace PhpOffice\PhpPresentation\Writer\PowerPoint2007;
21
22use PhpOffice\Common\Adapter\Zip\ZipInterface;
23use PhpOffice\Common\XMLWriter;
24use PhpOffice\PhpPresentation\DocumentProperties;
25
26class DocPropsCustom extends AbstractDecoratorWriter
27{
28    public function render(): ZipInterface
29    {
30        // Variables
31        $pId = 0;
32
33        // Create XML writer
34        $objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
35
36        // XML header
37        $objWriter->startDocument('1.0', 'UTF-8', 'yes');
38
39        // Properties
40        $objWriter->startElement('Properties');
41        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
42        $objWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
43
44        if ($this->getPresentation()->getPresentationProperties()->isMarkedAsFinal()) {
45            // property
46            $objWriter->startElement('property');
47            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
48            $objWriter->writeAttribute('pid', (++$pId) * 2);
49            $objWriter->writeAttribute('name', '_MarkAsFinal');
50
51            // property > vt:bool
52            $objWriter->writeElement('vt:bool', 'true');
53
54            // > property
55            $objWriter->endElement();
56        }
57
58        $oDocumentProperties = $this->oPresentation->getDocumentProperties();
59        foreach ($oDocumentProperties->getCustomProperties() as $customProperty) {
60            $propertyValue = $oDocumentProperties->getCustomPropertyValue($customProperty);
61            $propertyType = $oDocumentProperties->getCustomPropertyType($customProperty);
62
63            $objWriter->startElement('property');
64            $objWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
65            $objWriter->writeAttribute('pid', (++$pId) * 2);
66            $objWriter->writeAttribute('name', $customProperty);
67            switch ($propertyType) {
68                case DocumentProperties::PROPERTY_TYPE_INTEGER:
69                    $objWriter->writeElement('vt:i4', (string) $propertyValue);
70
71                    break;
72                case DocumentProperties::PROPERTY_TYPE_FLOAT:
73                    $objWriter->writeElement('vt:r8', (string) $propertyValue);
74
75                    break;
76                case DocumentProperties::PROPERTY_TYPE_BOOLEAN:
77                    $objWriter->writeElement('vt:bool', $propertyValue ? 'true' : 'false');
78
79                    break;
80                case DocumentProperties::PROPERTY_TYPE_DATE:
81                    $objWriter->startElement('vt:filetime');
82                    $objWriter->writeRaw(date(DATE_W3C, (int) $propertyValue));
83                    $objWriter->endElement();
84
85                    break;
86                default:
87                    $objWriter->writeElement('vt:lpwstr', (string) $propertyValue);
88
89                    break;
90            }
91            $objWriter->endElement();
92        }
93
94        // > Properties
95        $objWriter->endElement();
96
97        $this->oZip->addFromString('docProps/custom.xml', $objWriter->getData());
98
99        // Return
100        return $this->oZip;
101    }
102}