Remove dependency to the second parameter of XML::fromArray

This commit is contained in:
Hypolite Petovan 2022-12-31 11:59:19 -05:00
parent 931ccde90d
commit 8bbf9e93c6
10 changed files with 23 additions and 35 deletions

View File

@ -36,9 +36,7 @@ function oexchange_init(App $a)
$baseURL = DI::baseUrl()->get(); $baseURL = DI::baseUrl()->get();
$xml = null; $xmlString = XML::fromArray([
XML::fromArray([
'XRD' => [ 'XRD' => [
'@attributes' => [ '@attributes' => [
'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0', '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) function oexchange_content(App $a)

View File

@ -294,7 +294,7 @@ class System
} }
DI::apiResponse()->setType(Response::TYPE_XML); 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()); DI::page()->exit(DI::apiResponse()->generate());
self::exit(); self::exit();

View File

@ -99,7 +99,7 @@ class ApiResponse extends Response
$data3 = [$root_element => $data2]; $data3 = [$root_element => $data2];
return XML::fromArray($data3, $xml, false, $namespaces); return XML::fromArray($data3, $dummy, false, $namespaces);
} }
/** /**

View File

@ -43,8 +43,6 @@ class OpenSearch extends BaseModule
$baseUrl = DI::baseUrl()->get(); $baseUrl = DI::baseUrl()->get();
/** @var DOMDocument $xml */ /** @var DOMDocument $xml */
$xml = null;
XML::fromArray([ XML::fromArray([
'OpenSearchDescription' => [ 'OpenSearchDescription' => [
'@attributes' => [ '@attributes' => [

View File

@ -34,7 +34,6 @@ class ReallySimpleDiscovery extends BaseModule
{ {
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$xml = null;
$content = XML::fromArray([ $content = XML::fromArray([
'rsd' => [ 'rsd' => [
'@attributes' => [ '@attributes' => [
@ -67,7 +66,7 @@ class ReallySimpleDiscovery extends BaseModule
], ],
], ],
], ],
], $xml); ]);
System::httpExit($content, Response::TYPE_XML); System::httpExit($content, Response::TYPE_XML);
} }
} }

View File

@ -48,7 +48,6 @@ class HostMeta extends BaseModule
$domain = DI::baseUrl()->get(); $domain = DI::baseUrl()->get();
$xml = null;
XML::fromArray([ XML::fromArray([
'XRD' => [ 'XRD' => [
'@attributes' => [ '@attributes' => [

View File

@ -230,9 +230,7 @@ class Xrd extends BaseModule
{ {
$baseURL = $this->baseUrl->get(); $baseURL = $this->baseUrl->get();
$xml = null; $xmlString = XML::fromArray([
XML::fromArray([
'XRD' => [ 'XRD' => [
'@attributes' => [ '@attributes' => [
'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0', '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: *'); header('Access-Control-Allow-Origin: *');
System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml'); System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
} }
} }

View File

@ -2855,7 +2855,7 @@ class Diaspora
$namespaces = ['me' => ActivityNamespace::SALMON_ME]; $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 * @param array $message The message data
* *
* @return string The post XML * @return string The post XML
* @throws \Exception
*/ */
public static function buildPostXml(string $type, array $message): string public static function buildPostXml(string $type, array $message): string
{ {
$data = [$type => $message]; return XML::fromArray([$type => $message]);
return XML::fromArray($data, $xml);
} }
/** /**

View File

@ -160,7 +160,7 @@ class Salmon
$namespaces = ['me' => ActivityNamespace::SALMON_ME]; $namespaces = ['me' => ActivityNamespace::SALMON_ME];
$salmon = XML::fromArray($xmldata, $xml, false, $namespaces); $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces);
// slap them // slap them
$postResult = DI::httpClient()->post($url, $salmon, [ $postResult = DI::httpClient()->post($url, $salmon, [
@ -187,9 +187,7 @@ class Salmon
] ]
]; ];
$namespaces = ['me' => ActivityNamespace::SALMON_ME]; $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces);
$salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
// slap them // slap them
$postResult = DI::httpClient()->post($url, $salmon, [ $postResult = DI::httpClient()->post($url, $salmon, [
@ -214,9 +212,7 @@ class Salmon
] ]
]; ];
$namespaces = ['me' => ActivityNamespace::SALMON_ME]; $salmon = XML::fromArray($xmldata, $dummy, false, $namespaces);
$salmon = XML::fromArray($xmldata, $xml, false, $namespaces);
// slap them // slap them
$postResult = DI::httpClient()->post($url, $salmon, [ $postResult = DI::httpClient()->post($url, $salmon, [

View File

@ -37,14 +37,15 @@ class XML
/** /**
* Creates an XML structure out of a given array * Creates an XML structure out of a given array
* *
* @param array $array The array of the XML structure that will be generated * @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 object|null $xml The created XML will be returned by reference
* @param bool $remove_header Should the XML header be removed or not? * @param bool $remove_header Should the XML header be removed or not?
* @param array $namespaces List of namespaces * @param array $namespaces List of namespaces
* @param bool $root interally used parameter. Mustn't be used from outside. * @param bool $root interally used parameter. Mustn't be used from outside.
* @return string * @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) { if ($root) {
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
@ -125,7 +126,7 @@ class XML
if (!is_array($value)) { if (!is_array($value)) {
$element = $xml->addChild($key, self::escape($value ?? ''), $namespace); $element = $xml->addChild($key, self::escape($value ?? ''), $namespace);
} elseif (is_array($value)) { } else {
$element = $xml->addChild($key, null, $namespace); $element = $xml->addChild($key, null, $namespace);
self::fromArray($value, $element, $remove_header, $namespaces, false); self::fromArray($value, $element, $remove_header, $namespaces, false);
} }