Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
Text
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
4
 writeOpeningTrackChange
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
 writeClosingTrackChange
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
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\Element;
19
20use PhpOffice\PhpWord\Element\TrackChange;
21
22/**
23 * Text element writer.
24 *
25 * @since 0.10.0
26 */
27class Text extends AbstractElement
28{
29    /**
30     * Write text element.
31     */
32    public function write(): void
33    {
34        $xmlWriter = $this->getXmlWriter();
35        $element = $this->getElement();
36        if (!$element instanceof \PhpOffice\PhpWord\Element\Text) {
37            return;
38        }
39
40        $this->startElementP();
41
42        $this->writeOpeningTrackChange();
43
44        $xmlWriter->startElement('w:r');
45
46        $this->writeFontStyle();
47
48        $textElement = 'w:t';
49        //'w:delText' in case of deleted text
50        $changed = $element->getTrackChange();
51        if ($changed != null && $changed->getChangeType() == TrackChange::DELETED) {
52            $textElement = 'w:delText';
53        }
54        $xmlWriter->startElement($textElement);
55
56        $xmlWriter->writeAttribute('xml:space', 'preserve');
57        $this->writeText($this->getText($element->getText()));
58        $xmlWriter->endElement();
59        $xmlWriter->endElement(); // w:r
60
61        $this->writeClosingTrackChange();
62
63        $this->endElementP(); // w:p
64    }
65
66    /**
67     * Write opening of changed element.
68     */
69    protected function writeOpeningTrackChange(): void
70    {
71        $changed = $this->getElement()->getTrackChange();
72        if ($changed == null) {
73            return;
74        }
75
76        $xmlWriter = $this->getXmlWriter();
77
78        if (($changed->getChangeType() == TrackChange::INSERTED)) {
79            $xmlWriter->startElement('w:ins');
80        } elseif ($changed->getChangeType() == TrackChange::DELETED) {
81            $xmlWriter->startElement('w:del');
82        }
83        $xmlWriter->writeAttribute('w:author', $changed->getAuthor());
84        if ($changed->getDate() != null) {
85            $xmlWriter->writeAttribute('w:date', $changed->getDate()->format('Y-m-d\TH:i:s\Z'));
86        }
87        $xmlWriter->writeAttribute('w:id', $this->getElement()->getElementId());
88    }
89
90    /**
91     * Write ending.
92     */
93    protected function writeClosingTrackChange(): void
94    {
95        $changed = $this->getElement()->getTrackChange();
96        if ($changed == null) {
97            return;
98        }
99
100        $xmlWriter = $this->getXmlWriter();
101
102        $xmlWriter->endElement(); // w:ins|w:del
103    }
104}