Changed to implement JsonSerializable

This commit is contained in:
Kalle Fagerberg 2022-09-17 17:20:26 +02:00 committed by thrillfall
parent 2b82e9e9c9
commit 403ead674d
3 changed files with 30 additions and 10 deletions

View File

@ -35,12 +35,8 @@ class PersonalSettingsController extends Controller {
*/
public function metrics(): JSONResponse {
$metrics = $this->metricsReader->metrics($this->userId);
$metricsArrays = array_map(function (PodcastMetrics $metric) {
return $metric->toArray();
}, $metrics);
return new JSONResponse([
'subscriptions' => $metricsArrays,
'subscriptions' => $metrics,
]);
}
}

View File

@ -4,9 +4,10 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\PodcastData;
use DateTime;
use JsonSerializable;
use SimpleXMLElement;
class PodcastData {
class PodcastData implements JsonSerializable {
private string $title;
private string $author;
private string $link;
@ -30,6 +31,10 @@ class PodcastData {
$this->fetchedAtUnix = $fetchedAtUnix;
}
/**
* @return PodcastData
* @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;
@ -45,7 +50,7 @@ class PodcastData {
);
}
private static function getXPathContent(SimpleXMLElement $xml, string $xpath) {
private static function getXPathContent(SimpleXMLElement $xml, string $xpath): ?string {
$match = $xml->xpath($xpath);
if ($match) {
return (string)$match[0];
@ -53,7 +58,7 @@ class PodcastData {
return null;
}
private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath) {
private static function getXPathAttribute(SimpleXMLElement $xml, string $xpath): ?string {
$match = $xml->xpath($xpath);
if ($match) {
return (string)$match[0][0];
@ -111,7 +116,7 @@ class PodcastData {
}
/**
* @return array
* @return array<string,mixed>
*/
public function toArray(): array {
return
@ -125,6 +130,13 @@ class PodcastData {
];
}
/**
* @return array<string,mixed>
*/
public function jsonSerialize(): mixed {
return $this->toArray();
}
/**
* @return PodcastData
*/

View File

@ -3,7 +3,9 @@ declare(strict_types=1);
namespace OCA\GPodderSync\Core\PodcastData;
class PodcastMetrics {
use JsonSerializable;
class PodcastMetrics implements JsonSerializable {
private string $url;
private int $listenedSeconds;
private PodcastActionCounts $actionCounts;
@ -56,6 +58,9 @@ class PodcastMetrics {
return $this->podcastData;
}
/**
* @return array<string,mixed>
*/
public function toArray(): array {
return
[
@ -65,4 +70,11 @@ class PodcastMetrics {
'podcastData' => $this->podcastData->toArray(),
];
}
/**
* @return array<string,mixed>
*/
public function jsonSerialize(): mixed {
return $this->toArray();
}
}