Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FormField
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
10.02
0.00% covered (danger)
0.00%
0 / 1
 write
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
10.02
1<?php
2
3/**
4 * This file is part of PHPWord - A pure PHP library for reading and writing
5 * document files.
6 *
7 * PHPWord is free software distributed under the terms of the GNU Lesser
8 * General Public License 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
19namespace PhpOffice\PhpWord\Writer\ODText\Element;
20
21use PhpOffice\PhpWord\Element\FormField as FormFieldElement;
22
23/**
24 * FormField element writer.
25 */
26class FormField extends Control
27{
28    public function write(): void
29    {
30        $element = $this->getElement();
31        if (!$element instanceof FormFieldElement) {
32            return;
33        }
34        $xmlWriter = $this->getXmlWriter();
35        if (!$this->withoutP) {
36            $xmlWriter->startElement('text:p');
37            $xmlWriter->writeAttribute('text:style-name', $element->getParagraphStyle() ?: 'Normal');
38        }
39        $text = null;
40        if ($element->getType() === 'textinput') {
41            $text = $element->getValue() ?? $element->getDefault();
42        } elseif ($element->getType() === 'dropdown' && $element->getEntries()) {
43            $index = $element->getValue() ?? $element->getDefault();
44            $text = $element->getEntries()[(int) $index] ?? null;
45        }
46        $this->writeControlText($element, $text === null ? null : (string) $text);
47        $this->writeControlAnchor('control-' . $element->getElementId(), $element->getName(), $text === null ? null : (string) $text, $element->getFontStyle());
48        if (!$this->withoutP) {
49            $xmlWriter->endElement();
50        }
51    }
52}