Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Meta
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 getXmlWriter
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
3
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\EPub3\Part;
20
21use PhpOffice\PhpWord\Shared\XMLWriter;
22
23/**
24 * Class for EPub3 metadata part.
25 */
26class Meta extends AbstractPart
27{
28    /**
29     * Get XML Writer.
30     */
31    protected function getXmlWriter(): XMLWriter
32    {
33        $xmlWriter = new XMLWriter();
34        $xmlWriter->openMemory();
35        $xmlWriter->startDocument('1.0', 'UTF-8');
36
37        return $xmlWriter;
38    }
39
40    /**
41     * Write part content.
42     */
43    public function write(): string
44    {
45        $xmlWriter = $this->getXmlWriter();
46
47        $xmlWriter->startElement('metadata');
48        $xmlWriter->writeAttribute('xmlns', 'http://www.idpf.org/2007/opf');
49        $xmlWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
50
51        // Write basic metadata
52        $title = $this->getParentWriter()->getPhpWord()->getDocInfo()->getTitle() ?: 'Sample EPub3 Document';
53        $xmlWriter->writeRaw('<dc:title>' . htmlspecialchars($title, ENT_QUOTES) . '</dc:title>');
54        $xmlWriter->writeElement('dc:language', 'en');
55        $xmlWriter->writeElement('dc:identifier', 'urn:uuid:12345');
56        $xmlWriter->writeAttribute('id', 'bookid');
57
58        // Write document info if available
59        $docInfo = $this->getParentWriter()->getPhpWord()->getDocInfo();
60        if ($docInfo->getCreator()) {
61            $xmlWriter->writeElement('dc:creator', $docInfo->getCreator());
62        }
63
64        // Write modification date
65        $xmlWriter->startElement('meta');
66        $xmlWriter->writeAttribute('property', 'dcterms:modified');
67        $xmlWriter->text('2023-01-01T00:00:00Z');
68        $xmlWriter->endElement();
69
70        $xmlWriter->endElement(); // metadata
71
72        return $xmlWriter->getData();
73    }
74}