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