Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Field
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
2 / 2
15
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
6
 writeDefault
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
1 / 1
9
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// Not fully implemented
18//     - supports only PAGE, NUMPAGES, DATE and FILENAME
19//     - supports only default formats and options
20//     - supports style only if specified by name
21//     - spaces before and after field may be dropped
22
23namespace PhpOffice\PhpWord\Writer\ODText\Element;
24
25/**
26 * Field element writer.
27 *
28 * @since 0.11.0
29 */
30class Field extends Text
31{
32    /**
33     * Write field element.
34     */
35    public function write(): void
36    {
37        $element = $this->getElement();
38        if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
39            return;
40        }
41
42        $type = strtolower($element->getType());
43        switch ($type) {
44            case 'date':
45            case 'page':
46            case 'numpages':
47            case 'filename':
48                $this->writeDefault($element, $type);
49
50                break;
51        }
52    }
53
54    private function writeDefault(\PhpOffice\PhpWord\Element\Field $element, $type): void
55    {
56        $xmlWriter = $this->getXmlWriter();
57
58        $xmlWriter->startElement('text:span');
59        if (method_exists($element, 'getFontStyle')) {
60            $fstyle = $element->getFontStyle();
61            if (is_string($fstyle)) {
62                $xmlWriter->writeAttribute('text:style-name', $fstyle);
63            }
64        }
65        switch ($type) {
66            case 'date':
67                $xmlWriter->startElement('text:date');
68                $xmlWriter->writeAttribute('text:fixed', 'false');
69                $xmlWriter->endElement();
70
71                break;
72            case 'page':
73                $xmlWriter->startElement('text:page-number');
74                $xmlWriter->writeAttribute('text:fixed', 'false');
75                $xmlWriter->endElement();
76
77                break;
78            case 'numpages':
79                $xmlWriter->startElement('text:page-count');
80                $xmlWriter->endElement();
81
82                break;
83            case 'filename':
84                $xmlWriter->startElement('text:file-name');
85                $xmlWriter->writeAttribute('text:fixed', 'false');
86                $options = $element->getOptions();
87                if ($options != null && in_array('Path', $options)) {
88                    $xmlWriter->writeAttribute('text:display', 'full');
89                } else {
90                    $xmlWriter->writeAttribute('text:display', 'name');
91                }
92                $xmlWriter->endElement();
93
94                break;
95        }
96        $xmlWriter->endElement(); // text:span
97    }
98}