Skip to content

Commit 9653619

Browse files
authored
Merge pull request #980 from sergeizelenyi/dev-sdt
Add support for alias and tag on SDT elements
2 parents a7a35c6 + 1e9203a commit 9653619

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

src/PhpWord/Element/SDT.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ class SDT extends Text
4545
*/
4646
private $listItems = array();
4747

48+
/**
49+
* Alias
50+
*
51+
* @var string
52+
*/
53+
private $alias;
54+
55+
/**
56+
* Tag
57+
*
58+
* @var string
59+
*/
60+
private $tag;
61+
4862
/**
4963
* Create new instance
5064
*
@@ -126,4 +140,50 @@ public function setListItems($value)
126140

127141
return $this;
128142
}
143+
144+
/**
145+
* Get tag
146+
*
147+
* @return string
148+
*/
149+
public function getTag()
150+
{
151+
return $this->tag;
152+
}
153+
154+
/**
155+
* Set tag
156+
*
157+
* @param string $tag
158+
* @return self
159+
*/
160+
public function setTag($tag)
161+
{
162+
$this->tag = $tag;
163+
164+
return $this;
165+
}
166+
167+
/**
168+
* Get alias
169+
*
170+
* @return string
171+
*/
172+
public function getAlias()
173+
{
174+
return $this->alias;
175+
}
176+
177+
/**
178+
* Set alias
179+
*
180+
* @param string $alias
181+
* @return self
182+
*/
183+
public function setAlias($alias)
184+
{
185+
$this->alias = $alias;
186+
187+
return $this;
188+
}
129189
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,19 @@ public function write()
4141
}
4242
$type = $element->getType();
4343
$writeFormField = "write{$type}";
44+
$alias = $element->getAlias();
45+
$tag = $element->getTag();
4446

4547
$this->startElementP();
4648

4749
$xmlWriter->startElement('w:sdt');
4850

4951
// Properties
5052
$xmlWriter->startElement('w:sdtPr');
51-
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
53+
$xmlWriter->writeElementIf($alias != null, 'w:alias', 'w:val', $alias);
5254
$xmlWriter->writeElementBlock('w:lock', 'w:val', 'sdtLocked');
55+
$xmlWriter->writeElementBlock('w:id', 'w:val', rand(100000000, 999999999));
56+
$xmlWriter->writeElementIf($tag != null, 'w:tag', 'w:val', $tag);
5357
$this->$writeFormField($xmlWriter, $element);
5458
$xmlWriter->endElement(); // w:sdtPr
5559

tests/PhpWord/Element/SDTTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,20 @@ public function testConstruct()
3232
$types = array('comboBox', 'dropDownList', 'date');
3333
$type = $types[rand(0, 2)];
3434
$value = rand(0, 100);
35+
$alias = 'alias';
36+
$tag = 'my_tag';
3537
$object = new SDT($type);
3638
$object->setValue($value);
3739
$object->setListItems($types);
40+
$object->setAlias($alias);
41+
$object->setTag($tag);
3842

3943
$this->assertInstanceOf('PhpOffice\\PhpWord\\Element\\SDT', $object);
4044
$this->assertEquals($type, $object->getType());
4145
$this->assertEquals($types, $object->getListItems());
4246
$this->assertEquals($value, $object->getValue());
47+
$this->assertEquals($alias, $object->getAlias());
48+
$this->assertEquals($tag, $object->getTag());
4349
}
4450

4551
/**

tests/PhpWord/Writer/Word2007/ElementTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,17 @@ public function testSDTElements()
269269

270270
$section->addSDT('comboBox');
271271
$section->addSDT('dropDownList');
272-
$section->addSDT('date');
272+
$section->addSDT('date')->setAlias('date_alias')->setTag('my_tag');
273273

274274
$doc = TestHelperDOCX::getDocument($phpWord);
275275

276-
$path = '/w:document/w:body/w:p/w:sdt/w:sdtPr';
277-
$this->assertTrue($doc->elementExists($path . '/w:comboBox'));
278-
$this->assertTrue($doc->elementExists($path . '/w:dropDownList'));
279-
$this->assertTrue($doc->elementExists($path . '/w:date'));
276+
$path = '/w:document/w:body/w:p';
277+
278+
$this->assertTrue($doc->elementExists($path . '[1]/w:sdt/w:sdtPr/w:comboBox'));
279+
$this->assertTrue($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:dropDownList'));
280+
$this->assertFalse($doc->elementExists($path . '[2]/w:sdt/w:sdtPr/w:alias'));
281+
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:date'));
282+
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:alias'));
283+
$this->assertTrue($doc->elementExists($path . '[3]/w:sdt/w:sdtPr/w:tag'));
280284
}
281285
}

tests/PhpWord/_includes/XmlDocument.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,22 @@ public function elementExists($path, $file = 'word/document.xml')
162162

163163
return !($nodeList->length == 0);
164164
}
165+
166+
/**
167+
* Returns the xml, or part of it as a formatted string
168+
*
169+
* @param string $path
170+
* @param string $file
171+
* @return string
172+
*/
173+
public function printXml($path = '/w:document', $file = 'word/document.xml')
174+
{
175+
$newdoc = new \DOMDocument();
176+
$newdoc->formatOutput = true;
177+
$newdoc->preserveWhiteSpace = false;
178+
$node = $newdoc->importNode($this->getElement($path, $file), true);
179+
$newdoc->appendChild($node);
180+
181+
return $newdoc->saveXML($node);
182+
}
165183
}

0 commit comments

Comments
 (0)