1
0
Fork 0

Transform ApiResponse::createXML() into dynamic method

This commit is contained in:
Philipp Holzer 2021-11-12 19:56:37 +01:00
parent 319f91301d
commit f28e64299e
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
3 changed files with 19 additions and 8 deletions

View file

@ -430,6 +430,15 @@ abstract class DI
return self::$dice->create(Model\Log\ParsedLogIterator::class); return self::$dice->create(Model\Log\ParsedLogIterator::class);
} }
//
// "Module" namespace
//
public static function apiResponse(): Module\Api\ApiResponse
{
return self::$dice->create(Module\Api\ApiResponse::class);
}
// //
// "Network" namespace // "Network" namespace
// //

View file

@ -40,15 +40,17 @@ class ApiResponse
* *
* @return string The XML data * @return string The XML data
*/ */
public static function createXML(array $data, string $root_element): string public function createXML(array $data, string $root_element): string
{ {
$childname = key($data); $childname = key($data);
$data2 = array_pop($data); $data2 = array_pop($data);
$namespaces = ['' => 'http://api.twitter.com', $namespaces = [
'' => 'http://api.twitter.com',
'statusnet' => 'http://status.net/schema/api/1/', 'statusnet' => 'http://status.net/schema/api/1/',
'friendica' => 'http://friendi.ca/schema/api/1/', 'friendica' => 'http://friendi.ca/schema/api/1/',
'georss' => 'http://www.georss.org/georss']; 'georss' => 'http://www.georss.org/georss'
];
/// @todo Auto detection of needed namespaces /// @todo Auto detection of needed namespaces
if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) { if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
@ -91,7 +93,7 @@ class ApiResponse
case 'atom': case 'atom':
case 'rss': case 'rss':
case 'xml': case 'xml':
$ret = static::createXML($data, $root_element); $ret = DI::apiResponse()->createXML($data, $root_element);
break; break;
case 'json': case 'json':
default: default:

View file

@ -1114,7 +1114,7 @@ class ApiTest extends FixtureTest
'xmlns:georss="http://www.georss.org/georss">' . "\n" . 'xmlns:georss="http://www.georss.org/georss">' . "\n" .
' <data>some_data</data>' . "\n" . ' <data>some_data</data>' . "\n" .
'</root_element>' . "\n", '</root_element>' . "\n",
ApiResponse::createXML(['data' => ['some_data']], 'root_element') DI::apiResponse()->createXML(['data' => ['some_data']], 'root_element')
); );
} }
@ -1130,7 +1130,7 @@ class ApiTest extends FixtureTest
'<ok>' . "\n" . '<ok>' . "\n" .
' <data>some_data</data>' . "\n" . ' <data>some_data</data>' . "\n" .
'</ok>' . "\n", '</ok>' . "\n",
ApiResponse::createXML(['data' => ['some_data']], 'ok') DI::apiResponse()->createXML(['data' => ['some_data']], 'ok')
); );
} }