repod/lib/Core/PodcastData/PodcastData.php
Michel Roux 822bf92a47
All checks were successful
repod / xml (push) Successful in 14s
repod / php (push) Successful in 59s
repod / nodejs (push) Successful in 2m13s
repod / release (push) Has been skipped
ci: fix psalm check
2024-01-13 00:34:32 +01:00

106 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\RePod\Core\PodcastData;
use OCA\GPodderSync\Core\PodcastData\PodcastData as CorePodcastData;
/**
* @psalm-type PodcastDataType = array{
* title: ?string,
* author: ?string,
* link: ?string,
* description: ?string,
* imageUrl: ?string,
* fetchedAtUnix: int,
* imageBlob: ?string,
* atomLink: ?string
* }
*/
class PodcastData extends CorePodcastData implements \JsonSerializable
{
public function __construct(
?string $title,
?string $author,
?string $link,
?string $description,
?string $imageUrl,
int $fetchedAtUnix,
?string $imageBlob = null,
private ?string $atomLink
) {
parent::__construct(
$title,
$author,
$link,
$description,
$imageUrl,
$fetchedAtUnix,
$imageBlob
);
}
/**
* @throws \Exception if the XML data could not be parsed
*/
public static function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): PodcastData {
$xml = new \SimpleXMLElement($xmlString);
$channel = $xml->channel;
return new PodcastData(
self::stringOrNull($channel->title),
self::getXPathContent($xml, '/rss/channel/itunes:author'),
self::stringOrNull($channel->link),
self::stringOrNull($channel->description),
self::getXPathContent($xml, '/rss/channel/image/url')
?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href'),
$fetchedAtUnix ?? (new \DateTime())->getTimestamp(),
null,
self::getXPathContent($xml, '/rss/channel/atom:link/@href')
);
}
/**
* @param null|\SimpleXMLElement|string $value
*/
public static function stringOrNull($value): ?string {
if ($value) {
return (string) $value;
}
return null;
}
public function getAtomLink(): ?string {
return $this->atomLink;
}
/**
* @return PodcastDataType
*/
public function toArrayWithExtras() {
return array_merge(parent::toArray(), [
'atomLink' => $this->atomLink,
]);
}
private static function getXPathContent(\SimpleXMLElement $xml, string $xpath): ?string {
$match = $xml->xpath($xpath);
if ($match) {
return (string) $match[0];
}
return null;
}
private static function getXPathAttribute(\SimpleXMLElement $xml, string $xpath): ?string {
$match = $xml->xpath($xpath);
if ($match) {
return (string) $match[0][0];
}
return null;
}
}