Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Text
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
17
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
31 / 31
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        $this->writeCommentRangeStart();
57        if ($element->getTrackChange() != null && $element->getTrackChange()->getChangeType() == TrackChange::DELETED) {
58            $xmlWriter->startElement('text:change');
59            $xmlWriter->writeAttribute('text:change-id', $element->getTrackChange()->getElementId());
60            $xmlWriter->endElement();
61        } else {
62            if (empty($paragraphStyle)) {
63                if (!$this->withoutP) {
64                    $xmlWriter->writeAttribute('text:style-name', 'Normal');
65                }
66            } elseif (is_string($paragraphStyle)) {
67                if (!$this->withoutP) {
68                    $xmlWriter->writeAttribute('text:style-name', $paragraphStyle);
69                }
70            }
71
72            if (!empty($fontStyle)) {
73                // text:span
74                $xmlWriter->startElement('text:span');
75                if (is_string($fontStyle)) {
76                    $xmlWriter->writeAttribute('text:style-name', $fontStyle);
77                }
78            }
79
80            $this->writeChangeInsertion(true, $element->getTrackChange());
81            $this->replaceTabs($element->getText(), $xmlWriter);
82            $this->writeChangeInsertion(false, $element->getTrackChange());
83
84            if (!empty($fontStyle)) {
85                $xmlWriter->endElement();
86            }
87        }
88        $this->writeCommentRangeEnd();
89        if (!$this->withoutP) {
90            $xmlWriter->endElement(); // text:p
91        }
92    }
93
94    private function writeChangeInsertion($start = true, ?TrackChange $trackChange = null): void
95    {
96        if ($trackChange == null || $trackChange->getChangeType() != TrackChange::INSERTED) {
97            return;
98        }
99        $xmlWriter = $this->getXmlWriter();
100        $xmlWriter->startElement('text:change-' . ($start ? 'start' : 'end'));
101        $xmlWriter->writeAttribute('text:change-id', $trackChange->getElementId());
102        $xmlWriter->endElement();
103    }
104}