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