From 8bbf9e93c61079bfa556d51b3b6e19f80ab8d795 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sat, 31 Dec 2022 11:59:19 -0500 Subject: [PATCH] Remove dependency to the second parameter of XML::fromArray --- mod/oexchange.php | 8 +++----- src/Core/System.php | 2 +- src/Module/Api/ApiResponse.php | 2 +- src/Module/OpenSearch.php | 2 -- src/Module/ReallySimpleDiscovery.php | 3 +-- src/Module/WellKnown/HostMeta.php | 1 - src/Module/Xrd.php | 8 +++----- src/Protocol/Diaspora.php | 7 +++---- src/Protocol/Salmon.php | 10 +++------- src/Util/XML.php | 15 ++++++++------- 10 files changed, 23 insertions(+), 35 deletions(-) diff --git a/mod/oexchange.php b/mod/oexchange.php index c531eefdf6..4fbf7ffeb8 100644 --- a/mod/oexchange.php +++ b/mod/oexchange.php @@ -36,9 +36,7 @@ function oexchange_init(App $a) $baseURL = DI::baseUrl()->get(); - $xml = null; - - XML::fromArray([ + $xmlString = XML::fromArray([ 'XRD' => [ '@attributes' => [ 'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0', @@ -90,9 +88,9 @@ function oexchange_init(App $a) ] ], ], - ], $xml); + ]); - System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml'); + System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml'); } function oexchange_content(App $a) diff --git a/src/Core/System.php b/src/Core/System.php index 4f06bcf520..f94004a880 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -294,7 +294,7 @@ class System } DI::apiResponse()->setType(Response::TYPE_XML); - DI::apiResponse()->addContent(XML::fromArray(["result" => $result], $xml)); + DI::apiResponse()->addContent(XML::fromArray(['result' => $result])); DI::page()->exit(DI::apiResponse()->generate()); self::exit(); diff --git a/src/Module/Api/ApiResponse.php b/src/Module/Api/ApiResponse.php index a113dab84f..2d54bc1765 100644 --- a/src/Module/Api/ApiResponse.php +++ b/src/Module/Api/ApiResponse.php @@ -99,7 +99,7 @@ class ApiResponse extends Response $data3 = [$root_element => $data2]; - return XML::fromArray($data3, $xml, false, $namespaces); + return XML::fromArray($data3, $dummy, false, $namespaces); } /** diff --git a/src/Module/OpenSearch.php b/src/Module/OpenSearch.php index d5426cf384..36e8713d99 100644 --- a/src/Module/OpenSearch.php +++ b/src/Module/OpenSearch.php @@ -43,8 +43,6 @@ class OpenSearch extends BaseModule $baseUrl = DI::baseUrl()->get(); /** @var DOMDocument $xml */ - $xml = null; - XML::fromArray([ 'OpenSearchDescription' => [ '@attributes' => [ diff --git a/src/Module/ReallySimpleDiscovery.php b/src/Module/ReallySimpleDiscovery.php index 11ab3596d6..d76094003f 100644 --- a/src/Module/ReallySimpleDiscovery.php +++ b/src/Module/ReallySimpleDiscovery.php @@ -34,7 +34,6 @@ class ReallySimpleDiscovery extends BaseModule { protected function rawContent(array $request = []) { - $xml = null; $content = XML::fromArray([ 'rsd' => [ '@attributes' => [ @@ -67,7 +66,7 @@ class ReallySimpleDiscovery extends BaseModule ], ], ], - ], $xml); + ]); System::httpExit($content, Response::TYPE_XML); } } diff --git a/src/Module/WellKnown/HostMeta.php b/src/Module/WellKnown/HostMeta.php index fdebb05d71..fa6619920f 100644 --- a/src/Module/WellKnown/HostMeta.php +++ b/src/Module/WellKnown/HostMeta.php @@ -48,7 +48,6 @@ class HostMeta extends BaseModule $domain = DI::baseUrl()->get(); - $xml = null; XML::fromArray([ 'XRD' => [ '@attributes' => [ diff --git a/src/Module/Xrd.php b/src/Module/Xrd.php index 7b008549d6..ad2b259025 100644 --- a/src/Module/Xrd.php +++ b/src/Module/Xrd.php @@ -230,9 +230,7 @@ class Xrd extends BaseModule { $baseURL = $this->baseUrl->get(); - $xml = null; - - XML::fromArray([ + $xmlString = XML::fromArray([ 'XRD' => [ '@attributes' => [ 'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0', @@ -319,10 +317,10 @@ class Xrd extends BaseModule ] ], ], - ], $xml); + ]); header('Access-Control-Allow-Origin: *'); - System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml'); + System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml'); } } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index bf6bfdd63d..c0d0bc0510 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -2855,7 +2855,7 @@ class Diaspora $namespaces = ['me' => ActivityNamespace::SALMON_ME]; - return XML::fromArray($xmldata, $xml, false, $namespaces); + return XML::fromArray($xmldata, $dummy, false, $namespaces); } /** @@ -2974,12 +2974,11 @@ class Diaspora * @param array $message The message data * * @return string The post XML + * @throws \Exception */ public static function buildPostXml(string $type, array $message): string { - $data = [$type => $message]; - - return XML::fromArray($data, $xml); + return XML::fromArray([$type => $message]); } /** diff --git a/src/Protocol/Salmon.php b/src/Protocol/Salmon.php index 7685c1dd97..50d1b931c9 100644 --- a/src/Protocol/Salmon.php +++ b/src/Protocol/Salmon.php @@ -160,7 +160,7 @@ class Salmon $namespaces = ['me' => ActivityNamespace::SALMON_ME]; - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces); // slap them $postResult = DI::httpClient()->post($url, $salmon, [ @@ -187,9 +187,7 @@ class Salmon ] ]; - $namespaces = ['me' => ActivityNamespace::SALMON_ME]; - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces); // slap them $postResult = DI::httpClient()->post($url, $salmon, [ @@ -214,9 +212,7 @@ class Salmon ] ]; - $namespaces = ['me' => ActivityNamespace::SALMON_ME]; - - $salmon = XML::fromArray($xmldata, $xml, false, $namespaces); + $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces); // slap them $postResult = DI::httpClient()->post($url, $salmon, [ diff --git a/src/Util/XML.php b/src/Util/XML.php index 50ecc6d2cb..0cdf1f34ed 100644 --- a/src/Util/XML.php +++ b/src/Util/XML.php @@ -37,14 +37,15 @@ class XML /** * Creates an XML structure out of a given array * - * @param array $array The array of the XML structure that will be generated - * @param object $xml The created XML will be returned by reference - * @param bool $remove_header Should the XML header be removed or not? - * @param array $namespaces List of namespaces - * @param bool $root interally used parameter. Mustn't be used from outside. + * @param array $array The array of the XML structure that will be generated + * @param object|null $xml The created XML will be returned by reference + * @param bool $remove_header Should the XML header be removed or not? + * @param array $namespaces List of namespaces + * @param bool $root interally used parameter. Mustn't be used from outside. * @return string + * @throws \Exception */ - public static function fromArray(array $array, &$xml, bool $remove_header = false, array $namespaces = [], bool $root = true): string + public static function fromArray(array $array, object &$xml = null, bool $remove_header = false, array $namespaces = [], bool $root = true): string { if ($root) { foreach ($array as $key => $value) { @@ -125,7 +126,7 @@ class XML if (!is_array($value)) { $element = $xml->addChild($key, self::escape($value ?? ''), $namespace); - } elseif (is_array($value)) { + } else { $element = $xml->addChild($key, null, $namespace); self::fromArray($value, $element, $remove_header, $namespaces, false); }