Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ContentTypes
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
4
 writeContentType
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
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 PhpOffice\PhpWord\Shared\XMLWriter;
21
22/**
23 * Word2007 contenttypes part writer: [Content_Types].xml.
24 */
25class ContentTypes extends AbstractPart
26{
27    /**
28     * Write part.
29     *
30     * @return string
31     */
32    public function write()
33    {
34        /** @var \PhpOffice\PhpWord\Writer\Word2007 $parentWriter Type hint */
35        $parentWriter = $this->getParentWriter();
36        $contentTypes = $parentWriter->getContentTypes();
37
38        $openXMLPrefix = 'application/vnd.openxmlformats-';
39        $wordMLPrefix = $openXMLPrefix . 'officedocument.wordprocessingml.';
40        $drawingMLPrefix = $openXMLPrefix . 'officedocument.drawingml.';
41        $overrides = [
42            '/docProps/core.xml' => $openXMLPrefix . 'package.core-properties+xml',
43            '/docProps/app.xml' => $openXMLPrefix . 'officedocument.extended-properties+xml',
44            '/docProps/custom.xml' => $openXMLPrefix . 'officedocument.custom-properties+xml',
45            '/word/document.xml' => $wordMLPrefix . 'document.main+xml',
46            '/word/styles.xml' => $wordMLPrefix . 'styles+xml',
47            '/word/numbering.xml' => $wordMLPrefix . 'numbering+xml',
48            '/word/settings.xml' => $wordMLPrefix . 'settings+xml',
49            '/word/theme/theme1.xml' => $openXMLPrefix . 'officedocument.theme+xml',
50            '/word/webSettings.xml' => $wordMLPrefix . 'webSettings+xml',
51            '/word/fontTable.xml' => $wordMLPrefix . 'fontTable+xml',
52            '/word/comments.xml' => $wordMLPrefix . 'comments+xml',
53        ];
54
55        $defaults = $contentTypes['default'];
56        if (!empty($contentTypes['override'])) {
57            foreach ($contentTypes['override'] as $key => $val) {
58                if ($val == 'chart') {
59                    $overrides[$key] = $drawingMLPrefix . $val . '+xml';
60                } else {
61                    $overrides[$key] = $wordMLPrefix . $val . '+xml';
62                }
63            }
64        }
65
66        $xmlWriter = $this->getXmlWriter();
67
68        $xmlWriter->startDocument('1.0', 'UTF-8', 'yes');
69        $xmlWriter->startElement('Types');
70        $xmlWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
71
72        $this->writeContentType($xmlWriter, $defaults, true);
73        $this->writeContentType($xmlWriter, $overrides, false);
74
75        $xmlWriter->endElement(); // Types
76
77        return $xmlWriter->getData();
78    }
79
80    /**
81     * Write content types element.
82     *
83     * @param \PhpOffice\PhpWord\Shared\XMLWriter $xmlWriter XML Writer
84     * @param array $parts
85     * @param bool $isDefault
86     */
87    private function writeContentType(XMLWriter $xmlWriter, $parts, $isDefault): void
88    {
89        foreach ($parts as $partName => $contentType) {
90            $partType = $isDefault ? 'Default' : 'Override';
91            $partAttribute = $isDefault ? 'Extension' : 'PartName';
92            $xmlWriter->startElement($partType);
93            $xmlWriter->writeAttribute($partAttribute, $partName);
94            $xmlWriter->writeAttribute('ContentType', $contentType);
95            $xmlWriter->endElement();
96        }
97    }
98}