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