diff --git a/docs/changes/2.x/2.0.0.md b/docs/changes/2.x/2.0.0.md index a299d15b41..06a7ba3675 100644 --- a/docs/changes/2.x/2.0.0.md +++ b/docs/changes/2.x/2.0.0.md @@ -7,6 +7,7 @@ - IOFactory : Added extractVariables method to extract variables from a document [@sibalonat](https://github.com/sibalonat) in [#2515](https://github.com/PHPOffice/PHPWord/pull/2515) - PDF Writer : Documented how to specify a PDF renderer, when working with the PDF writer, as well as the three available choices by [@settermjd](https://github.com/settermjd) in [#2642](https://github.com/PHPOffice/PHPWord/pull/2642) - Word2007 Reader: Support for Paragraph Border Style by [@damienfa](https://github.com/damienfa) in [#2651](https://github.com/PHPOffice/PHPWord/pull/2651) +- Word2007 Writer: Support for field REF by [@crystoline](https://github.com/crystoline) in [#2652](https://github.com/PHPOffice/PHPWord/pull/2652) ### Bug fixes diff --git a/docs/usage/elements/field.md b/docs/usage/elements/field.md index 415b4d170e..1cafd18ef8 100644 --- a/docs/usage/elements/field.md +++ b/docs/usage/elements/field.md @@ -26,24 +26,24 @@ For instance for the INDEX field, you can do the following (See `Index Field for ``` php addText('My '); $fieldText->addText('bold index', ['bold' => true]); $fieldText->addText(' entry'); $section->addField('XE', array(), array(), $fieldText); -//this actually adds the index +// this actually adds the index $section->addField('INDEX', array(), array('\\e " " \\h "A" \\c "3"'), 'right click to update index'); -//Adding reference to a bookmark +// Adding reference to a bookmark $fieldText->addField('REF', [ - 'name' => 'bookmark' - ], [ - 'InsertParagraphNumberRelativeContext', - 'CreateHyperLink', - ]); + 'name' => 'bookmark' +], [ + 'InsertParagraphNumberRelativeContext', + 'CreateHyperLink', +]); ``` diff --git a/src/PhpWord/Element/Field.php b/src/PhpWord/Element/Field.php index ed4225f3f1..a828aaa02e 100644 --- a/src/PhpWord/Element/Field.php +++ b/src/PhpWord/Element/Field.php @@ -91,10 +91,10 @@ class Field extends AbstractElement ], 'options' => ['Path', 'PreserveFormat'], ], - 'REF' => array( - 'properties' => array('name' => ''), - 'options' => array('f', 'h', 'n', 'p', 'r', 't', 'w'), - ), + 'REF' => [ + 'properties' => ['name' => ''], + 'options' => ['f', 'h', 'n', 'p', 'r', 't', 'w'], + ], ]; /** diff --git a/src/PhpWord/Writer/Word2007/Element/Field.php b/src/PhpWord/Writer/Word2007/Element/Field.php index f393883b1b..a8be717860 100644 --- a/src/PhpWord/Writer/Word2007/Element/Field.php +++ b/src/PhpWord/Writer/Word2007/Element/Field.php @@ -228,11 +228,9 @@ private function buildPropertiesAndOptions(\PhpOffice\PhpWord\Element\Field $ele } /** - * Writes a REF field - * - * @param \PhpOffice\PhpWord\Element\Field $element + * Writes a REF field. */ - protected function writeRef(\PhpOffice\PhpWord\Element\Field $element) + protected function writeRef(\PhpOffice\PhpWord\Element\Field $element): void { $xmlWriter = $this->getXmlWriter(); $this->startElementP(); @@ -303,7 +301,7 @@ protected function writeRef(\PhpOffice\PhpWord\Element\Field $element) $this->endElementP(); // w:p } - private function convertRefOption($optionKey, $optionValue) + private function convertRefOption(string $optionKey, string $optionValue): string { if ($optionKey === 'NumberSeperatorSequence') { return '\\d ' . $optionValue; diff --git a/tests/PhpWord/Writer/Word2007/Element/FieldTest.php b/tests/PhpWord/Writer/Word2007/Element/FieldTest.php index cf8e0fc834..c76bc70057 100644 --- a/tests/PhpWord/Writer/Word2007/Element/FieldTest.php +++ b/tests/PhpWord/Writer/Word2007/Element/FieldTest.php @@ -3,38 +3,38 @@ namespace PhpOffice\PhpWord\Writer\Word2007\Element; use PhpOffice\PhpWord\PhpWord; -use PhpOffice\PhpWord\TestHelperDOCX; +use PhpOffice\PhpWordTests\TestHelperDOCX; use PHPUnit\Framework\TestCase; /** - * Test class for PhpOffice\PhpWord\Writer\Word2007\Field + * Test class for PhpOffice\PhpWord\Writer\Word2007\Field. */ class FieldTest extends TestCase { /** - * Executed before each method of the class + * Executed before each method of the class. */ - public function tearDown() + protected function tearDown(): void { TestHelperDOCX::clear(); } /** - * Test Field write + * Test Field write. */ - public function testWriteWithRefType() + public function testWriteWithRefType(): void { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $section->addField( 'REF', - array( + [ 'name' => 'my-bookmark', - ), - array( + ], + [ 'InsertParagraphNumberRelativeContext', 'CreateHyperLink', - ) + ] ); $section->addListItem('line one item'); @@ -45,18 +45,14 @@ public function testWriteWithRefType() $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); $refFieldPath = '/w:document/w:body/w:p[1]/w:r[2]/w:instrText'; - - $this->assertTrue($doc->elementExists($refFieldPath)); + self::assertTrue($doc->elementExists($refFieldPath)); $bookMarkElement = $doc->getElement($refFieldPath); - - $this->assertNotNull($bookMarkElement); - - $this->assertEquals(' REF my-bookmark \r \h ', $bookMarkElement->textContent); + self::assertNotNull($bookMarkElement); + self::assertEquals(' REF my-bookmark \r \h ', $bookMarkElement->textContent); $bookmarkPath = '/w:document/w:body/w:bookmarkStart'; - - $this->assertTrue($doc->elementExists($bookmarkPath)); - $this->assertEquals('my-bookmark', $doc->getElementAttribute("$bookmarkPath", 'w:name')); + self::assertTrue($doc->elementExists($bookmarkPath)); + self::assertEquals('my-bookmark', $doc->getElementAttribute("$bookmarkPath", 'w:name')); } }