Skip to content

Commit ee2dcdd

Browse files
committed
Merge branch 'support_for_xe_and_index_field' into develop
2 parents 7ce54ab + 6a84b8e commit ee2dcdd

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

src/PhpWord/Element/Field.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Field extends AbstractElement
6060
),
6161
'INDEX'=>array(
6262
'properties' => array(),
63-
'options'=>array('PreserveFormat')
63+
'options' => array('PreserveFormat')
6464
)
6565
);
6666

@@ -122,7 +122,7 @@ public function setType($type = null)
122122
if (isset($this->fieldsArray[$type])) {
123123
$this->type = $type;
124124
} else {
125-
throw new \InvalidArgumentException("Invalid type");
125+
throw new \InvalidArgumentException("Invalid type '$type'");
126126
}
127127
}
128128
return $this->type;
@@ -152,7 +152,7 @@ public function setProperties($properties = array())
152152
if (is_array($properties)) {
153153
foreach (array_keys($properties) as $propkey) {
154154
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
155-
throw new \InvalidArgumentException("Invalid property");
155+
throw new \InvalidArgumentException("Invalid property '$propkey'");
156156
}
157157
}
158158
$this->properties = array_merge($this->properties, $properties);
@@ -183,8 +183,8 @@ public function setOptions($options = array())
183183
{
184184
if (is_array($options)) {
185185
foreach (array_keys($options) as $optionkey) {
186-
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey]))) {
187-
throw new \InvalidArgumentException("Invalid option");
186+
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
187+
throw new \InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
188188
}
189189
}
190190
$this->options = array_merge($this->options, $options);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ public function write()
7777
case 'Italic':
7878
$instruction .= '\i ';
7979
break;
80+
default:
81+
$instruction .= $option .' ';
8082
}
8183
}
8284

@@ -106,7 +108,7 @@ public function write()
106108
$xmlWriter->startElement('w:noProof');
107109
$xmlWriter->endElement(); // w:noProof
108110
$xmlWriter->endElement(); // w:rPr
109-
$xmlWriter->writeElement('w:t', '1');
111+
$xmlWriter->writeElement('w:t', $element->getText() == null ? '1' : $element->getText());
110112
$xmlWriter->endElement(); // w:r
111113

112114
$xmlWriter->startElement('w:r');

tests/PhpWord/Element/FieldTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,25 @@ public function testConstructWithTypePropertiesOptions()
7575
*/
7676
public function testConstructWithTypePropertiesOptionsText()
7777
{
78-
$oField = new Field('XE', array(), array('Bold'), 'FieldValue');
78+
$oField = new Field('XE', array(), array('Bold', 'Italic'), 'FieldValue');
7979

8080
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
8181
$this->assertEquals('XE', $oField->getType());
8282
$this->assertEquals(array(), $oField->getProperties());
83-
$this->assertEquals(array('Bold'), $oField->getOptions());
83+
$this->assertEquals(array('Bold', 'Italic'), $oField->getOptions());
8484
$this->assertEquals('FieldValue', $oField->getText());
8585
}
8686

87+
public function testConstructWithOptionValue()
88+
{
89+
$oField = new Field('INDEX', array(), array('\\c "3" \\h "A"'));
90+
91+
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
92+
$this->assertEquals('INDEX', $oField->getType());
93+
$this->assertEquals(array(), $oField->getProperties());
94+
$this->assertEquals(array('\\c "3" \\h "A"'), $oField->getOptions());
95+
}
96+
8797
/**
8898
* Test setType exception
8999
*

tests/PhpWord/Writer/Word2007/ElementTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,24 @@ public function testChartElements()
192192
}
193193
}
194194

195+
public function testFieldElement()
196+
{
197+
$phpWord = new PhpWord();
198+
$section = $phpWord->addSection();
199+
200+
$section->addField('INDEX', [], ['\\c "3"']);
201+
$section->addField('XE', [], ['Bold', 'Italic'], 'Index Entry');
202+
$section->addField('DATE', ['dateformat' => 'd-M-yyyy'], ['PreserveFormat', 'LastUsedFormat']);
203+
$section->addField('DATE', [], ['LunarCalendar']);
204+
$section->addField('DATE', [], ['SakaEraCalendar']);
205+
$section->addField('NUMPAGES', ['format' => 'roman', 'numformat' => '0,00'], ['SakaEraCalendar']);
206+
$doc = TestHelperDOCX::getDocument($phpWord);
207+
208+
$element = '/w:document/w:body/w:p/w:r/w:instrText';
209+
$this->assertTrue($doc->elementExists($element));
210+
$this->assertEquals(' INDEX \\c "3" ', $doc->getElement($element)->textContent);
211+
}
212+
195213
/**
196214
* Test form fields
197215
*/

0 commit comments

Comments
 (0)