Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SDT
100.00% covered (success)
100.00%
43 / 43
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
3
 writePlainText
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 writeComboBox
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 writeDropDownList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeDate
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
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\SDT as SDTElement;
21use PhpOffice\PhpWord\Shared\XMLWriter;
22
23/**
24 * Structured document tag element writer.
25 *
26 * @since 0.12.0
27 * @see  http://www.datypic.com/sc/ooxml/t-w_CT_SdtBlock.html
28 *
29 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
30 */
31class SDT extends Text
32{
33    /**
34     * Write element.
35     */
36    public function write(): void
37    {
38        $xmlWriter = $this->getXmlWriter();
39        $element = $this->getElement();
40        if (!$element instanceof SDTElement) {
41            return;
42        }
43        $type = $element->getType();
44        $writeFormField = "write{$type}";
45        $alias = $element->getAlias();
46        $tag = $element->getTag();
47        $value = $element->getValue();
48        if ($value === null) {
49            $value = 'Pick value';
50        }
51
52        $this->startElementP();
53
54        $xmlWriter->startElement('w:sdt');
55
56        // Properties
57        $xmlWriter->startElement('w:sdtPr');
58        $xmlWriter->writeElementIf($alias != null, 'w:alias', 'w:val', $alias);
59        $xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked');
60        $xmlWriter->writeElementBlock('w:id', 'w:val', mt_rand(100000000, 999999999));
61        $xmlWriter->writeElementIf($tag != null, 'w:tag', 'w:val', $tag);
62        $this->$writeFormField($xmlWriter, $element);
63        $xmlWriter->endElement(); // w:sdtPr
64
65        // Content
66        $xmlWriter->startElement('w:sdtContent');
67        $xmlWriter->startElement('w:r');
68        $xmlWriter->writeElement('w:t', $value);
69        $xmlWriter->endElement(); // w:r
70        $xmlWriter->endElement(); // w:sdtContent
71
72        $xmlWriter->endElement(); // w:sdt
73
74        $this->endElementP(); // w:p
75    }
76
77    /**
78     * Write text.
79     *
80     * @see  http://www.datypic.com/sc/ooxml/t-w_CT_SdtText.html
81     */
82    private function writePlainText(XMLWriter $xmlWriter): void
83    {
84        $xmlWriter->startElement('w:text');
85        $xmlWriter->endElement(); // w:text
86    }
87
88    /**
89     * Write combo box.
90     *
91     * @see  http://www.datypic.com/sc/ooxml/t-w_CT_SdtComboBox.html
92     */
93    private function writeComboBox(XMLWriter $xmlWriter, SDTElement $element): void
94    {
95        $type = $element->getType();
96        $listItems = $element->getListItems();
97
98        $xmlWriter->startElement("w:{$type}");
99        foreach ($listItems as $key => $val) {
100            $xmlWriter->writeElementBlock('w:listItem', ['w:value' => $key, 'w:displayText' => $val]);
101        }
102        $xmlWriter->endElement(); // w:{$type}
103    }
104
105    /**
106     * Write drop down list.
107     *
108     * @see  http://www.datypic.com/sc/ooxml/t-w_CT_SdtDropDownList.html
109     */
110    private function writeDropDownList(XMLWriter $xmlWriter, SDTElement $element): void
111    {
112        $this->writeComboBox($xmlWriter, $element);
113    }
114
115    /**
116     * Write date.
117     *
118     * @see  http://www.datypic.com/sc/ooxml/t-w_CT_SdtDate.html
119     */
120    private function writeDate(XMLWriter $xmlWriter, SDTElement $element): void
121    {
122        $type = $element->getType();
123
124        $xmlWriter->startElement("w:{$type}");
125        $xmlWriter->writeElementBlock('w:dateFormat', 'w:val', 'd/M/yyyy');
126        $xmlWriter->writeElementBlock('w:lid', 'w:val', 'en-US');
127        $xmlWriter->writeElementBlock('w:storeMappedDataAs', 'w:val', 'dateTime');
128        $xmlWriter->writeElementBlock('w:calendar', 'w:val', 'gregorian');
129        $xmlWriter->endElement(); // w:date
130    }
131}