Add possiblity to add not defined options

This commit is contained in:
antoine 2017-06-07 02:26:59 +02:00
parent 0953065b8c
commit 6a84b8ed26
4 changed files with 38 additions and 8 deletions

View File

@ -60,7 +60,7 @@ class Field extends AbstractElement
), ),
'INDEX'=>array( 'INDEX'=>array(
'properties' => array(), 'properties' => array(),
'options'=>array('PreserveFormat') 'options' => array('PreserveFormat')
) )
); );
@ -122,7 +122,7 @@ class Field extends AbstractElement
if (isset($this->fieldsArray[$type])) { if (isset($this->fieldsArray[$type])) {
$this->type = $type; $this->type = $type;
} else { } else {
throw new \InvalidArgumentException("Invalid type"); throw new \InvalidArgumentException("Invalid type '$type'");
} }
} }
return $this->type; return $this->type;
@ -152,7 +152,7 @@ class Field extends AbstractElement
if (is_array($properties)) { if (is_array($properties)) {
foreach (array_keys($properties) as $propkey) { foreach (array_keys($properties) as $propkey) {
if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) { if (!(isset($this->fieldsArray[$this->type]['properties'][$propkey]))) {
throw new \InvalidArgumentException("Invalid property"); throw new \InvalidArgumentException("Invalid property '$propkey'");
} }
} }
$this->properties = array_merge($this->properties, $properties); $this->properties = array_merge($this->properties, $properties);
@ -183,8 +183,8 @@ class Field extends AbstractElement
{ {
if (is_array($options)) { if (is_array($options)) {
foreach (array_keys($options) as $optionkey) { foreach (array_keys($options) as $optionkey) {
if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey]))) { if (!(isset($this->fieldsArray[$this->type]['options'][$optionkey])) && substr($optionkey, 0, 1) !== '\\') {
throw new \InvalidArgumentException("Invalid option"); throw new \InvalidArgumentException("Invalid option '$optionkey', possible values are " . implode(', ', $this->fieldsArray[$this->type]['options']));
} }
} }
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);

View File

@ -77,6 +77,8 @@ class Field extends Text
case 'Italic': case 'Italic':
$instruction .= '\i '; $instruction .= '\i ';
break; break;
default:
$instruction .= $option .' ';
} }
} }
@ -106,7 +108,7 @@ class Field extends Text
$xmlWriter->startElement('w:noProof'); $xmlWriter->startElement('w:noProof');
$xmlWriter->endElement(); // w:noProof $xmlWriter->endElement(); // w:noProof
$xmlWriter->endElement(); // w:rPr $xmlWriter->endElement(); // w:rPr
$xmlWriter->writeElement('w:t', '1'); $xmlWriter->writeElement('w:t', $element->getText() == null ? '1' : $element->getText());
$xmlWriter->endElement(); // w:r $xmlWriter->endElement(); // w:r
$xmlWriter->startElement('w:r'); $xmlWriter->startElement('w:r');

View File

@ -75,15 +75,25 @@ class FieldTest extends \PHPUnit_Framework_TestCase
*/ */
public function testConstructWithTypePropertiesOptionsText() public function testConstructWithTypePropertiesOptionsText()
{ {
$oField = new Field('XE', array(), array('Bold'), 'FieldValue'); $oField = new Field('XE', array(), array('Bold', 'Italic'), 'FieldValue');
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField); $this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
$this->assertEquals('XE', $oField->getType()); $this->assertEquals('XE', $oField->getType());
$this->assertEquals(array(), $oField->getProperties()); $this->assertEquals(array(), $oField->getProperties());
$this->assertEquals(array('Bold'), $oField->getOptions()); $this->assertEquals(array('Bold', 'Italic'), $oField->getOptions());
$this->assertEquals('FieldValue', $oField->getText()); $this->assertEquals('FieldValue', $oField->getText());
} }
public function testConstructWithOptionValue()
{
$oField = new Field('INDEX', array(), array('\\c "3" \\h "A"'));
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\Field', $oField);
$this->assertEquals('INDEX', $oField->getType());
$this->assertEquals(array(), $oField->getProperties());
$this->assertEquals(array('\\c "3" \\h "A"'), $oField->getOptions());
}
/** /**
* Test setType exception * Test setType exception
* *

View File

@ -192,6 +192,24 @@ class ElementTest extends \PHPUnit_Framework_TestCase
} }
} }
public function testFieldElement()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addField('INDEX', [], ['\\c "3"']);
$section->addField('XE', [], ['Bold', 'Italic'], 'Index Entry');
$section->addField('DATE', ['dateformat' => 'd-M-yyyy'], ['PreserveFormat', 'LastUsedFormat']);
$section->addField('DATE', [], ['LunarCalendar']);
$section->addField('DATE', [], ['SakaEraCalendar']);
$section->addField('NUMPAGES', ['format' => 'roman', 'numformat' => '0,00'], ['SakaEraCalendar']);
$doc = TestHelperDOCX::getDocument($phpWord);
$element = '/w:document/w:body/w:p/w:r/w:instrText';
$this->assertTrue($doc->elementExists($element));
$this->assertEquals(' INDEX \\c "3" ', $doc->getElement($element)->textContent);
}
/** /**
* Test form fields * Test form fields
*/ */