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