Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
DocPropsCustom
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * word processing documents.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License version 3 as published by the Free Software Foundation.
9 *
10 * For the full copyright and license information, please read the LICENSE
11 * file that was distributed with this source code. For the full list of
12 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13 *
14 * @see         https://github.com/PHPOffice/PHPWord
15 *
16 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17 */
18
19namespace PhpOffice\PhpWord\Writer\Word2007\Part;
20
21use DateTime;
22
23/**
24 * Word2007 custom document properties part writer: docProps/custom.xml.
25 *
26 * @since 0.11.0
27 */
28class DocPropsCustom extends AbstractPart
29{
30    /**
31     * Write part.
32     *
33     * @return string
34     */
35    public function write()
36    {
37        $phpWord = $this->getParentWriter()->getPhpWord();
38        $xmlWriter = $this->getXmlWriter();
39
40        $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
41        $xmlWriter->startElement('Properties');
42        $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/officeDocument/2006/custom-properties');
43        $xmlWriter->writeAttribute('xmlns:vt', 'http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes');
44
45        $docProps = $phpWord->getDocInfo();
46        $properties = $docProps->getCustomProperties();
47        foreach ($properties as $key => $property) {
48            $propertyValue = $docProps->getCustomPropertyValue($property);
49            $propertyType = $docProps->getCustomPropertyType($property);
50
51            $xmlWriter->startElement('property');
52            $xmlWriter->writeAttribute('fmtid', '{D5CDD505-2E9C-101B-9397-08002B2CF9AE}');
53            $xmlWriter->writeAttribute('pid', $key + 2);
54            $xmlWriter->writeAttribute('name', $property);
55            switch ($propertyType) {
56                case 'i':
57                    $xmlWriter->writeElement('vt:i4', $propertyValue);
58
59                    break;
60                case 'f':
61                    $xmlWriter->writeElement('vt:r8', $propertyValue);
62
63                    break;
64                case 'b':
65                    $xmlWriter->writeElement('vt:bool', ($propertyValue) ? 'true' : 'false');
66
67                    break;
68                case 'd':
69                    if ($propertyValue instanceof DateTime) {
70                        $xmlWriter->writeElement('vt:filetime', $propertyValue->format($this->dateFormat));
71                    } else {
72                        $xmlWriter->writeElement('vt:filetime', date($this->dateFormat, $propertyValue));
73                    }
74
75                    break;
76                default:
77                    $xmlWriter->writeElement('vt:lpwstr', $propertyValue);
78
79                    break;
80            }
81            $xmlWriter->endElement(); // property
82        }
83
84        $xmlWriter->endElement(); // Properties
85
86        return $xmlWriter->getData();
87    }
88}