Merge pull request #980 from sergeizelenyi/dev-sdt

Add support for alias and tag on SDT elements
This commit is contained in:
troosan 2017-11-06 22:09:55 +01:00 committed by GitHub
commit 9653619c48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 6 deletions

View File

@ -45,6 +45,20 @@ class SDT extends Text
*/
private $listItems = array();
/**
* Alias
*
* @var string
*/
private $alias;
/**
* Tag
*
* @var string
*/
private $tag;
/**
* Create new instance
*
@ -126,4 +140,50 @@ class SDT extends Text
return $this;
}
/**
* Get tag
*
* @return string
*/
public function getTag()
{
return $this->tag;
}
/**
* Set tag
*
* @param string $tag
* @return self
*/
public function setTag($tag)
{
$this->tag = $tag;
return $this;
}
/**
* Get alias
*
* @return string
*/
public function getAlias()
{
return $this->alias;
}
/**
* Set alias
*
* @param string $alias
* @return self
*/
public function setAlias($alias)
{
$this->alias = $alias;
return $this;
}
}

View File

@ -41,6 +41,8 @@ class SDT extends Text
}
$type = $element->getType();
$writeFormField = "write{$type}";
$alias = $element->getAlias();
$tag = $element->getTag();
$this->startElementP();
@ -48,8 +50,10 @@ class SDT extends Text
// Properties
$xmlWriter->startElement('w:sdtPr');
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
$xmlWriter->writeElementIf($alias != null, 'w:alias', 'w:val', $alias);
$xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked');
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
$xmlWriter->writeElementIf($tag != null, 'w:tag', 'w:val', $tag);
$this->$writeFormField($xmlWriter, $element);
$xmlWriter->endElement(); // w:sdtPr

View File

@ -32,14 +32,20 @@ class SDTTest extends \PHPUnit_Framework_TestCase
$types = array('comboBox', 'dropDownList', 'date');
$type = $types[rand(0, 2)];
$value = rand(0, 100);
$alias = 'alias';
$tag = 'my_tag';
$object = new SDT($type);
$object->setValue($value);
$object->setListItems($types);
$object->setAlias($alias);
$object->setTag($tag);
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object);
$this->assertEquals($type, $object->getType());
$this->assertEquals($types, $object->getListItems());
$this->assertEquals($value, $object->getValue());
$this->assertEquals($alias, $object->getAlias());
$this->assertEquals($tag, $object->getTag());
}
/**

View File

@ -269,13 +269,17 @@ class ElementTest extends \PHPUnit_Framework_TestCase
$section->addSDT('comboBox');
$section->addSDT('dropDownList');
$section->addSDT('date');
$section->addSDT('date')->setAlias('date_alias')->setTag('my_tag');
$doc = TestHelperDOCX::getDocument($phpWord);
$path = '/w:document/w:body/w:p/w:sdt/w:sdtPr';
$this->assertTrue($doc->elementExists($path . '/w:comboBox'));
$this->assertTrue($doc->elementExists($path . '/w:dropDownList'));
$this->assertTrue($doc->elementExists($path . '/w:date'));
$path = '/w:document/w:body/w:p';
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox'));
$this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList'));
$this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias'));
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date'));
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias'));
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag'));
}
}

View File

@ -162,4 +162,22 @@ class XmlDocument
return !($nodeList->length == 0);
}
/**
* Returns the xml, or part of it as a formatted string
*
* @param string $path
* @param string $file
* @return string
*/
public function printXml($path = '/w:document', $file = 'word/document.xml')
{
$newdoc = new \DOMDocument();
$newdoc->formatOutput = true;
$newdoc->preserveWhiteSpace = false;
$node = $newdoc->importNode($this->getElement($path, $file), true);
$newdoc->appendChild($node);
return $newdoc->saveXML($node);
}
}