mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-03-14 12:23:33 +00:00
Strip namespaces from namespaced XML plugin response, so we get usuable output
This commit is contained in:
parent
b96a96155d
commit
463daea1c9
2 changed files with 50 additions and 2 deletions
|
|
@ -18,7 +18,7 @@ class XmlResponseParser implements ResponseParser
|
|||
}
|
||||
|
||||
try {
|
||||
$xml = simplexml_load_string($response->body());
|
||||
$xml = $this->simplexml_load_string_strip_namespaces($response->body());
|
||||
if ($xml === false) {
|
||||
throw new Exception('Invalid XML content');
|
||||
}
|
||||
|
|
@ -43,4 +43,25 @@ class XmlResponseParser implements ResponseParser
|
|||
|
||||
return $array;
|
||||
}
|
||||
|
||||
function simplexml_load_string_strip_namespaces($xml_response) {
|
||||
$xml = simplexml_load_string($xml_response);
|
||||
if ($xml === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$namespaces = array_keys($xml->getDocNamespaces(true));
|
||||
$namespaces = array_filter($namespaces, function($name) { return !empty($name); });
|
||||
if (count($namespaces) == 0) {
|
||||
return $xml;
|
||||
}
|
||||
$namespaces = array_map(function($ns) { return "$ns:"; }, $namespaces);
|
||||
|
||||
$xml_no_namespaces = str_replace(
|
||||
array_merge(["xmlns="], $namespaces),
|
||||
array_merge(["ns="], array_fill(0, count($namespaces), '')),
|
||||
$xml_response
|
||||
);
|
||||
return simplexml_load_string($xml_no_namespaces);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ test('plugin parses JSON responses correctly', function (): void {
|
|||
]);
|
||||
});
|
||||
|
||||
test('plugin parses XML responses and wraps under rss key', function (): void {
|
||||
test('plugin parses RSS XML responses and wraps under rss key', function (): void {
|
||||
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0">
|
||||
<channel>
|
||||
|
|
@ -73,6 +73,33 @@ test('plugin parses XML responses and wraps under rss key', function (): void {
|
|||
expect($plugin->data_payload['rss']['channel']['item'])->toHaveCount(2);
|
||||
});
|
||||
|
||||
test('plugin parses namespaces XML responses and wraps under root key', function (): void {
|
||||
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>
|
||||
<foo:cake version="2.0" xmlns:foo="http://example.com/foo">
|
||||
<bar:icing xmlns:bar="http://example.com/bar">
|
||||
<ontop>Cherry</ontop>
|
||||
</bar:icing>
|
||||
</foo:cake>';
|
||||
|
||||
Http::fake([
|
||||
'example.com/namespace.xml' => Http::response($xmlContent, 200, ['Content-Type' => 'application/xml']),
|
||||
]);
|
||||
|
||||
$plugin = Plugin::factory()->create([
|
||||
'data_strategy' => 'polling',
|
||||
'polling_url' => 'https://example.com/namespace.xml',
|
||||
'polling_verb' => 'get',
|
||||
]);
|
||||
|
||||
$plugin->updateDataPayload();
|
||||
|
||||
$plugin->refresh();
|
||||
|
||||
expect($plugin->data_payload)->toHaveKey('rss');
|
||||
expect($plugin->data_payload['rss'])->toHaveKey('icing');
|
||||
expect($plugin->data_payload['rss']['icing']['ontop'])->toBe('Cherry');
|
||||
});
|
||||
|
||||
test('plugin parses JSON-parsable response body as JSON', function (): void {
|
||||
$jsonContent = '{"title": "Test Data", "items": [1, 2, 3]}';
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue