91 lines
2.0 KiB
PHP
91 lines
2.0 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace OCA\RePod\Core\PodcastData;
|
||
|
|
||
|
use OCA\GPodderSync\Core\PodcastData\PodcastData as CorePodcastData;
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
}
|