Skip to content

Commit 5ace17f

Browse files
committed
Some Tweaks for Fields
1 parent 7fa0337 commit 5ace17f

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/PhpWord/Element/Field.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Field extends AbstractElement
9797
'options' => [
9898
'f', 'h', 'n', 'p', 'r', 't', 'w',
9999
'd' => [',', '.', ''],
100-
'NumberSeperatorSequence' => [',', '.', ''],
100+
'NumberSeperatorSequence' => [',', '.', ''], // grandfather typo
101101
'NumberSeparatorSequence' => [',', '.', ''],
102102
],
103103
],
@@ -254,7 +254,8 @@ public function getProperties()
254254
public function setOptions(array $options = [])
255255
{
256256
foreach (array_keys($options) as $optionkey) {
257-
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
257+
$optionkey = preg_replace('/^[\\\\](.)$/', '$1', $optionkey) ?? $optionkey;
258+
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey]))) {
258259
throw new InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
259260
}
260261
}

src/PhpWord/Writer/Word2007/Element/Field.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ private function buildPropertiesAndOptions(ElementField $element)
224224

225225
break;
226226
default:
227+
$option = preg_replace('/^(.)$/', '\\\\$1', $option) ?? $option;
227228
$propertiesAndOptions .= $option . ' ';
228229
}
229230
}
@@ -318,13 +319,14 @@ protected function writeRef(ElementField $element): void
318319
'InsertParagraphNumberFullContext' => '\\w',
319320
];
320321

321-
private const NUMBER_SEPARATOR_SEQUENCE = ['NumberSeperatorSequence', 'NumberSeparatorSequence', '\\d'];
322+
private const NUMBER_SEPARATOR_SEQUENCE = ['NumberSeperatorSequence', 'NumberSeparatorSequence', '\\d', 'd'];
322323

323324
private function convertRefOption(string $optionKey, string $optionValue): string
324325
{
325326
if (in_array($optionKey, self::NUMBER_SEPARATOR_SEQUENCE, true)) {
326-
return '\\d ' . $optionValue;
327+
return ($optionValue === '') ? '' : ('\\d ' . $optionValue);
327328
}
329+
$optionValue = preg_replace('/^(.)$/', '\\\\$1', $optionValue) ?? $optionValue;
328330

329331
return self::OPTION_VALUES[$optionValue] ?? (in_array($optionValue, self::OPTION_VALUES, true) ? $optionValue : '');
330332
}

tests/PhpWordTests/Writer/Word2007/Element/FieldTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testWriteWithRefType(): void
4949
],
5050
[
5151
'InsertParagraphNumberRelativeContext',
52-
'CreateHyperLink',
52+
'h', // could have been supplied as 'CreateHyperLink' or '\h'
5353
'NumberSeperatorSequence' => ',',
5454
]
5555
);

0 commit comments

Comments
 (0)