Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SDT
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
11
0.00% covered (danger)
0.00%
0 / 1
 write
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
11
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
9 * Foundation.
10 *
11 * For the full copyright and license information, please view the LICENSE
12 * file that was distributed with this source code.
13 */
14
15namespace PhpOffice\PhpWord\Writer\ODText\Element;
16
17use PhpOffice\PhpWord\Element\SDT as SDTElement;
18
19/**
20 * Structured document tag element writer.
21 */
22class SDT extends Text
23{
24    /**
25     * Write an SDT as an inline ODF form control with visible fallback text.
26     */
27    public function write(): void
28    {
29        $element = $this->getElement();
30        if (!$element instanceof SDTElement) {
31            return;
32        }
33
34        $xmlWriter = $this->getXmlWriter();
35        $value = $element->getValue();
36        $text = $value === null ? 'Pick value' : (string) $value;
37        $id = 'sdt-' . $element->getElementId();
38
39        if (!$this->withoutP) {
40            $xmlWriter->startElement('text:p');
41            $xmlWriter->writeAttribute('text:style-name', $element->getParagraphStyle() ?: 'Normal');
42        }
43
44        $fontStyle = $element->getFontStyle();
45        if ($fontStyle) {
46            $xmlWriter->startElement('text:span');
47            if (is_string($fontStyle)) {
48                $xmlWriter->writeAttribute('text:style-name', $fontStyle);
49            }
50        }
51        $this->replaceTabs($text, $xmlWriter);
52        if ($fontStyle) {
53            $xmlWriter->endElement();
54        }
55
56        $xmlWriter->startElement('draw:control');
57        $xmlWriter->writeAttribute('draw:control', $id);
58        $xmlWriter->writeAttribute('draw:name', $element->getAlias() ?: $id);
59        if (is_string($fontStyle)) {
60            $xmlWriter->writeAttribute('draw:text-style-name', $fontStyle);
61        }
62        $xmlWriter->endElement();
63
64        if (!$this->withoutP) {
65            $xmlWriter->endElement();
66        }
67    }
68}