From 60b59961a3c7144f03c4d9971f04277dd9c0085d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Miguel=20Moreno?= Date: Tue, 25 May 2021 19:54:56 +0200 Subject: [PATCH] Added support for PHP 8.1 - Fixed "Passing null to parameter 1 ($version) of type string is deprecated" - Updated unit tests --- src/UXML.php | 10 ++++++++-- tests/UXMLTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/UXML.php b/src/UXML.php index ff4b16b..dc5952b 100644 --- a/src/UXML.php +++ b/src/UXML.php @@ -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(); @@ -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); } } diff --git a/tests/UXMLTest.php b/tests/UXMLTest.php index 321850f..8b2d65a 100644 --- a/tests/UXMLTest.php +++ b/tests/UXMLTest.php @@ -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('', (string) $xml); + $this->assertEquals('', $xml->asXML(null)); + $this->assertEquals("\n\n", $xml->asXML()); + $this->assertEquals("\n\n", $xml->asXML('1.1', 'ISO-8859-1')); + + $xml = UXML::fromString('12'); + $this->assertEquals("\n 1\n 2\n", $xml->asXML(null)); + $this->assertEquals('12', (string) $xml); + $this->assertEquals('12', $xml->asXML(null, 'UTF-8', false)); + } + public function testCanLoadXml(): void { $source = ""; $source .= "Banana";