Extracted fromArray into new function

This commit is contained in:
Kalle Fagerberg 2022-09-17 17:03:02 +02:00 committed by thrillfall
parent 9b42b486ae
commit aaadcf17b6
2 changed files with 24 additions and 11 deletions

View File

@ -30,7 +30,7 @@ class PodcastData {
$this->fetchedAtUnix = $fetchedAtUnix;
}
public static function parseXml(string $xmlString): PodcastData {
public static function parseRssXml(string $xmlString, ?int $fetchedAtUnix = null): PodcastData {
$xml = new SimpleXMLElement($xmlString);
$channel = $xml->channel;
return new PodcastData(
@ -41,7 +41,7 @@ class PodcastData {
image:
self::getXPathContent($xml, '/rss/channel/image/url')
?? self::getXPathAttribute($xml, '/rss/channel/itunes:image/@href'),
fetchedAtUnix: (new DateTime())->getTimestamp(),
fetchedAtUnix: $fetchedAtUnix ?? (new DateTime())->getTimestamp(),
);
}
@ -103,10 +103,16 @@ class PodcastData {
return $this->fetchedAtUnix;
}
/**
* @return string
*/
public function __toString() : String {
return $this->title;
}
/**
* @return array
*/
public function toArray(): array {
return
[
@ -118,5 +124,19 @@ class PodcastData {
'fetchedAtUnix' => $this->fetchedAtUnix,
];
}
/**
* @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'],
);
}
}

View File

@ -42,7 +42,7 @@ class PodcastDataCache {
throw new \ErrorException("Podcast RSS URL returned non-2xx status code: $statusCode");
}
$body = $resp->getBody();
return PodcastData::parseXml($body);
return PodcastData::parseRssXml($body);
}
public function tryGetCachedPodcastData(string $url): ?PodcastData {
@ -50,14 +50,7 @@ class PodcastDataCache {
if (!$oldData) {
return null;
}
return new PodcastData(
title: $oldData['title'],
author: $oldData['author'],
link: $oldData['link'],
description: $oldData['description'],
image: $oldData['image'],
fetchedAtUnix: $oldData['fetchedAtUnix'],
);
return PodcastData::fromArray($oldData);
}
public function trySetCachedPodcastData(string $url, PodcastData $data) {