Compare commits

..

3 commits

Author SHA1 Message Date
Benjamin Nussbaum
b267923595 feat: bump to Design Framework v2.3.0
Some checks failed
tests / ci (push) Has been cancelled
2026-02-09 13:24:04 +01:00
Jamie Shiell
9c5b5b33f5 Use root element name for root of array rather than "rss" 2026-02-09 13:20:54 +01:00
Jamie Shiell
afc29e15d5 Strip namespaces from namespaced XML plugin response, so we get usuable output 2026-02-09 13:20:54 +01:00
4 changed files with 66 additions and 18 deletions

View file

@ -18,12 +18,12 @@ 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');
}
return ['rss' => $this->xmlToArray($xml)];
return [$xml->getName() => $this->xmlToArray($xml)];
} catch (Exception $exception) {
Log::warning('Failed to parse XML response: '.$exception->getMessage());
@ -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);
}
}

View file

@ -15,7 +15,7 @@
"ext-imagick": "*",
"ext-simplexml": "*",
"ext-zip": "*",
"bnussbau/laravel-trmnl-blade": "2.1.1",
"bnussbau/laravel-trmnl-blade": "2.3.*",
"bnussbau/trmnl-pipeline-php": "^0.6.0",
"keepsuit/laravel-liquid": "^0.5.2",
"laravel/fortify": "^1.30",

14
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "60a7e51edd8408cffdb901e4a1c1684a",
"content-hash": "a4438c591a5d746f546eab261e9e1efc",
"packages": [
{
"name": "aws/aws-crt-php",
@ -214,16 +214,16 @@
},
{
"name": "bnussbau/laravel-trmnl-blade",
"version": "2.1.1",
"version": "2.3.0",
"source": {
"type": "git",
"url": "https://github.com/bnussbau/laravel-trmnl-blade.git",
"reference": "6ad96eba917ebc30ebe550e6fce4a995e94f6b35"
"reference": "e19efc6c873816395e0989d078998f02ef4f0adf"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/bnussbau/laravel-trmnl-blade/zipball/6ad96eba917ebc30ebe550e6fce4a995e94f6b35",
"reference": "6ad96eba917ebc30ebe550e6fce4a995e94f6b35",
"url": "https://api.github.com/repos/bnussbau/laravel-trmnl-blade/zipball/e19efc6c873816395e0989d078998f02ef4f0adf",
"reference": "e19efc6c873816395e0989d078998f02ef4f0adf",
"shasum": ""
},
"require": {
@ -278,7 +278,7 @@
],
"support": {
"issues": "https://github.com/bnussbau/laravel-trmnl-blade/issues",
"source": "https://github.com/bnussbau/laravel-trmnl-blade/tree/2.1.1"
"source": "https://github.com/bnussbau/laravel-trmnl-blade/tree/2.3.0"
},
"funding": [
{
@ -294,7 +294,7 @@
"type": "github"
}
],
"time": "2026-01-29T20:40:42+00:00"
"time": "2026-02-09T12:00:32+00:00"
},
{
"name": "bnussbau/trmnl-pipeline-php",

View file

@ -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('cake');
expect($plugin->data_payload['cake'])->toHaveKey('icing');
expect($plugin->data_payload['cake']['icing']['ontop'])->toBe('Cherry');
});
test('plugin parses JSON-parsable response body as JSON', function (): void {
$jsonContent = '{"title": "Test Data", "items": [1, 2, 3]}';
@ -164,8 +191,8 @@ test('plugin handles multiple URLs with mixed content types', function (): void
expect($plugin->data_payload['IDX_0'])->toBe($jsonResponse);
// Second URL should be XML wrapped under rss
expect($plugin->data_payload['IDX_1'])->toHaveKey('rss');
expect($plugin->data_payload['IDX_1']['rss']['item'])->toBe('XML Data');
expect($plugin->data_payload['IDX_1'])->toHaveKey('root');
expect($plugin->data_payload['IDX_1']['root']['item'])->toBe('XML Data');
});
test('plugin handles POST requests with XML responses', function (): void {
@ -186,11 +213,11 @@ test('plugin handles POST requests with XML responses', function (): void {
$plugin->refresh();
expect($plugin->data_payload)->toHaveKey('rss');
expect($plugin->data_payload['rss'])->toHaveKey('status');
expect($plugin->data_payload['rss'])->toHaveKey('data');
expect($plugin->data_payload['rss']['status'])->toBe('success');
expect($plugin->data_payload['rss']['data'])->toBe('test');
expect($plugin->data_payload)->toHaveKey('response');
expect($plugin->data_payload['response'])->toHaveKey('status');
expect($plugin->data_payload['response'])->toHaveKey('data');
expect($plugin->data_payload['response']['status'])->toBe('success');
expect($plugin->data_payload['response']['data'])->toBe('test');
});
test('plugin parses iCal responses and filters to recent window', function (): void {