Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Text
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
2 / 2
17
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
13
 writeChangeInsertion
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
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
21use PhpOffice\PhpWord\Element\TrackChange;
22use PhpOffice\PhpWord\Exception\Exception;
23
24/**
25 * Text element writer.
26 *
27 * @since 0.10.0
28 */
29class Text extends AbstractElement
30{
31    /**
32     * Write element.
33     */
34    public function write(): void
35    {
36        $xmlWriter = $this->getXmlWriter();
37        $element = $this->getElement();
38        if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
39            return;
40        }
41        $fontStyle = $element->getFontStyle();
42        $paragraphStyle = $element->getParagraphStyle();
43
44        // @todo Commented for TextRun. Should really checkout this value
45        // $fStyleIsObject = ($fontStyle instanceof Font) ? true : false;
46        //$fStyleIsObject = false;
47
48        //if ($fStyleIsObject) {
49        // Don't never be the case, because I browse all sections for cleaning all styles not declared
50        //    throw new Exception('PhpWord : $fStyleIsObject wouldn\'t be an object');
51        //}
52
53        if (!$this->withoutP) {
54            $xmlWriter->startElement('text:p'); // text:p
55        }
56        if ($element->getTrackChange() != null && $element->getTrackChange()->getChangeType() == TrackChange::DELETED) {
57            $xmlWriter->startElement('text:change');
58            $xmlWriter->writeAttribute('text:change-id', $element->getTrackChange()->getElementId());
59            $xmlWriter->endElement();
60        } else {
61            if (empty($paragraphStyle)) {
62                if (!$this->withoutP) {
63                    $xmlWriter->writeAttribute('text:style-name', 'Normal');
64                }
65            } elseif (is_string($paragraphStyle)) {
66                if (!$this->withoutP) {
67                    $xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
68                }
69            }
70
71            if (!empty($fontStyle)) {
72                // text:span
73                $xmlWriter->startElement('text:span');
74                if (is_string($fontStyle)) {
75                    $xmlWriter->writeAttribute('text:style-name', $fontStyle);
76                }
77            }
78
79            $this->writeChangeInsertion(true, $element->getTrackChange());
80            $this->replaceTabs($element->getText(), $xmlWriter);
81            $this->writeChangeInsertion(false, $element->getTrackChange());
82
83            if (!empty($fontStyle)) {
84                $xmlWriter->endElement();
85            }
86        }
87        if (!$this->withoutP) {
88            $xmlWriter->endElement(); // text:p
89        }
90    }
91
92    private function writeChangeInsertion($start = true, ?TrackChange $trackChange = null): void
93    {
94        if ($trackChange == null || $trackChange->getChangeType() != TrackChange::INSERTED) {
95            return;
96        }
97        $xmlWriter = $this->getXmlWriter();
98        $xmlWriter->startElement('text:change-' . ($start ? 'start' : 'end'));
99        $xmlWriter->writeAttribute('text:change-id', $trackChange->getElementId());
100        $xmlWriter->endElement();
101    }
102}