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