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