2022-08-28 18:44:30 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Core\PodcastData;
|
|
|
|
|
|
|
|
use DateTime;
|
2022-09-17 15:20:26 +00:00
|
|
|
use JsonSerializable;
|
2022-08-28 18:44:30 +00:00
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2022-09-17 15:20:26 +00:00
|
|
|
class PodcastData implements JsonSerializable {
|
2022-08-28 18:44:30 +00:00
|
|
|
private string $title;
|
|
|
|
private string $author;
|
|
|
|
private string $link;
|
|
|
|
private string $description;
|
|
|
|
private string $image;
|
|
|
|
private int $fetchedAtUnix;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $title,
|
|
|
|
string $author,
|
|
|
|
string $link,
|
|
|
|
string $description,
|
|
|
|
string $image,
|
|
|
|
int $fetchedAtUnix,
|
|
|
|
) {
|
|
|
|
$this->title = $title;
|
|
|
|
$this->author = $author;
|
|
|
|
$this->link = $link;
|
|
|
|
$this->description = $description;
|
|
|
|
$this->image = $image;
|
|
|
|
$this->fetchedAtUnix = $fetchedAtUnix;
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:20:26 +00:00
|
|
|
/**
|
|
|
|
* @return PodcastData
|
|
|
|
* @throws Exception if the XML data could not be parsed.
|
|
|
|
*/
|
2022-09-17 15:03:02 +00:00
|
|
|
public static function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): PodcastData {
|
2022-08-28 18:44:30 +00:00
|
|
|
$xml = new SimpleXMLElement($xmlString);
|
|
|
|
$channel = $xml->channel;
|
|
|
|
return new PodcastData(
|
|
|
|
title: (string)$channel->title,
|
2022-09-17 15:03:49 +00:00
|
|
|
author: (string)self::getXPathContent($xml, '/rss/channel/itunes:author'),
|
2022-08-28 18:44:30 +00:00
|
|
|
link: (string)$channel->link,
|
|
|
|
description: (string)$channel->description,
|
|
|
|
image:
|
2022-09-17 15:03:49 +00:00
|
|
|
(string)(self::getXPathContent($xml, '/rss/channel/image/url')
|
|
|
|
?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href')),
|
2022-09-17 15:03:02 +00:00
|
|
|
fetchedAtUnix: $fetchedAtUnix ?? (new DateTime())->getTimestamp(),
|
2022-08-28 18:44:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:20:26 +00:00
|
|
|
private static function getXPathContent(SimpleXMLElement $xml, string $xpath): ?string {
|
2022-08-28 18:44:30 +00:00
|
|
|
$match = $xml->xpath($xpath);
|
|
|
|
if ($match) {
|
|
|
|
return (string)$match[0];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:20:26 +00:00
|
|
|
private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath): ?string {
|
2022-08-28 18:44:30 +00:00
|
|
|
$match = $xml->xpath($xpath);
|
|
|
|
if ($match) {
|
|
|
|
return (string)$match[0][0];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle(): string {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAuthor(): string {
|
|
|
|
return $this->author;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLink(): string {
|
|
|
|
return $this->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDescription(): string {
|
|
|
|
return $this->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getImage(): string {
|
|
|
|
return $this->image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getFetchedAtUnix(): int {
|
|
|
|
return $this->fetchedAtUnix;
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:03:02 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-08-28 18:44:30 +00:00
|
|
|
public function __toString() : String {
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:03:02 +00:00
|
|
|
/**
|
2022-09-17 15:20:26 +00:00
|
|
|
* @return array<string,mixed>
|
2022-09-17 15:03:02 +00:00
|
|
|
*/
|
2022-08-28 18:44:30 +00:00
|
|
|
public function toArray(): array {
|
|
|
|
return
|
|
|
|
[
|
|
|
|
'title' => $this->title,
|
|
|
|
'author' => $this->author,
|
|
|
|
'link' => $this->link,
|
|
|
|
'description' => $this->description,
|
|
|
|
'image' => $this->image,
|
|
|
|
'fetchedAtUnix' => $this->fetchedAtUnix,
|
|
|
|
];
|
|
|
|
}
|
2022-09-17 15:03:02 +00:00
|
|
|
|
2022-09-17 15:20:26 +00:00
|
|
|
/**
|
|
|
|
* @return array<string,mixed>
|
|
|
|
*/
|
|
|
|
public function jsonSerialize(): mixed {
|
|
|
|
return $this->toArray();
|
|
|
|
}
|
|
|
|
|
2022-09-17 15:03:02 +00:00
|
|
|
/**
|
|
|
|
* @return PodcastData
|
|
|
|
*/
|
|
|
|
public static function fromArray(array $data): PodcastData {
|
|
|
|
return new PodcastData(
|
|
|
|
title: $data['title'],
|
|
|
|
author: $data['author'],
|
|
|
|
link: $data['link'],
|
|
|
|
description: $data['description'],
|
|
|
|
image: $data['image'],
|
|
|
|
fetchedAtUnix: $data['fetchedAtUnix'],
|
|
|
|
);
|
|
|
|
}
|
2022-08-28 18:44:30 +00:00
|
|
|
}
|
|
|
|
|