Skip to content

Commit

Permalink
Added support for PHP 8.1
Browse files Browse the repository at this point in the history
- Fixed "Passing null to parameter 1 ($version) of type string is deprecated"
- Updated unit tests
  • Loading branch information
josemmo committed May 25, 2021
1 parent ff1e44c commit 60b5996
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/UXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,13 @@ public function asText(): string {
* @return string XML string
*/
public function asXML(?string $version="1.0", string $encoding="UTF-8", bool $format=true): string {
$doc = new DOMDocument($version, $encoding);
$doc = new DOMDocument();
if ($version === null) {
$doc->xmlStandalone = true;
} else {
$doc->xmlVersion = $version;
}
$doc->encoding = $encoding;
$doc->formatOutput = $format;
$doc->appendChild($doc->importNode($this->element, true));
$res = ($version === null) ? $doc->saveXML($doc->documentElement) : $doc->saveXML();
Expand All @@ -209,6 +215,6 @@ public function asXML(?string $version="1.0", string $encoding="UTF-8", bool $fo
* @inheritdoc
*/
public function __toString(): string {
return $this->asXML(null, '', false);
return $this->asXML(null, 'UTF-8', false);
}
}
13 changes: 13 additions & 0 deletions tests/UXMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public function testCanHandleSpecialCharacters(): void {
UXML::newInstance('Test&Fail', 'Not a valid tag name');
}

public function testCanExportXml(): void {
$xml = UXML::newInstance('Root');
$this->assertEquals('<Root/>', (string) $xml);
$this->assertEquals('<Root/>', $xml->asXML(null));
$this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Root/>\n", $xml->asXML());
$this->assertEquals("<?xml version=\"1.1\" encoding=\"ISO-8859-1\"?>\n<Root/>\n", $xml->asXML('1.1', 'ISO-8859-1'));

$xml = UXML::fromString('<a><b>1</b><b>2</b></a>');
$this->assertEquals("<a>\n <b>1</b>\n <b>2</b>\n</a>", $xml->asXML(null));
$this->assertEquals('<a><b>1</b><b>2</b></a>', (string) $xml);
$this->assertEquals('<a><b>1</b><b>2</b></a>', $xml->asXML(null, 'UTF-8', false));
}

public function testCanLoadXml(): void {
$source = "<fruits>";
$source .= "<fruit>Banana</fruit>";
Expand Down

0 comments on commit 60b5996

Please sign in to comment.