Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
PreserveText
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
1 / 1
5
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
20/**
21 * PreserveText element writer.
22 *
23 * @since 0.10.0
24 */
25class PreserveText extends Text
26{
27    /**
28     * Write preserve text element.
29     */
30    public function write(): void
31    {
32        $xmlWriter = $this->getXmlWriter();
33        $element = $this->getElement();
34        if (!$element instanceof \PhpOffice\PhpWord\Element\PreserveText) {
35            return;
36        }
37
38        $texts = $element->getText();
39        if (!is_array($texts)) {
40            $texts = [$texts];
41        }
42
43        $this->startElementP();
44
45        foreach ($texts as $text) {
46            if (substr($text, 0, 1) == '{') {
47                $text = substr($text, 1, -1);
48
49                $xmlWriter->startElement('w:r');
50                $xmlWriter->startElement('w:fldChar');
51                $xmlWriter->writeAttribute('w:fldCharType', 'begin');
52                $xmlWriter->endElement();
53                $xmlWriter->endElement();
54
55                $xmlWriter->startElement('w:r');
56
57                $this->writeFontStyle();
58
59                $xmlWriter->startElement('w:instrText');
60                $xmlWriter->writeAttribute('xml:space', 'preserve');
61                $this->writeText($text);
62                $xmlWriter->endElement();
63                $xmlWriter->endElement();
64
65                $xmlWriter->startElement('w:r');
66                $xmlWriter->startElement('w:fldChar');
67                $xmlWriter->writeAttribute('w:fldCharType', 'separate');
68                $xmlWriter->endElement();
69                $xmlWriter->endElement();
70
71                $xmlWriter->startElement('w:r');
72                $xmlWriter->startElement('w:fldChar');
73                $xmlWriter->writeAttribute('w:fldCharType', 'end');
74                $xmlWriter->endElement();
75                $xmlWriter->endElement();
76            } else {
77                $xmlWriter->startElement('w:r');
78
79                $this->writeFontStyle();
80
81                $xmlWriter->startElement('w:t');
82                $xmlWriter->writeAttribute('xml:space', 'preserve');
83                $this->writeText($this->getText($text));
84                $xmlWriter->endElement();
85                $xmlWriter->endElement();
86            }
87        }
88
89        $this->endElementP(); // w:p
90    }
91}