mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-03-14 12:23:33 +00:00
Compare commits
3 commits
0aa38428f6
...
b267923595
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b267923595 | ||
|
|
9c5b5b33f5 | ||
|
|
afc29e15d5 |
4 changed files with 66 additions and 18 deletions
|
|
@ -18,12 +18,12 @@ class XmlResponseParser implements ResponseParser
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$xml = simplexml_load_string($response->body());
|
$xml = $this->simplexml_load_string_strip_namespaces($response->body());
|
||||||
if ($xml === false) {
|
if ($xml === false) {
|
||||||
throw new Exception('Invalid XML content');
|
throw new Exception('Invalid XML content');
|
||||||
}
|
}
|
||||||
|
|
||||||
return ['rss' => $this->xmlToArray($xml)];
|
return [$xml->getName() => $this->xmlToArray($xml)];
|
||||||
} catch (Exception $exception) {
|
} catch (Exception $exception) {
|
||||||
Log::warning('Failed to parse XML response: '.$exception->getMessage());
|
Log::warning('Failed to parse XML response: '.$exception->getMessage());
|
||||||
|
|
||||||
|
|
@ -43,4 +43,25 @@ class XmlResponseParser implements ResponseParser
|
||||||
|
|
||||||
return $array;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@
|
||||||
"ext-imagick": "*",
|
"ext-imagick": "*",
|
||||||
"ext-simplexml": "*",
|
"ext-simplexml": "*",
|
||||||
"ext-zip": "*",
|
"ext-zip": "*",
|
||||||
"bnussbau/laravel-trmnl-blade": "2.1.1",
|
"bnussbau/laravel-trmnl-blade": "2.3.*",
|
||||||
"bnussbau/trmnl-pipeline-php": "^0.6.0",
|
"bnussbau/trmnl-pipeline-php": "^0.6.0",
|
||||||
"keepsuit/laravel-liquid": "^0.5.2",
|
"keepsuit/laravel-liquid": "^0.5.2",
|
||||||
"laravel/fortify": "^1.30",
|
"laravel/fortify": "^1.30",
|
||||||
|
|
|
||||||
14
composer.lock
generated
14
composer.lock
generated
|
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "60a7e51edd8408cffdb901e4a1c1684a",
|
"content-hash": "a4438c591a5d746f546eab261e9e1efc",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "aws/aws-crt-php",
|
"name": "aws/aws-crt-php",
|
||||||
|
|
@ -214,16 +214,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "bnussbau/laravel-trmnl-blade",
|
"name": "bnussbau/laravel-trmnl-blade",
|
||||||
"version": "2.1.1",
|
"version": "2.3.0",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/bnussbau/laravel-trmnl-blade.git",
|
"url": "https://github.com/bnussbau/laravel-trmnl-blade.git",
|
||||||
"reference": "6ad96eba917ebc30ebe550e6fce4a995e94f6b35"
|
"reference": "e19efc6c873816395e0989d078998f02ef4f0adf"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/bnussbau/laravel-trmnl-blade/zipball/6ad96eba917ebc30ebe550e6fce4a995e94f6b35",
|
"url": "https://api.github.com/repos/bnussbau/laravel-trmnl-blade/zipball/e19efc6c873816395e0989d078998f02ef4f0adf",
|
||||||
"reference": "6ad96eba917ebc30ebe550e6fce4a995e94f6b35",
|
"reference": "e19efc6c873816395e0989d078998f02ef4f0adf",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
|
@ -278,7 +278,7 @@
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/bnussbau/laravel-trmnl-blade/issues",
|
"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": [
|
"funding": [
|
||||||
{
|
{
|
||||||
|
|
@ -294,7 +294,7 @@
|
||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"time": "2026-01-29T20:40:42+00:00"
|
"time": "2026-02-09T12:00:32+00:00"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "bnussbau/trmnl-pipeline-php",
|
"name": "bnussbau/trmnl-pipeline-php",
|
||||||
|
|
|
||||||
|
|
@ -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"?>
|
$xmlContent = '<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<rss version="2.0">
|
<rss version="2.0">
|
||||||
<channel>
|
<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);
|
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 {
|
test('plugin parses JSON-parsable response body as JSON', function (): void {
|
||||||
$jsonContent = '{"title": "Test Data", "items": [1, 2, 3]}';
|
$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);
|
expect($plugin->data_payload['IDX_0'])->toBe($jsonResponse);
|
||||||
|
|
||||||
// Second URL should be XML wrapped under rss
|
// Second URL should be XML wrapped under rss
|
||||||
expect($plugin->data_payload['IDX_1'])->toHaveKey('rss');
|
expect($plugin->data_payload['IDX_1'])->toHaveKey('root');
|
||||||
expect($plugin->data_payload['IDX_1']['rss']['item'])->toBe('XML Data');
|
expect($plugin->data_payload['IDX_1']['root']['item'])->toBe('XML Data');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('plugin handles POST requests with XML responses', function (): void {
|
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();
|
$plugin->refresh();
|
||||||
|
|
||||||
expect($plugin->data_payload)->toHaveKey('rss');
|
expect($plugin->data_payload)->toHaveKey('response');
|
||||||
expect($plugin->data_payload['rss'])->toHaveKey('status');
|
expect($plugin->data_payload['response'])->toHaveKey('status');
|
||||||
expect($plugin->data_payload['rss'])->toHaveKey('data');
|
expect($plugin->data_payload['response'])->toHaveKey('data');
|
||||||
expect($plugin->data_payload['rss']['status'])->toBe('success');
|
expect($plugin->data_payload['response']['status'])->toBe('success');
|
||||||
expect($plugin->data_payload['rss']['data'])->toBe('test');
|
expect($plugin->data_payload['response']['data'])->toBe('test');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('plugin parses iCal responses and filters to recent window', function (): void {
|
test('plugin parses iCal responses and filters to recent window', function (): void {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue