Skip to content

Commit 60b5996

Browse files
committed
Added support for PHP 8.1
- Fixed "Passing null to parameter 1 ($version) of type string is deprecated" - Updated unit tests
1 parent ff1e44c commit 60b5996

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/UXML.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ public function asText(): string {
196196
* @return string XML string
197197
*/
198198
public function asXML(?string $version="1.0", string $encoding="UTF-8", bool $format=true): string {
199-
$doc = new DOMDocument($version, $encoding);
199+
$doc = new DOMDocument();
200+
if ($version === null) {
201+
$doc->xmlStandalone = true;
202+
} else {
203+
$doc->xmlVersion = $version;
204+
}
205+
$doc->encoding = $encoding;
200206
$doc->formatOutput = $format;
201207
$doc->appendChild($doc->importNode($this->element, true));
202208
$res = ($version === null) ? $doc->saveXML($doc->documentElement) : $doc->saveXML();
@@ -209,6 +215,6 @@ public function asXML(?string $version="1.0", string $encoding="UTF-8", bool $fo
209215
* @inheritdoc
210216
*/
211217
public function __toString(): string {
212-
return $this->asXML(null, '', false);
218+
return $this->asXML(null, 'UTF-8', false);
213219
}
214220
}

tests/UXMLTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ public function testCanHandleSpecialCharacters(): void {
3434
UXML::newInstance('Test&Fail', 'Not a valid tag name');
3535
}
3636

37+
public function testCanExportXml(): void {
38+
$xml = UXML::newInstance('Root');
39+
$this->assertEquals('<Root/>', (string) $xml);
40+
$this->assertEquals('<Root/>', $xml->asXML(null));
41+
$this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Root/>\n", $xml->asXML());
42+
$this->assertEquals("<?xml version=\"1.1\" encoding=\"ISO-8859-1\"?>\n<Root/>\n", $xml->asXML('1.1', 'ISO-8859-1'));
43+
44+
$xml = UXML::fromString('<a><b>1</b><b>2</b></a>');
45+
$this->assertEquals("<a>\n <b>1</b>\n <b>2</b>\n</a>", $xml->asXML(null));
46+
$this->assertEquals('<a><b>1</b><b>2</b></a>', (string) $xml);
47+
$this->assertEquals('<a><b>1</b><b>2</b></a>', $xml->asXML(null, 'UTF-8', false));
48+
}
49+
3750
public function testCanLoadXml(): void {
3851
$source = "<fruits>";
3952
$source .= "<fruit>Banana</fruit>";

0 commit comments

Comments
 (0)