Skip to content

Commit 4f0d6f7

Browse files
authored
Merge pull request #1717 from Samuel-BF/rtf-basic-fields
Add support for basic fields in RTF writer.
2 parents dfa0b5f + 7628b41 commit 4f0d6f7

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
* @copyright 2019 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Writer\RTF\Element;
19+
20+
/**
21+
* Field element writer
22+
*
23+
* Note: for now, only date, page and numpages fields are implemented for RTF.
24+
*/
25+
class Field extends Text
26+
{
27+
/**
28+
* Write field element.
29+
*/
30+
public function write()
31+
{
32+
$element = $this->element;
33+
if (!$element instanceof \PhpOffice\PhpWord\Element\Field) {
34+
return;
35+
}
36+
37+
$this->getStyles();
38+
39+
$content = '';
40+
$content .= $this->writeOpening();
41+
$content .= '{';
42+
$content .= $this->writeFontStyle();
43+
44+
$methodName = 'write' . ucfirst(strtolower($element->getType()));
45+
if (!method_exists($this, $methodName)) {
46+
// Unsupported field
47+
$content .= '';
48+
} else {
49+
$content .= '\\field{\\*\\fldinst ';
50+
$content .= $this->$methodName($element);
51+
$content .= '}{\\fldrslt}';
52+
}
53+
$content .= '}';
54+
$content .= $this->writeClosing();
55+
56+
return $content;
57+
}
58+
59+
protected function writePage()
60+
{
61+
return 'PAGE';
62+
}
63+
64+
protected function writeNumpages()
65+
{
66+
return 'NUMPAGES';
67+
}
68+
69+
protected function writeDate(\PhpOffice\PhpWord\Element\Field $element)
70+
{
71+
$content = '';
72+
$content .= 'DATE';
73+
$properties = $element->getProperties();
74+
if (isset($properties['dateformat'])) {
75+
$content .= ' \\\\@ "' . $properties['dateformat'] . '"';
76+
}
77+
78+
return $content;
79+
}
80+
}

tests/PhpWord/Writer/RTF/ElementTest.php

+37-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ElementTest extends \PHPUnit\Framework\TestCase
2929
*/
3030
public function testUnmatchedElements()
3131
{
32-
$elements = array('Container', 'Text', 'Title', 'Link', 'Image', 'Table');
32+
$elements = array('Container', 'Text', 'Title', 'Link', 'Image', 'Table', 'Field');
3333
foreach ($elements as $element) {
3434
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $element;
3535
$parentWriter = new RTF();
@@ -39,4 +39,40 @@ public function testUnmatchedElements()
3939
$this->assertEquals('', $object->write());
4040
}
4141
}
42+
43+
public function testPageField()
44+
{
45+
$parentWriter = new RTF();
46+
$element = new \PhpOffice\PhpWord\Element\Field('PAGE');
47+
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
48+
49+
$this->assertEquals("{\\field{\\*\\fldinst PAGE}{\\fldrslt}}\\par\n", $field->write());
50+
}
51+
52+
public function testNumpageField()
53+
{
54+
$parentWriter = new RTF();
55+
$element = new \PhpOffice\PhpWord\Element\Field('NUMPAGES');
56+
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
57+
58+
$this->assertEquals("{\\field{\\*\\fldinst NUMPAGES}{\\fldrslt}}\\par\n", $field->write());
59+
}
60+
61+
public function testDateField()
62+
{
63+
$parentWriter = new RTF();
64+
$element = new \PhpOffice\PhpWord\Element\Field('DATE', array('dateformat' => 'd MM yyyy H:mm:ss'));
65+
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
66+
67+
$this->assertEquals("{\\field{\\*\\fldinst DATE \\\\@ \"d MM yyyy H:mm:ss\"}{\\fldrslt}}\\par\n", $field->write());
68+
}
69+
70+
public function testIndexField()
71+
{
72+
$parentWriter = new RTF();
73+
$element = new \PhpOffice\PhpWord\Element\Field('INDEX');
74+
$field = new \PhpOffice\PhpWord\Writer\RTF\Element\Field($parentWriter, $element);
75+
76+
$this->assertEquals("{}\\par\n", $field->write());
77+
}
4278
}

0 commit comments

Comments
 (0)