Skip to content

Commit eaba15b

Browse files
committed
Add stringify and document element shortcuts to XML Document
1 parent 3172e96 commit eaba15b

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

docs/dom.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,9 @@ use function VeeWee\Xml\Dom\Locator\document_element;
763763

764764
$doc = Document::fromXmlFile('some.xml');
765765
$rootElement = $doc->locate(document_element());
766+
767+
// Since this is a common action, there is also a shortcut:
768+
$doc->locateDocumentElement();
766769
```
767770

768771
#### elements_with_namespaced_tagname
@@ -1116,7 +1119,12 @@ Converts the DOM document to something else.
11161119
use VeeWee\Xml\Dom\Document;
11171120

11181121
$doc = Document::fromXmlFile('some.xml');
1122+
1123+
// Get full XML including the XML declaration tag:
11191124
$xml = $doc->toXmlString();
1125+
1126+
// OR, get only the XML part without declaration:
1127+
$xml = $doc->stringifyDocumentElement();
11201128
```
11211129

11221130
Instead of mapping a full document, you can also map a specific node only to XML.
@@ -1126,6 +1134,9 @@ use function VeeWee\Xml\Dom\Mapper\xml_string;
11261134

11271135
$mapper = xml_string();
11281136
$xml = $mapper($someNode);
1137+
1138+
// Or use the shortcut on Document level:
1139+
$xml = $doc->stringifyNode($someNode);
11291140
```
11301141

11311142
#### xslt_template

src/Xml/Dom/Document.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Closure;
88
use DOMDocument;
9+
use DOMElement;
910
use DOMNode;
1011
use DOMXPath;
1112
use VeeWee\Xml\Dom\Traverser\Traverser;
@@ -113,6 +114,11 @@ public function locate(callable $locator)
113114
return $locator($this->document);
114115
}
115116

117+
public function locateDocumentElement(): DOMElement
118+
{
119+
return $this->locate(Locator\document_element());
120+
}
121+
116122
/**
117123
* @param callable(DOMDocument): mixed $manipulator
118124
*
@@ -191,4 +197,20 @@ public function toXmlString(): string
191197
{
192198
return $this->map(xml_string());
193199
}
200+
201+
/**
202+
* @return non-empty-string
203+
*/
204+
public function stringifyDocumentElement(): string
205+
{
206+
return xml_string()($this->locateDocumentElement());
207+
}
208+
209+
/**
210+
* @return non-empty-string
211+
*/
212+
public function stringifyNode(DOMNode $node): string
213+
{
214+
return xml_string()($node);
215+
}
194216
}

tests/Xml/Dom/DocumentTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,34 @@ public function test_it_can_reconfigure_document(): void
144144
static::assertSame($doc1->toUnsafeDocument(), $doc2->toUnsafeDocument());
145145
static::assertXmlStringEqualsXmlString($xml1, $xml2);
146146
}
147+
148+
public function test_it_can_locate(): void
149+
{
150+
$doc = Document::fromXmlString('<hello />');
151+
$actual1 = $doc->locate(document_element());
152+
$actual2 = $doc->locateDocumentElement();
153+
154+
static::assertSame($doc->toUnsafeDocument()->documentElement, $actual1);
155+
static::assertSame($actual1, $actual2);
156+
}
157+
158+
public function test_it_can_stringify_parts(): void
159+
{
160+
$doc = Document::fromXmlString('<hello value="world"/>');
161+
$full = $doc->toXmlString();
162+
$documentElement = $doc->stringifyDocumentElement();
163+
$node = $doc->stringifyNode($doc->locateDocumentElement());
164+
$attr = $doc->stringifyNode($doc->locateDocumentElement()->getAttributeNode('value'));
165+
166+
167+
$expected = '<hello value="world"/>';
168+
169+
static::assertSame(
170+
'<?xml version="1.0"?>'.PHP_EOL.$expected.PHP_EOL,
171+
$full
172+
);
173+
static::assertSame($expected, $documentElement);
174+
static::assertSame($expected, $node);
175+
static::assertSame(' value="world"', $attr);
176+
}
147177
}

0 commit comments

Comments
 (0)