Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Field
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 write
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
3
 writePage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeNumpages
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 writeFilename
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 writeDate
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
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
19namespace PhpOffice\PhpWord\Writer\RTF\Element;
20
21use PhpOffice\PhpWord\Element\Field as ElementField;
22
23/**
24 * Field element writer.
25 *
26 * Note: for now, only date, page, numpages and filename fields are implemented for RTF.
27 */
28class Field extends Text
29{
30    /**
31     * Write field element.
32     */
33    public function write()
34    {
35        $element = $this->element;
36        if (!$element instanceof ElementField) {
37            return;
38        }
39
40        $this->getStyles();
41
42        $content = '';
43        $content .= $this->writeOpening();
44        $content .= '{';
45        $content .= $this->writeFontStyle();
46
47        $methodName = 'write' . ucfirst(strtolower($element->getType()));
48        if (!method_exists($this, $methodName)) {
49            // Unsupported field
50            $content .= '';
51        } else {
52            $content .= '\\field{\\*\\fldinst ';
53            $content .= $this->$methodName($element);
54            $content .= '}{\\fldrslt}';
55        }
56        $content .= '}';
57        $content .= $this->writeClosing();
58
59        return $content;
60    }
61
62    protected function writePage()
63    {
64        return 'PAGE';
65    }
66
67    protected function writeNumpages()
68    {
69        return 'NUMPAGES';
70    }
71
72    protected function writeFilename(ElementField $element): string
73    {
74        $content = 'FILENAME';
75        $options = $element->getOptions();
76        if ($options != null && in_array('Path', $options)) {
77            $content .= ' \\\\p';
78        }
79
80        return $content;
81    }
82
83    protected function writeDate(ElementField $element)
84    {
85        $content = '';
86        $content .= 'DATE';
87        $properties = $element->getProperties();
88        if (isset($properties['dateformat'])) {
89            $content .= ' \\\\@ "' . $properties['dateformat'] . '"';
90        }
91
92        return $content;
93    }
94}