Skip to content

Commit

Permalink
Rebase & Fixed CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Aug 12, 2024
1 parent 2404cc8 commit 4f1ce84
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
1 change: 1 addition & 0 deletions docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions docs/usage/elements/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ For instance for the INDEX field, you can do the following (See `Index Field for
``` php
<?php

//the $fieldText can be either a simple string
// the $fieldText can be either a simple string
$fieldText = 'The index value';

//or a 'TextRun', to be able to format the text you want in the index
// or a 'TextRun', to be able to format the text you want in the index
$fieldText = new TextRun();
$fieldText->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',
]);
```
8 changes: 4 additions & 4 deletions src/PhpWord/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
],
];

/**
Expand Down
8 changes: 3 additions & 5 deletions src/PhpWord/Writer/Word2007/Element/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 15 additions & 19 deletions tests/PhpWord/Writer/Word2007/Element/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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'));
}
}

0 comments on commit 4f1ce84

Please sign in to comment.