mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat(#129): add iCal response parser
This commit is contained in:
parent
838db288e7
commit
60f2a38169
12 changed files with 513 additions and 49 deletions
46
app/Services/Plugin/Parsers/XmlResponseParser.php
Normal file
46
app/Services/Plugin/Parsers/XmlResponseParser.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services\Plugin\Parsers;
|
||||
|
||||
use Exception;
|
||||
use Illuminate\Http\Client\Response;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use SimpleXMLElement;
|
||||
|
||||
class XmlResponseParser implements ResponseParser
|
||||
{
|
||||
public function parse(Response $response): ?array
|
||||
{
|
||||
$contentType = $response->header('Content-Type');
|
||||
|
||||
if (! $contentType || ! str_contains(mb_strtolower($contentType), 'xml')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$xml = simplexml_load_string($response->body());
|
||||
if ($xml === false) {
|
||||
throw new Exception('Invalid XML content');
|
||||
}
|
||||
|
||||
return ['rss' => $this->xmlToArray($xml)];
|
||||
} catch (Exception $exception) {
|
||||
Log::warning('Failed to parse XML response: '.$exception->getMessage());
|
||||
|
||||
return ['error' => 'Failed to parse XML response'];
|
||||
}
|
||||
}
|
||||
|
||||
private function xmlToArray(SimpleXMLElement $xml): array
|
||||
{
|
||||
$array = (array) $xml;
|
||||
|
||||
foreach ($array as $key => $value) {
|
||||
if ($value instanceof SimpleXMLElement) {
|
||||
$array[$key] = $this->xmlToArray($value);
|
||||
}
|
||||
}
|
||||
|
||||
return $array;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue