Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Meta
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
1 / 1
4
 writeCustomProperty
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
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\ODText\Part;
19
20use PhpOffice\PhpWord\Shared\XMLWriter;
21
22/**
23 * ODText meta part writer: meta.xml.
24 *
25 * @since 0.11.0
26 */
27class Meta extends AbstractPart
28{
29    /**
30     * Write part.
31     *
32     * @return string
33     */
34    public function write()
35    {
36        $phpWord = $this->getParentWriter()->getPhpWord();
37        $docProps = $phpWord->getDocInfo();
38        $xmlWriter = $this->getXmlWriter();
39
40        $xmlWriter->startDocument('1.0', 'UTF-8');
41        $xmlWriter->startElement('office:document-meta');
42        $xmlWriter->writeAttribute('office:version', '1.2');
43        $xmlWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
44        $xmlWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
45        $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
46        $xmlWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
47        $xmlWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
48        $xmlWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
49        $xmlWriter->startElement('office:meta');
50
51        // Core properties
52        $xmlWriter->writeElement('dc:title', $docProps->getTitle());
53        $xmlWriter->writeElement('dc:subject', $docProps->getSubject());
54        $xmlWriter->writeElement('dc:description', $docProps->getDescription());
55        $xmlWriter->writeElement('dc:creator', $docProps->getLastModifiedBy());
56        $xmlWriter->writeElement('dc:date', gmdate($this->dateFormat, $docProps->getModified()));
57
58        // Extended properties
59        $xmlWriter->writeElement('meta:generator', 'PHPWord');
60        $xmlWriter->writeElement('meta:initial-creator', $docProps->getCreator());
61        $xmlWriter->writeElement('meta:creation-date', gmdate($this->dateFormat, $docProps->getCreated()));
62        $xmlWriter->writeElement('meta:keyword', $docProps->getKeywords());
63
64        // Category, company, and manager are put in meta namespace
65        $properties = ['Category', 'Company', 'Manager'];
66        foreach ($properties as $property) {
67            $method = "get{$property}";
68            if ($docProps->$method() !== null) {
69                $this->writeCustomProperty($xmlWriter, $property, $docProps->$method());
70            }
71        }
72
73        // Other custom properties
74        // @todo Check type. Currently all assumed as string
75        foreach ($docProps->getCustomProperties() as $property) {
76            $value = $docProps->getCustomPropertyValue($property);
77            $this->writeCustomProperty($xmlWriter, $property, $value);
78        }
79
80        $xmlWriter->endElement(); // office:meta
81        $xmlWriter->endElement(); // office:document-meta
82
83        return $xmlWriter->getData();
84    }
85
86    /**
87     * Write individual property.
88     *
89     * @param string $property
90     * @param string $value
91     *
92     * @todo Handle other `$type`: double|date|dateTime|duration|boolean (4th arguments)
93     */
94    private function writeCustomProperty(XMLWriter $xmlWriter, $property, $value): void
95    {
96        $xmlWriter->startElement('meta:user-defined');
97        $xmlWriter->writeAttribute('meta:name', $property);
98        // if ($type !== null) {
99        //     $xmlWriter->writeAttribute('meta:value-type', $type);
100        // }
101        $this->writeText($value);
102        $xmlWriter->endElement(); // meta:user-defined
103    }
104}