Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Title
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
2 / 2
8
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
7
 compareToFirstElement
100.00% covered (success)
100.00%
1 / 1
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\Element;
20
21/**
22 * Title element writer.
23 *
24 * @since 0.11.0
25 */
26class Title extends AbstractElement
27{
28    /**
29     * Write element.
30     */
31    public function write(): void
32    {
33        $xmlWriter = $this->getXmlWriter();
34        $element = $this->getElement();
35        if (!$element instanceof \PhpOffice\PhpWord\Element\Title) {
36            return;
37        }
38
39        $xmlWriter->startElement('text:h');
40        $hdname = 'HD';
41        $sect = $element->getParent();
42        if ($sect instanceof \PhpOffice\PhpWord\Element\Section) {
43            if (self::compareToFirstElement($element, $sect->getElements())) {
44                $hdname = 'HE';
45            }
46        }
47        $depth = $element->getDepth();
48        $xmlWriter->writeAttribute('text:style-name', "$hdname$depth");
49        $xmlWriter->writeAttribute('text:outline-level', $depth);
50        $xmlWriter->startElement('text:span');
51        if ($depth > 0) {
52            $xmlWriter->writeAttribute('text:style-name', 'Heading_' . $depth);
53        } else {
54            $xmlWriter->writeAttribute('text:style-name', 'Title');
55        }
56        $text = $element->getText();
57        if (is_string($text)) {
58            $this->writeText($text);
59        }
60        if ($text instanceof \PhpOffice\PhpWord\Element\AbstractContainer) {
61            $containerWriter = new Container($xmlWriter, $text);
62            $containerWriter->write();
63        }
64        $xmlWriter->endElement(); // text:span
65        $xmlWriter->endElement(); // text:h
66    }
67
68    /**
69     * Test if element is same as first element in array.
70     *
71     * @param \PhpOffice\PhpWord\Element\AbstractElement $elem
72     * @param \PhpOffice\PhpWord\Element\AbstractElement[] $elemarray
73     *
74     * @return bool
75     */
76    private static function compareToFirstElement($elem, $elemarray)
77    {
78        return $elem === $elemarray[0];
79    }
80}