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